- Home /
Question by
Dthobbs · Apr 07, 2016 at 05:37 PM ·
javascriptscripting problemanimatorcharactercontroller
2d Character runs in place via animator
Ok I'm using the animator to make my 2d sprite to look like hes running on a key press. I FINALLY got him to look like hes running (transition between idle and moving) but now I have the problem of him not going back to the idle animation. I'm not sure if theres a problem with my code, or if theres a setting I need to apply in the animator (exit time and fixed duration are unchecked). Any advice???
#pragma strict
public var maxJumps = 0; // maximum numer of jumps
private var numJumps = 0; //number of current jumps
public var moveSpeed = 0;
private var facingRight = true; //initally sprite faces to the right
private var isWalking : boolean;
public var jumpheight = 0;
private var anim : Animator;
function Start() {
anim = GetComponent(Animator);
}
function Update () {
var x:float;
var y:float;
if (Input.GetKeyDown("space") && CanJump()){
x = GetComponent(Rigidbody2D).velocity.x;
GetComponent(Rigidbody2D).velocity = new Vector2(0, jumpheight);
numJumps ++;
}
if (Input.GetKey (KeyCode.A))
{
x = GetComponent(Rigidbody2D).velocity.x;
y = GetComponent(Rigidbody2D).velocity.y;
GetComponent(Rigidbody2D).velocity = new Vector2(-moveSpeed, y);
anim.SetFloat("Speed", Mathf.Abs(x));
if (facingRight) {
Flip();
}
}
if (Input.GetKey (KeyCode.D)) {
x = GetComponent(Rigidbody2D).velocity.x;
y = GetComponent(Rigidbody2D).velocity.y;
GetComponent(Rigidbody2D).velocity = new Vector2(moveSpeed, y);
anim.SetFloat("Speed", Mathf.Abs(x));
if (!facingRight) {
Flip();
}
}
}
function OnCollisionEnter2D (coll : Collision2D)
{
if (coll.gameObject.CompareTag("Ground")) {
numJumps = 0;
}
}
function CanJump() {
return numJumps < maxJumps;
}
function Flip() {
var flipScale : Vector3;
var rigidbody : Rigidbody2D;
rigidbody = GetComponent(Rigidbody2D);
flipScale = rigidbody.transform.localScale;
flipScale.x *= -1; //flip horizontally
rigidbody.transform.localScale = flipScale;
facingRight = !facingRight; // now facing opposite direction
}
Comment
And a screener of your statemachine? Got all your transitions?