- Home /
Question by
Kek_Kek · Nov 08, 2017 at 01:10 AM ·
airaycasting
How Can I make an AI change animation once looked at using Raycasting.
I'm trying to make it if the AI is looked at using Raycasting on my player, It changes from Idle Animation to another Animation.
Comment
Best Answer
Answer by Daemonhahn · Nov 09, 2017 at 12:44 PM
Vector3 direction; // this is just for illustration, essentially you want a vector that points towards the object
RaycastHit hit;
float distance;
Vector3 rayCastDirection;
if (Physics.Raycast(transform.position,rayCastDirection, out hit, distance))
{
if(hit.tag == "Player")
{
print("Found an object - distance: " + hit.distance);
// set a bool for idle state here, for example isIdle = true;
// then in your animation script, have the mecanim set the idle state based on that bool (animator.SetBool("isIdle", isIdle);
// that will cause the idle state to happen whenever isIdle is true, which is now set from that raycast
}
}
Answer by TanselAltinel · Nov 09, 2017 at 12:22 PM
Hi DarthVader1234
By your question, I'm going to assume that you have at least basic knowledge of scripting.
What you'd basically do is that
Have a public function in your AI scrip that will change its animation
Check if your raycast actually hit your AI object.
If it did, use get component on the hit result object to get AI script component, and then call animation change function.
That should solve the issue easily.