- Home /
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?
Answer by kilgore · Aug 18, 2011 at 12:52 AM
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?
Well, the gameobject that has the trail script on it is a parent, but you're right, the trail is not a parent. I have no idea how to make the trail a parent.
Answer by stopsecret · Aug 18, 2011 at 01:34 AM
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 did that. It doesn't work. Here's the script:
var trail : GameObject;
function Update ()
{
if(Input.Get$$anonymous$$eyDown ("space")){
var trailTransform = Instantiate(trail, transform.position, transform.rotation);
trailTransform.parent = transform;
}
if(Input.Get$$anonymous$$eyUp ("space")){
Destroy(transform.trail);
}
}
What did I do wrong?
Answer by stopsecret · Aug 18, 2011 at 01:48 AM
I think what you did wrong was put var in front of trailTransform, try without it...
Answer by stopsecret · Aug 18, 2011 at 01:59 AM
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 ;)
Perfect man! This works. Thanks a lot. (Now I just have to figure out how to destroy it :(
Answer by Peppercat · Aug 27, 2013 at 09:28 PM
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;