I have created a first person controller as an enemy in my game (Zombie). My character is an archer that uses arrows as projectiles that are shot with the following code:
if(Input.GetButton("Fire1")) {
shotForce += 45f;
}
if (Input.GetMouseButtonUp(0)) {
Rigidbody shot = Instantiate (projectile, shotPos.position, shotPos.rotation) as Rigidbody;
shot.mass = shotMass;
shot.AddForce (Camera.main.transform.forward * shotForce);
shotForce = 0f;
}
I have a script to parent the fired arrow to the collided object which works fine for most objects in the game, but my enemy object (Zombie) speeds up and moves all around the place on a collision, I'm new to unity, I've spent a few hours trying to resolve this with out success.
I have a Rigidbody, Charcter controller and First person controller on the Zombie.
I have a Rigidbody and Sphere Collider on the Arrow.
The method I think is causing the problem:
void OnCollisionEnter(Collision collision){
if (this.flying) {
this.flying = false;
Destroy (this.GetComponent<Rigidbody>());
this.transform.position = collision.contacts [0].point;
GameObject anchor = new GameObject ("ARROW_ANCHOR");
anchor.transform.position = this.transform.position;
anchor.transform.rotation = this.transform.rotation;
anchor.transform.parent = collision.transform;
this.anchor = anchor.transform;
collision.gameObject.SendMessage ("arrowHit", SendMessageOptions.DontRequireReceiver);
}
}
I think the issue is with the line anchor.transform.parent = collision.transform;
Maybe there is a solution without parenting the arrow to the Zombie?
I'm stumped at this stage any help appreciated.
rigidbody, but it still happens, I think you're on the right track though, something is getting force added to it on the collision, but what and why, I don't know. \$\endgroup\$