- Home /
homing missiles and transform and colission help c#
hi ive been looking everywhere online for some help with coding a homing missile in unity can any one help and tell me where i am going wrong
public Rigidbody projectile;
public float speed = 10.0f;
Public Transform target;
void Update() {
if (Input.GetButtonDown("Fire1")) {
Rigidbody clone;
clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
clone.transform.LookAt(target);
clone.velocity = transform.TransformDirection(Vector3.forward * speed);
Destroy(clone.gameObject,5.0f);
}
} void OnCollisionEnter(Collision collision){
Destroy(clone.gameObject);
}
the code used to just shoot the projectile in front of the character and would bounce off the walls and then after 5 seconds it would be destroyed after i tried implementing destroying the object when ever it collides with something and trying to make it go towards the given target it doesn't even move now
can i please get some help I've tried looking online for tutorials but they are all in java and I've tried converting the code to c# but it doesn't work
enter code here
Answer by aldonaletto · Jan 22, 2012 at 07:09 PM
You must add the OnCollisionEnter event to the projectile script, not to the creator's:
// projectile script: void OnCollisionEnter(Collision collision){ // <- instantiate the explosion here, if you want -> Destroy(gameObject); // destroy projectile }
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to call OnTriggerEnter once 5 Answers
Hiding a GUI Texture upon death. 1 Answer
Help in solving mistake in my code 1 Answer