How can I make my character walk while pressing arrow keys?
Hi, so I made my character and a few animations to go along with it using Adobe Fuse and Mixamo. I'm having a few problems and after hours of searching for resolutions, I'm starting to go bananas! (literally)
Ok so enough sob story, let me tell you about my setup. I have an Animator Controller(AC) attached to my character GameObject and inside the AC there are two animations, an Idle which transitions from the entry, and a Walking animation. The idle seems to be working fine, but the Walking is where I get my problems. I want the player to be able to press the arrow keys and for the Walking animation to play while the key is being held down and once its released I want the animation to switch back to Idle. I currently have a script that controls my character movement but when I press the keys the Walking animation only play once, not repeatedly and it doesn't go back to Idle.
Here is my c# script that's attached to my character:
public class Harpermovement : MonoBehaviour {
public float speed = 2.0f;
public Animator anim;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
anim = GetComponent<Animator>();
if (Input.GetKey(KeyCode.RightArrow))
{
anim.Play("Walking");
transform.position += Vector3.right * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
anim.Play("Walking");
transform.position += Vector3.left * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.UpArrow))
{
anim.Play("Walking");
transform.position += Vector3.forward * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.DownArrow))
{
anim.Play("Walking");
transform.position += Vector3.back * speed * Time.deltaTime;
}
}
}
Also, if anyone knows how to switch the position my character is facing when I press the left and right arrow keys to walk that would be awesome. Currently when I walk her to the side her feet are still walking forward but she's moving sideways :/
If its 3d game check out this http://unity3d.com/learn/tutorials/modules/intermediate/live-training-archive/setting-up-a-humanoid-avatar?playlist=17099 should answer all you need to know. After 30 $$anonymous$$ it gets on how to configure your animation and after how to control them.
Answer by ComradeVanti · Mar 29, 2016 at 11:11 AM
Ok so I will just assume you are making a 2D game for this answer, since you didnt provide any information.
First of all: This script is very very inefficient. It would be much smarter to try something like this:
using UnityEngine;
using System.Collections;
public class CharacterMover : MonoBehaviour
{
public float speed;
void Update ()
{
transform.Translate (Input.GetAxisRaw ("Horizontal") * speed, Input.GetAxisRaw ("Vertical") * speed, 0);
}
}
Just assign "speed" and you should be good.
Now. How to change your animation? How about this:
Make animations for walking left, walking up, and walking down. Then controll these animations through bools and triggers.
If you are not familiar with them I would suggest you watch this tutorial:
http://unity3d.com/learn/tutorials/modules/beginner/animation/animator-scripting
Any more questions? Comment them :)
Thanks for your response, I actually got it to work and the character is moving according to her axis :)! Also, the animation is finally playing whenever I click the corresponding arrow keys. So now I'm on to the next step which Is trying to add buttons to my UI that each trigger an animation, for example, I have a kick button that is supposed to trigger the kick animation in my controller when the user clicks it. ($$anonymous$$y game is 3d, just too clarify) So far I've managed to get the buttons onto the UI and I added an onClick event to the button that calls the kick animation, but the problem is is that my character doesn't do the animation and it only plays once. (I want it to play whenever the user clicks).
Sorry if this is a lot, but any help is greatly appreciated!
Answer by bowserscastle · Feb 22, 2018 at 12:56 AM
I would add one thing that you're missing and the other answer doesn't mention: don't forget to include Time.deltaTime
when doing your movement calculations. Movement should be based on time elapsed, not on frames, and since Update()
is called with every frame, it's very likely that if you move the character a certain distance every frame, instead of every X.x seconds, you won't get a consistent result across different devices. Time.deltaTime
will give you the amount of time elapsed since the last frame was displayed, ie: the delta of time [elapsed].
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementController : MonoBehaviour
{
public float movementSpeed = 50.0f; // The number of units you want to move
Vector2 movement = new Vector2();
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
Vector2 newPosition = new Vector2(movement.x * movementSpeed * Time.deltaTime, movement.y * movementSpeed * Time.deltaTime);
transform.Translate(newPosition);
}
}