Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How do I toggle the trail renderer for my ski game

Discussion in 'Scripting' started by kootenay, Feb 23, 2011.

  1. kootenay

    kootenay

    Joined:
    Feb 8, 2011
    Posts:
    116
    So, I am aware of the "GetComponent(TrailRenderer).enabled = true;" script function, but when I switch this to false in my move script below - it turns off the entire trail. I want to be able to start and stop it when my skier gets airborne.

    Here is my script below... I have tried popping it into my script as per below when the skier is on the ground only, but when I disable when in the air, the whole track/trail dissappears from my scene. Any ideas?


    var speed : float = 10.0;
    var jumpSpeed : float = 8.0;
    var gravity : float = 10.0;
    private var moveDirection : Vector3 = Vector3.zero;
    var tracker: Transform;
    var rotateSpeed = 3.0;

    function Update ()
    {
    var controller : CharacterController = GetComponent(CharacterController);
    transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
    var forward = transform.TransformDirection(Vector3.forward);
    var curSpeed = speed * Input.GetAxis ("Vertical");
    controller.SimpleMove(forward * curSpeed);

    var hit1: RaycastHit;
    var hit2: RaycastHit;

    if (Physics.Raycast(tracker.position, -Vector3.up, hit1)) {
    if (Physics.Raycast(tracker.TransformPoint(Vector3.forward * 0.1), -Vector3.up, hit2)) {
    transform.rotation = Quaternion.LookRotation(hit2.point - hit1.point, hit1.normal);
    }
    if (controller.isGrounded) {
    // We are grounded, so recalculate
    // move direction directly from axes
    moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
    Input.GetAxis("Vertical"));
    moveDirection = transform.TransformDirection(moveDirection);
    moveDirection *= speed;

    gameObject.GetComponent(ParticleEmitter).emit= true; //tell the emitter to start
    GetComponent(TrailRenderer).enabled = true; // this isn't working
    if (Input.GetButton ("Jump")) {
    moveDirection.y = jumpSpeed;
    }
    }
    else {
    gameObject.GetComponent(ParticleEmitter).emit= false; //tell the emitter to stop.
    GetComponent(TrailRenderer).enabled = false; // this switches off the trail for the whole scene, not just when skier is in the air

    }

    // Move the controller
    controller.Move(moveDirection * Time.deltaTime);
    }

    }
    function FixedUpdate () {
    rigidbody.AddForce (-Vector3.up * 10);

    }
    @script RequireComponent(CharacterController)
     
  2. Simba

    Simba

    Joined:
    Nov 8, 2010
    Posts:
    77
    You probably could unattach the trail renderer, then when grounded again, create a copy. That would be quite hard, I suspect, and render-heavy. That's just my idea - you could give it a shot. But I don't know if it would work perfectly.

    Honestly though, I'm just guessing.

    Edit: you could comment out the enabled = false line. Again, just a suggestion - but the emmiter.emit = false looks like it might do what you're thinking of.
     
    Last edited: Feb 23, 2011
  3. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    That's not really possible with the built-in trail renderer, or even the line renderer. You will need to create your own procedural mesh, which allows for disconnected vertices.

    Look at Yoggy's time based trail renderer or the procedural example project for inspiration.
     
  4. KevinCodes4Food

    KevinCodes4Food

    Joined:
    Dec 6, 2013
    Posts:
    61
  5. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Congratulations Kevin.

    You dug up two super old threads.

    /high five