- Home /
Could some wise person please help with this animation problem
Hi there and thanks for looking. The problem i am having is i want this script to play like this
- when i dont press anything it animates "idle"
- when i press "a" or "d" it animtes and loops "run"
- Whe i let go it stops looping "run" and Animates and loops idle.
Here is my code below also could someone please tell me how to format my code for these forums
function Awake() { animation["Idle"].layer = 0; animation["Run"].layer = 1; animation["Jump"].layer = 2; }
var wrapMode : WrapMode;
function Update() { if (Input.GetKey("d")) { animation["Run"].layer = 1; animation["Run"].wrapMode = wrapMode.Loop; } else if (Input.GetKeyUp("d")) { animation["Idle"].layer = 0; animation["Idle"].wrapMode = wrapMode.Loop; } else if (Input.GetKey("a")) { animation["Run"].layer = 1; animation["Run"].wrapMode = wrapMode.Loop; } else if (Input.GetKeyUp("a")){ animation["Idle"].layer = 0; animation["Idle"].wrapMode = wrapMode.Loop; }
if (Input.GetButtonDown("Jump")) { animation.CrossFade("Jump"); } else{ animation.CrossFade("Idle"); } }
Hit the 'edit' link, select what you want formatted as code, hit the 010/101 button along the top of the editing window.
Answer by ShadowBroker · May 11, 2011 at 02:00 PM
The problem is, that Run is on layer 1 and idle on 0, so Run beats Idle. You also don't have to set the layers and wrapModes always again. Once set, it keeps until the game is stopped. Also, you could try to stop the run animation if the key is lowered. I think of something in a way like this:
if (Input.GetKeyUp("a")){
animation.Stop("Run");
}
This should be everything you need.
Your answer
Follow this Question
Related Questions
Won't loop animation (solved!) 2 Answers
how to stop animation loop? 1 Answer
Unity does not recognize reload animation for gun 1 Answer
Add animation curve through script 2 Answers
Unity3D AI using animations 1 Answer