Simultaneously moving using two inputs W D or Up LEFT to move diagonally
Hi, this is my current script that may be way out to lunch and there may possibly be an easier way to achieve what I'm trying to do. I've been able to make him move in all directions with the animation working for his "Walk" state and back to "Idle".
My current issue is I'm unable for the life of me figure out how to rotate the local scale to turn him around and face the other direction I.E Facing right while pressing D W or Right UP arrow, then face left while pressing A W or Left UP arrow using GetAxisRaw.
If anyone has any suggestion to make this a little easier please do. I will be adding more animations down the road for walking straight down or up, I can see this heading to a problem because of the GetAxisRaw and already playing the Walk animation for all directions. I feel there is an easier solution to this but cannot figure it out.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float moveSpeed;
private Animator anim;
void Awake() {
anim = GetComponent<Animator> ();
}
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f )
{
transform.Translate (new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f ));
anim.SetBool ("Walk", true);
} else if (Input.GetAxisRaw("Horizontal") == 0.0f || Input.GetAxisRaw("Horizontal") == -0.0f )
anim.SetBool ("Walk", false);
if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
{
transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
anim.SetBool ("Walk", true);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612113948im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Player Controller, The character don't move when I insert my animations 0 Answers
Velocity in Player Movement Problem? 0 Answers
Unity Space Shooter Tutorial Player Will Not Move 1 Answer
Hit a wall in my RTS movement controller. Issues with MoveToward and Coroutine logic. 0 Answers
Rigidbody2D movement is lagging 0 Answers