Scripting animations

I’ve done some research but not sure how to phrase this one concisely. What I’m looking to do is have my human shaped model change animation on a button click.

I’ve got this human model which has a number of animations that I’ve set up to loop by dragging them into the Animator (such as Idle, Walk etc.). This all works fine. However, when I click a button I then want it to play a different animation, then return to the loop. I’ve tried the standard Raycast with animation.Play(); function but it brings up the error message that theres no animation attached to the GameObject (even if I add the Animation component to the Inspector).

I believe there’s a different script thats appropriate to use, something that includes booleans? Can anyone point me in the right direction?

You haven’t really given a lot to work with, but a few possible reasons is that you model either doesn’t have a collider, so the ray goes right through it and hit something in the background. Or some of the child elements have colliders and are being hit instead of the parent object.

First step is to print the name of the object you hit. If this is the name of your model the raycast is done correctly. If not then make sure to add a collider only to the parent object, the one with the animation component attached.

Sorry I haven’t been very detailed, I’m not really sure how to phrase it. Basically, when importing a model with animations on it, how do I code it so I can change to a different animation when I click a button?

For example, my model is idling by default but when I click a button I want it to play the walk cycle then return to idle.