2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ could it be possible that the arrow is adding force to the zombie rigidbody when they collide ? \$\endgroup\$ – Uri Popov Apr 26 '16 at 13:39
  • \$\begingroup\$ I've removed the 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\$ – Johntk Apr 26 '16 at 15:07
  • \$\begingroup\$ It seems to be the Character Controller that the force is being added to. \$\endgroup\$ – Johntk Apr 26 '16 at 15:53
1
\$\begingroup\$

I solved this by adding GetComponent<Rigidbody>().isKinematic = true; to my Arrow script in the OnCollisionEnter function.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.