- Home /
Problem with animation script for npc
Hello - in my game I need an npc to walk forward for 10 seconds, turn around and then stand idle.
This is the script I have, but the problem is that although the movement forward and turning is correct - he only ever stands in the 'idle' animation, and never 'walk forward'. (I have added them in correctly in the hierarchy)
Do you know if there's anything I'm doing wrong in the script? (JavaScript)
#pragma strict
public var placeWalkHere : AnimationClip;
public var placeIdleHere : AnimationClip;
private var walkTime : float = 10;
private var walkSpeed : float = .01f;
private var rotationTime : float = 3;
private var rotationSpeed : float = 1.2;
function Start () {
}
function Update () {
walkTime -= Time.deltaTime;
if(walkTime > .1f)
{
transform.Translate(0, 0, walkSpeed);
animation.Play(placeWalkHere.name);
}
if(walkTime < .1f)
{
rotationTime -= Time.deltaTime;
transform.Rotate(0, rotationSpeed, 0);
}
if (rotationTime < .1f)
{
rotationSpeed = 0;
animation.Play(placeIdleHere.name);
}
}
Thanks, Laurien
Answer by TonyLi · Jun 04, 2013 at 02:57 PM
Is there something wrong with my animator controller do you think? Or is the script not referencing it properly?
Use the Animator component, not animation.
The animation component [e.g., animation.Play()] is for the Legacy animation system.
Since you're using an animator controller (the newer Mecanim animation system), you need to use the Animator component to control it. Instead of using Play(), you'll need to trigger a transition from your Idle state to your Walk state. I recommend creating a parameter called Speed (or WalkSpeed). When Speed is greater than, say, 0.1, transition from Idle to Walk. When Speed is less than 0.1, transition from Walk to Idle.
In place of Start(), add this:
private var animator : Animator;
function Start() {
animator = GetComponent(Animator);
}
Then replace these lines:
animation.Play(placeWalkHere.name);
animation.Play(placeIdleHere.name);
With these:
animator.SetFloat("Speed", 1); //Walk
animator.SetFloat("Speed", 0); //Idle
BRILLIANT thanks so much!! I didn't realise about the two different systems! Thanks :)
Answer by Stormizin · Jun 04, 2013 at 12:00 PM
Dude i think this line is your problem:
private var walkSpeed : float = .01f;
Change to:
private var walkSpeed : float = 0.1f;
No - I've just changed that, but the character still stays in the idle animation. Is there something wrong with my animator controller do you think? Or is the script not referencing it properly?
Answer by SubatomicHero · Jun 04, 2013 at 01:41 PM
You could use a state machine into your update function to help you debug what state youre in like so:
// this script is in c# but you get the idea
// at the top do this
enum npcState {
Walking, Idle, Turning // You could add more if you wish
};
npcState myNpcState; // To access the enum
void Start(){
// setup your default state to start in
myNpcState = npcState.Walking;
}
void Update(){
Debug.Log("NPC State is " + myNpcState);
switch (myNpcState){
case npcState.Walking:
walkTime -= Time.deltaTime;
transform.Translate(0, 0, walkSpeed);
animation.Play(placeWalkNameHere.name);
if (walkTime < .01f) {
myNpcState = npcState.Turning;
}
break;
case npcState.Turning:
rotationTime -= Time.deltaTime;
transform.Rotate(0, rotationSpeed, 0);
walkTime = 0.0f;
if (rotationTime < .01f) {
myNpcState = npcState.Idle;
}
break;
case npcState.Idle:
rotationSpeed = 0;
animation.Play(placeIdleHere.name);
break;
}
}
Your answer
Follow this Question
Related Questions
How do I add NPCs? 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Play animation once 1 Answer
Play animation for horizontal and vertical movement? 1 Answer
Mixamo Call Animation from Script 0 Answers