- Home /
Question by
MagneticMincer · Jul 27, 2015 at 10:41 AM ·
animationcharacter controlleranimation controller
Character Doesn't Animate Properly When Stopped
My character has two animation sequences, idle and running. Whenever I transition into the running sequence my character doesn't return to the idle position. The running sequence is suppose to change to idle when "speed" is less than 0.01. Thanks!!
using UnityEngine;
using System.Collections;
public class CharacterController : MonoBehaviour {
//maximum player speed
public float maxSpeed = 10f;
//facing right is true
bool directionRight = true;
//reference to animator
Animator anim;
void Start () {
//gets the animator
anim = GetComponent<Animator>();
}
//don't need time.deltaTime in FixedUpdate
void FixedUpdate () {
//how much player is moving
float move = Input.GetAxis ("Horizontal");
//controls changes in animation & sets speed
anim.SetFloat("speed", Mathf.Abs(move));
//moves the player left/right based on maxSpeed
GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
//when moving left and not facing left, flip. when moving right and not facing right, flip.
if(move > 0 && !directionRight)
Turn ();
else if(move < 0 && directionRight)
Turn ();
}
void Turn () {
//facing right is not true
directionRight = !directionRight;
//get the localScale
Vector3 theScale = transform.localScale;
//flip the x axis
theScale.x *= -1;
//apply theScale back to localScale
transform.localScale = theScale;
}
}
screen-shot-2015-07-26-at-54353-pm.png
(27.4 kB)
Comment