Hello everyone, I’m pretty frustrated right now because I can’t find any way to do this properly. I looked everywhere and saw many tutorials on how would I play certain animations when I move left and right but they all don’t work for me for some reason. I went ahead and made a script and I solved the movement and direction problem but I just can’t get the animations to play when I want them to. Example is when I press “D” I want the running animation to play while I move right but only when I hold it down. So If I just press it I just want it to play through the first few frames of the running animation and when held down it would keep cycling through.
So in conclusion, I don’t know the proper way to do this and I’ve tried many ways which usually end up just cutting the running animation off and playing the default animation. I am using the animator and animator controller as well but I just can’t solve this problem. Here is my script I’m using which is placed onto my character who is controlled by an empty object so I can move him anywhere.
Also I’m sorry if the code is poor, I’m not too big of a programmer person and I’m doing this just to build a simple prototype. I have 6 animations in total but I just want the running the play at least when I move left or right.
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float speed = 1f;
public float someScale;
public float dir;
Animator anim;
// Use this for initialization
void Start () {
someScale = transform.localScale.x;
dir = 1;
anim = GetComponent<Animator>();
anim.SetBool ("Run", false);
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown (KeyCode.D))
{
anim.SetBool ("isMoving", true);
transform.position += new Vector3(speed * Time.deltaTime, 0.0f, 0.0f);
//Change direction facing
if(dir == 1)
{
transform.localScale = new Vector3(-someScale, transform.localScale.y, transform.localScale.z);
dir = -1;
}
}
if(Input.GetKeyDown (KeyCode.A))
{
transform.position -= new Vector3(speed * Time.deltaTime, 0.0f, 0.0f);
if(dir == -1)
{
transform.localScale = new Vector3(someScale, transform.localScale.y, transform.localScale.z);
dir = 1;
}
}
}
}