Reputation: 1
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.
Upvotes: 0
Views: 338
Reputation: 2174
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.
Upvotes: 0