- Home /
Animate Character movement?
Hello. So I made a simple animation and attached it to the main character. I was hoping that as I push down the button that makes the character move right that the animation will play while holding the button down, either I didn't do it correctly or it wasn't correct in the first place. How can I do this?
( For easy clarification )
I want a specific set animation to play while holding a button down with or without this script
using UnityEngine;
using System.Collections;
public class Move2 : MonoBehaviour
{
public float speed = 5f;
private bool mouseDown = false;
public void OnMouseDown ()
{
mouseDown = true;
}
public void OnMouseUp()
{
mouseDown = false;
}
public void Update()
{
if(mouseDown){
transform.position -= Vector3.left * speed * Time.deltaTime;
}
}
}
Answer by GiyomuGames · Aug 21, 2015 at 01:11 AM
You need to add a new parameter in the animator window which will start the animation. http://docs.unity3d.com/Manual/AnimationParameters.html
It could be a boolean called "Moving", which you set to True in a script when the mouse button is down. It doesn't have to be the script where you handle the movement. In your animator window you'll need to create a transition between the initial state and your "Move" animation which is triggered when Moving is true (and the opposite transition when Moving is false).
Or you could have a float parameter called "hSpeed" (for Horizontal Speed) into which you put the horizontal speed of your character. Then you can create a blend tree bend your idle animation and your moving animation, depending on hSpeed.
If all the above sounds like gibberish to you then please check a couple tutorials about animations in Unity ^^