Boost Streak/Trail

I’m making a space sim, and when the ship activates it’s boosters, a trail renderer has to appear and create a trail as it boosts, and when the player turns off boost, the trail renderer disappears. Right now, when the player boosts, in the hierarchy, it says that the trail are there, but visibly, I can’t see them on the ship. The trails don’t destroy themselves when the boost if off either. Here’s the set up

On each wing, an empty game object is there. To the game objects I added the trail script:

var trail : GameObject;

function Update () 
{
	if(Input.GetKeyDown ("space")){
Instantiate(trail, transform.position, transform.rotation);
}
	if(Input.GetKeyUp ("space")){
Destroy(transform.trail);
}
}

When you hit space, the trail supposedly instantiates (btw, I’m using a trail renderer to make the trail) but nothing happens. Plus, the trail doesn’t destroy itself like I wrote in the script. There are no errors, so the syntax is correct. I’m not sure why it’s doing this. Help would be appreciated. Thanks in advance.

Update

The questions has almost been answered thankfully to stopsecret but it hasn’t been completely answered. Now, using stopsecret’s script, I need to make it destroy when you let go of the space bar. I’m not sure how to fix that. Thanks in advance.

Update

I finally figured how to destroy the trail, but the problem is, once the trail is destroyed, you can’t instantiate it again, so when I boost, the trail appears, but when I boost again the trail isn’t there. So I need to figure out a way to destroy the trail when not boosting, and have it continually come up whenever I boost. Here’s the script:

var trail : GameObject;

function Update ()

{

if(Input.GetKeyDown ("space")){

var trailTransform = Instantiate(trail, transform.position, transform.rotation);

trailTransform.transform.parent = transform;

}
if(Input.GetKeyUp ("space")){

Destroy(gameObject);

}
In the script, once the gameObject is destroyed, it doesn't instantiate again. How would I solve this?

It looks like you need to parent the instantiated trail object to the ship. Can you check to see if it is being instantiated at zero coords?

to make the trail a parent, here’s what you do: ok, so before Instantiate, type a variable you want like trailTransform, then put an equals sign. Now this makes trailTransform a variable, and it can be accessed like a regular object. You can from there type trailTransform.parent = transform in order to parent the trail to your empty object. Finished code looks like this:

trailTransform = Instantiate(trail, transform.position, transform.rotation);

trailTransform.parent = transform

I hope this works, good luck on your space game endeavor!

I think what you did wrong was put var in front of trailTransform, try without it…

ok, I have taken your script into unity and tested it out. After fixing the errors, here’s what i’ve come up with. I couldn’t get rid of the trail after I was done, but I think you can figure that out… problem was I called trailTransform’s gameObject, which i don’t think has a parent function. A quick conversion to transform, and you’re all set!

var trail : GameObject;

function Update ()

{

if(Input.GetKeyDown ("space")){

var trailTransform = Instantiate(trail, transform.position, transform.rotation);

trailTransform.transform.parent = transform;

}
if(Input.GetKeyUp ("space")){

Destroy(transform.trail);

}

}

Hope this one actually works :wink:

In answer to your second update, why don’t you put your “trail” object in Resources folder? Then you can instantiate it as a prefab, and consequently instantiate and destroy it all the times you need to. That worked for me, hope it helps!

To create it:

GameObject trail = Instantiate(Resources.Load("TrailPrefab") as GameObject, gameObject.transform.position, Quaternion.identity) as GameObject;
trail.transform.parent = gameObject.transform;

You could also put a Trail Renderer on your ship and use something like this.

public class X
{
TrailRenderer tempTrail = null;

Start()
{
tempTrail = this.GetComponent<TrailRenderer>();
}

Update()
{
if (Input.GetKeyDown("space"))
        {
            tempTrail.renderer.enabled = false;
        }
}

}