seem like the trail renderer variables are inaccessible through script? is that right? seems strange because they are listed in the script reference…
I’m looking for a way to turn the rendering of the trail on/off with a user input.
off course enabled could be an option, but if i use that the trail doesn’t follow the player anymore while turned off, in other words if i turn it back on again it draws a straight line between the position i turned it off and the position of the player where turn on again…
anyway of keeping track of the player position while turned off?
i tried changing the width’s to 0 instead of setting enabled to false, but that doesn’t work for some reason.
here’s the script of this;
using UnityEngine;
using System.Collections;
public class TrailSwitch : MonoBehaviour {
public KeyCode UserKey = KeyCode.T;
TrailRenderer Trail;
bool TrailOn;
// Use this for initialization
void Start () {
Trail = gameObject.GetComponent<TrailRenderer>();
}
// Update is called once per frame
void Update () {
if(Input.GetKey(UserKey) TrailOn){
Trail.startWidth = 0.0f;
Trail.endWidth = 0.0f;
TrailOn = false;
}else if(Input.GetKey(UserKey) !TrailOn){
Trail.startWidth = 0.5f;
Trail.endWidth = 0.25f;
TrailOn = true;
}
}
}
I too lamented the lack of an emit off button for this component.
You might try setting the time to 0 instead of the width to 0. If you lerp it from it’s current value to 0 you can give it a chance to quickly finish up and then it will follow the player but not actually draw anything.
The alternatives are (this is in no way a comprehensive list):
A) To use the autodestruct part of the trail renderer and instantiate a trail renderer object when you need it… parented to a transform on your game object so it follows properly and then un-parent it when you no longer need it. The current cycle will complete and the object will delete it’s self.
B) Roll your own thingy that does the same job but has a public variable that tells the trail render to not emit anymore new points. This final solution lends it’s self more useable in situations where you’re trying to hook it up to an object pool. There’s some tron trail code on the wiki that might get you started if you choose this route.
P.S. If you do the full monty and code up a kickin-rad trail render drop me a copy pretty pretty please.
thanks
i tied the time also but thats the same looks like it’s inaccessible to…
will have a look at the tron wiki and maybe try it with line renderen instead of trail… anyway it’s for debugging movement flow of the player over a map, so it’s a not so important issue for me atm. but i was astonished that you can’t acces the variables while the are given in the script reference… i mean whats the use putting them there if you can access them by script?
The code as posted worked for me.
I dropped this script onto an object with a trail renderer component.
Pressed the T key and the time toggled between 0 and 1000
Unity 3.5.3 So I can only think of a version difference or possibly you’re setting the trail renderer value in other areas (outside of this script).
i have it on a player where the trail is on also… i do run a server that spawns the player, but that shouldn’t matter.
it works if i change it to Trail.enabled = false; so i don’t get why this doesn’t…
i’m running 3.5.0 … strange, will try it in a new project not in Multiplayer.
edit: darn now they all work or at least changing start and end width also work… so it has to do with my setup… think it’s maybe the difference between having the object in the scene and instantiating it through script. I’ll try so more stuff.
edit2: Ok so It’s not the fact that it gets instantiated, just tested that…
edit3: wow strange phenomena’s are hunting me… it works on my desktop but it doesn’t on my laptop :-s…???