Trigger animation when looking at an object?
How can I play an animation when a player looks at an object, and how would I set a distance for how far they would have to be looking at the object before it triggers the animation? Would I use raycasting? can anyone give me an example?
Comment
Raycasting sounds viable and has a distance argument. You asked 'how far they would be looking at before it triggers animation' - did you mean how long?
I meant at a distance of 10 feet the trigger would not fire, but if you got to a distance of 2 feet it would then activate.
Answer by SpaceManDan · Sep 02, 2015 at 12:44 AM
You probably want something like this:
RaycastHit hitInfo;
Ray ray = new Ray(transform.position, transform.forward);
bool didRayHit = Physics.Raycast(ray, out hitInfo, 10f);
if (didRayHit)
{
print("I am a 10 unit long ray hitting " + hitInfo.transform.name);
//hitInfo.doAnimationStuffHere;
}
the float 10 in the physics.raycast is the length of the ray. change it to any float value to change the length of the ray.