My Character is moving fine horizontally and vertically, but moves way too fast diagonally

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {

    public float moveSpeed;

    private Animator anim;

    private bool PlayerMoving;

    private Vector2 LastMove;

    private Rigidbody2D myRigidBody;
    
    // Use this for initialization
	void Start () {
        myRigidBody = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
	}
	
	// Update is called once per frame
	void Update () {

        PlayerMoving = false;

        if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f )
        {
            // transform.Translate  (new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
            myRigidBody.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), myRigidBody.velocity.y) * moveSpeed;
            PlayerMoving = true;
            LastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
        }


        if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
        {
            // transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
            myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, Input.GetAxisRaw("Vertical")) * moveSpeed;
            PlayerMoving = true;
            LastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
        }

        if (Input.GetAxisRaw("Horizontal") < 0.5f && Input.GetAxisRaw("Horizontal") > -0.5f) 
        {
            myRigidBody.velocity = new Vector2(0f, myRigidBody.velocity.y);
                }

        if (Input.GetAxisRaw("Vertical") < 0.5f && Input.GetAxisRaw("Vertical") > -0.5f) 

        {
            myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, 0f);
        }
            anim.SetFloat("moveX", Input.GetAxisRaw("Horizontal"));
        anim.SetFloat("moveY", Input.GetAxisRaw("Vertical"));
        anim.SetFloat("LastMoveX", LastMove.x);
        anim.SetFloat("LastMoveY", LastMove.y);
        anim.SetBool("PlayerMoving", PlayerMoving);
    }
}

Use a temporal Vector3 to store the velocity. At the end of the Update, set the real velocity to: tmpVelocity.normalized * moveSpeed;

If you want direct control of the velocity every frame you should make the Rigidbody Kinematic and use Transform.Translate instead. Otherwise you should use Rigidbody2D.AddForce in FixedUpdate. As a sidenote you should cache the Hash for the animator parameters instead of using a string lookup every frame.

2 Answers

2

Use a temporal Vector3 to store the velocity. At the end of the Update, set the real velocity to:
tmpVelocity.normalized * moveSpeed;

You can do as rabirland said. The reason why you are now moving twice as fast is because you are basically applying two forces when you move diagonally and only 1 when you strictly move horizon or vertical. Two forces are being applied because of the if statements.

Not quite, normalizing a vector forces it's length to 1. For example, Normalize(0f, 0.00001f, 0) becomes (0, 1, 0). What you should use instead is Vector3.ClampMagnitude. This will clamp the length of the vector to a value (In this case, 1), but will not cause it to grow if it is very small.

Make a local Vector3 variable called “velocity”. instead of setting myRigidBody.velocity in the first two if statements, set the x and y values of velocity instead, and -don’t- multiply it by rotation speed.

Finally before setting myRigidBody.velocity, use Vector3.ClampMagnitude to clamp the total of the vector to 1.

Here’s an example, please note that I left out the other parts of the code as they are not related to the answer. Also I’m using Mathf.Abs for clarity, instead of two if statements, though this is not essential:

void Update()
{
	var velocity = new Vector3();

	if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0.5f)
	{
		velocity.x = Input.GetAxisRaw("Horizontal");
	}

	if (Mathf.Abs(Input.GetAxisRaw("Vertical")) > 0.5f)
	{
		velocity.y = Input.GetAxisRaw("Vertical");
	}

	// This is where the magic happens
	velocity = Vector3.ClampMagnitude(velocity, 1) * moveSpeed;
}