- Home /
I try'd so hard, but my animation script doesnt work..?
**I'm a newbie with scripting.. But I tried my best to get my animation's working on the right time.. I wanted to
play my run animation when left shift is pressed and we were not walking or idle
to play my idle when you are not running / walking
to play my walk animation when were not running or idle
This may be a dumb question, but this script get this error: Expressions in statements must only be executed for their side-effects. Here is the script:**
#pragma strict
var idle : boolean = false;
var walking : boolean = false;
var running : boolean = false;
function Update () {
//make the idle animation play when you are not walking or running
if(walking == false && running == false)
{
animation.CrossFade('Idle');
idle == true;
running == false;
walking == false;
}
//Make the walk animation play when you are not Idle or running
if(Input.GetKey(KeyCode.W))
{
animation.CrossFade('Walk');
walking == true;
running == false;
idle == false;
}
//Make the run animation play when you are not Idle or walking
if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W))
{
animation.CrossFade('Run');
running == true;
idle == false;
walking == false;
}
//Make the walk animation stop when the W key is released
if(Input.GetKeyUp(KeyCode.W))
{
animation.Stop('Walk');
walking == false;
running == false;
idle == true;
}
//Make the run animation stop when the left-shift key is released.
if(Input.GetKeyUp(KeyCode.LeftShift))
{
animation.Stop('Run');
running == false;
}
//Make the run or walk animation stop when you are not running or walking
if (Input.GetKeyUp(KeyCode.LeftShift) && Input.GetKeyUp(KeyCode.W))
{
animation.CrossFade('Idle');
running == false;
idle == true;
walking == false;
}
}
Some-one who can help me get this working? :(
Answer by ZenithCode · Aug 26, 2013 at 07:08 PM
Use == only as a relational operator (Check if they are equal).
For the rest use a single = sign.
E.g:
if(walking == false && running == false)
{
animation.CrossFade('Idle');
idle = true;
running = false;
walking = false;
}
$$anonymous$$y animation doesn't play and the booleans don't go back to idle?
#pragma strict
var walking : boolean = false;
var idle : boolean = false;
var running : boolean = false;
function Update () {
//if W is pressed, change booleans.
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.W))
{
walking = true;
running = false;
idle = false;
}
//if W and LeftShift are pressed, change booleans
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.W) && Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift))
{
walking = false;
running = true;
idle = false;
//if W is released, change booleans
if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.W))
{
walking = false;
running = false;
idle = true;
}
//if statement to play Idle animation.
if(walking == false && running == false && idle == true)
{
animation.Play('Idle');
Debug.Log('you have no movement');
}
//if statement to play run animation.
if(walking == false && idle == false & running == true)
{
animation.Play('Run');
Debug.Log('you are running');
}
//if statement to play walk animation.
if(walking == true && idle == false && running == false)
{
animation.Play('Walk');
Debug.Log('you are walking');
}
}
}
using UnityEngine;
using System.Collections;
public class AnimationController : $$anonymous$$onoBehaviour
{
private enum AnimationStates
{
idle,
walk,
run
}
private AnimationStates _animationState;
void Update ()
{
//if W is pressed, walk
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.W)) {
_animationState = AnimationStates.walk;
}
//if W and LeftShift are pressed, go back to idle
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.W) && Input.Get$$anonymous$$ey ($$anonymous$$eyCode.LeftShift)) {
_animationState = AnimationStates.idle;
}
//if W is released, change booleans
if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.W)) {
_animationState = AnimationStates.idle;
}
switch (_animationState) {
case AnimationStates.idle:
{
Debug.Log ("Idle");
animation.Play ("Idle");
break;
}
case AnimationStates.walk:
{
Debug.Log ("Walk");
animation.Play ("Walk");
break;
}
case AnimationStates.run:
{
Debug.Log ("Run");
animation.Play ("Run");
break;
}
}
}
}
Your answer
Follow this Question
Related Questions
Animated Texture Offset 1 Answer
How do I add NPCs? 2 Answers
i need a help in script to move while he is animation? 2 Answers
How to click a 3d object in unity3d? 3 Answers
Stop animation on the time given then start animation when press a key 0 Answers