- Home /
Collision problem
var rocket : Rigidbody; var speed = 10.0;
function FireRocket() { var rocketClone : Rigidbody = Instantiate(rocket,transform.position,transform.rotation); rocketClone.velocity = transform.forward * speed; } function OnCollisionEnter () { Destroy (gameObject); }
function Update (){ if (Input.GetButtonDown ("Fire1")){ FireRocket(); } }
Hi there! I'm new to unity and I'm just learning how to make basic game elements, right now I'm trying to have this rocket disappear when it hits a wall or some object, but it does not do that. I wasn't able to find many other relevant questions like mine.
The object that shoots out is a cube (I know, not a missile) and at first it collides with terrain - that didn't work. So I had the cube collide with a cube and that didn't work either. The colliders are all on and I feel like I am missing something fairly obvious.
Thanks in advance for the help!
Answer by aldonaletto · Apr 16, 2012 at 04:55 AM
The OnCollisionEnter code is in the weapon's script, but it should be in the missile script - that's the missile that collides with things, not the weapon!
Weapon script:
var rocket : Rigidbody; var speed = 10.0;
function FireRocket(){ var rocketClone : Rigidbody = Instantiate(rocket,transform.position,transform.rotation); rocketClone.velocity = transform.forward * speed; }
function Update (){ if (Input.GetButtonDown ("Fire1")){ FireRocket(); } } - Rocket script:
function OnCollisionEnter (){ Destroy (gameObject); }
I get it! Or at least I think I do.
I parented (I think this is the right term) the rocket script to the game object (a cube with Rigidbody component) then I made the game object the Rocket variable in the weapon script!
Thanks!
when you want to use the on trigger function you need to make sure you set up a collider as a trigger.
No, this isn't parenting: you added the script to the projectile. A script is a component, just like Rigidbody, Collider etc., and is added to some GameObject, while parenting is a hierarchycal relationship between GameObjects. $$anonymous$$any people (me included) confuse these two concepts at first, what causes a lot of headaches.
Your answer
Follow this Question
Related Questions
Flight Script throws no errors but does nothing. 2 Answers
SendMessage Not Working: JavaScript 1 Answer
Checking to see if two objects are colliding. 2 Answers
OnGui working with javascript but not with C# 2 Answers
OnCollisionEnter not working. 2 Answers