Animation Through Script

Updated on January 15, 2017 in [A] Animation
Share on Facebook0Tweet about this on Twitter0Share on Google+0Share on Reddit0
8 on January 8, 2017

How do I start an animation through a script? please help me, I want the script to start an animation when it sees a player close, it follows them. Basically I’m making a wolf that starts running when it sees a player please help me.

  • Liked by
Reply
0 on January 8, 2017

You need help for the animation or the pathfinding?

  • Liked by
Reply
Cancel
0 on January 9, 2017

the animation, I want it to play but can’t figure out how.

  • Liked by
Reply
Cancel
0 on January 9, 2017
  • Liked by
Reply
Cancel
1 on January 9, 2017

Okay, well, i done a little search (Very little :P) And i’m rather sure given the name of the method that Animation.play is what you’re looking for.

Im sure you’re going to need to attach the animation through a public variable in order to then find it so it can actually play the animation. for example

public animation MyAnimation;

then reference the identifier of the animation (MyAnimation).Play

I wanted to test this for you but i am having problems setting up my enviornment as off now.

However since this is all you wanted to know, I’m sure your capable of assembling the rest.

on January 11, 2017

thank you man!

Show more replies
  • Liked by
Reply
Cancel
0 on January 11, 2017

The ‘Best’ way to do this is by using a Animator (Animator component), in this component you can set the transitions between animations + the conditions (and al lot more!) these conditions can you acces and set via script.
https://unity3d.com/learn/tutorials/topics/animation/animator-controller

  • Liked by
Reply
Cancel
0 on January 12, 2017

thnx everyone!!! so much help!

  • Liked by
Reply
Cancel
0 on January 15, 2017

I had been creating some animations recently, and thought I’d give my two cents here.
The way I decided worked best is as follows:

public Animator animator;
 
public void PlayAnimation() {
  animator.speed = 1;
  animator.Play( "NameOfAnimation", 0, percentAtWhichToStart );
}

NameOfAnimation is the name of an animation within the animator controller.
‘0’ is the default layer of animation.
‘percentAtWhichToStart’ is a float between 0 and 1 where you want the animation to start playing from. If your animation is 4 seconds long and you want to start 1 second in, then ‘percentAtWhichToStart’ should be 0.25f.
 
Now, this is just what worked best for my situation, because I wasn’t using this script for a character, So I couldn’t just use speed variables and triggers. This simple code is good for picking and choosing and manipulating specific animations.
 
Whether or not you use this now, I hope it helps at some time in the future.

  • Liked by
Reply
Cancel