0
votes

I have a gameobject called "Target" which contains the animator controller, I have a capsule collider on a gameObject in the tree for Target called Robot. The current script I have is attached to a gun object and is as follows and destroys the object and it disappears when the raycast collides with the collider, and prints out the tag and name of the collider it hit(just for testing purposes):

RaycastHit hit;
    if (Physics.Raycast (fpsCam.transform.position, fpsCam.transform.forward, out hit, range)) {
        if (hit.transform.tag == "Target") {

             print(hit.transform.gameObject.name);
            print(hit.transform.gameObject.tag);
            hit.transform.gameObject.GetComponent<Animator>().SetBool("isHit", true);
            Destroy(hit.transform.gameObject);
            target.getTargets = GameObject.FindGameObjectsWithTag ("Target");
            target.targetCount = target.getTargets.Length -1;
            target.countText.text = (target.targetCount ).ToString ();

        }
        GameObject impact = Instantiate (impactEffect, hit.point, Quaternion.LookRotation (hit.normal));
        Destroy (impact, 1f);
    }
}

However when i active the animator controller the raycast no longer seems to hit the collider at all, the name nor tag is printed out. I have a isHit variable in the animator that is supposed to play a death animation when set to true and then disappear as it should when no animator is present but I'm not sure how to access the animator on the main object from the child object that has the collider.

1
Does it hit the if statement in your code? And if it does, what is the name of the tag? Perhaps the name of the animator controller is returned instead of "Target" (would be odd, but it is possible)H.J. Meijer
To clarify: The above scripts works if the animator on the target is disabled, but doesn't work anymore when it is enabled?Gunnar B.

1 Answers

0
votes

It is hard to say why your code does not work or print out the values you are looking for.

Try debugging it and see where it ends up.

However your question on how to access the animator on the main object. Add root to your line.

hit.transform.root.GetComponentInChildren<Animator>().SetBool("isHit", true);

I removed the ".gameObject" because it is not needed. Also GetComponentInChildren will find the first occurence of Animator starting at the root.