- Home /
Cant get Raycast to activate animations on different game objects with an if statement
as stated in my title I'm completely baffled as to why this wont work.
Heres my code`
void Start ()
{
line = gameObject.GetComponent<LineRenderer> ();
line.enabled = false;
Random.seed = (int)Time.time;
}
void Update ()
{
if (Input.GetButtonDown ("Use")) {
StopCoroutine ("FireLaser");
StartCoroutine ("FireLaser");
}
}
IEnumerator FireLaser ()
{
line.enabled = true;
while (Input.GetButtonDown("Use")) {
Ray ray = new Ray (transform.position, transform.forward);
line.SetPosition (0, ray.origin);
if (Physics.Raycast (ray, out hit, 2)) {
line.SetPosition (1, hit.point);
if (hit.rigidbody) {
if (hit.rigidbody.gameObject.tag == "Bottom") {
anim = GameObject.FindGameObjectWithTag ("botPano").GetComponent<Animator> ();
Debug.Log ("CheckBelow");
anim.SetTrigger ("RaysHit");
}
if (hit.rigidbody.gameObject.tag == "Under") {
anim = GameObject.FindGameObjectWithTag ("undPano").GetComponent<Animator> ();
Debug.Log ("CheckUnder");
anim.SetTrigger ("RaysHit");
}
if (hit.rigidbody.gameObject.tag == "Outside") {
anim = GameObject.FindGameObjectWithTag ("outPano").GetComponent<Animator> ();
Debug.Log ("CheckOut");
anim.SetTrigger ("RaysHit");
}
}
} else
line.SetPosition (1, ray.GetPoint (2));
yield return null;
}
line.enabled = false;
}}
`
As you can see, the three if statments are practically identical, as are the gameObjects and the animations involved, but it will only accept a gameobject with the tag "botPano" regardless of where it appears in the script!
I hope theres somebody out there that can help because I need this urgently! Thanks in advance!
Comment