raycast trigger specific animations
My script worked fine with only one animation in the room to trigger with raycast, but I want two objects with animation (or more) and the animation to play only when raycast is hitting that particular objects trigger. The script below will trigger both animations if I mouseclick while looking at either trigger. I know I need to specify which trigger I am looking at some how like (hit.collider.Tag = "triggerstagname") but that and variations gave me errors.
using UnityEngine;
using System.Collections;
public class CameraRaycast : MonoBehaviour {
Animation animation1;
Animation animation2;
void Start() {
animation1 = GameObject.Find("dancertest").GetComponent<Animation>();
animation2 = GameObject.Find("MetalGrate").GetComponent<Animation>();
}
void Update() {
Ray ray = camera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 4))
{
if(hit.collider.isTrigger)
{
if (Input.GetKeyDown(KeyCode.Mouse1))
animation1.Play("Dance");
if (Input.GetKeyDown(KeyCode.Mouse1))
animation2.Play("GrateExplodes");
}
}
}
}
Answer by shadowpuppet · Jun 02, 2016 at 12:25 AM
answering my own question . the script triggers 3 different animations when hit by ray cast and right mouse is clicked. the script also changes the cursor to hilite when over the object that can animate. That is why the unity engine.UI up top
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class CameraRaycast : MonoBehaviour {
public Vector3 point = new Vector3(0.5f, 0.5f, 0);
Animation animation1;
Animation animation2;
Animation animation3;
private MonoBehaviour hilite;
void Start() {
animation1 = GameObject.Find("dancertest").GetComponent<Animation>();
animation2 = GameObject.Find("FratBoy").GetComponent<Animation>();
animation3 = GameObject.Find("NEWFATMAN").GetComponent<Animation>();
hilite = GameObject.Find("CrossHair2").GetComponent<RawImage>();
}
void Update() {
Ray ray = camera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 4))
{
Debug.DrawLine(ray.origin, hit.point);
if(hit.transform.gameObject.tag == ("DanceTrigger"))//
hilite.enabled = true;
if(hit.transform.gameObject.tag == ("FratIdle"))//
hilite.enabled = true;
if(hit.transform.gameObject.tag == ("Enemy"))//
hilite.enabled = true;
}
else hilite.enabled = false;
{
//print ( "Nothing here to look at");
}
if(hit.transform.gameObject.tag == ("DanceTrigger"))//animation
if (Input.GetKeyDown(KeyCode.Mouse1))
animation1.Play("Dance");
if(hit.transform.gameObject.tag == ("FratIdle"))//animation
if (Input.GetKeyDown(KeyCode.Mouse1))
animation2.Play("FratIdle");
if(hit.transform.gameObject.tag == ("Enemy"))//animation
if (Input.GetKeyDown(KeyCode.Mouse1))
animation3.Play("attack");
}
}
Your answer
Follow this Question
Related Questions
Storing successive raycast hits in array? 2 Answers
Trigger and Rotation Issue 0 Answers
In a object where to use raycasting? 1 Answer
Raycast is going through collider (A plane) 0 Answers
Foreach working very strange 0 Answers