Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Object moves faster going in the diagonal

I made a little player script by myself to make my player move, but I wanted the speed to be cut in half when he is moving in the diagonal because it has double the value of whenever he is going in a straight line. How would that be possible?

void Update()
    {
        Vector3 direction = new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime );
        gameObject.transform.position = gameObject.transform.position + direction;  
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

but I wanted the speed to be cut in half when he is moving in the diagonal because it has double the value of whenever he is going in a straight line

No, actually it will be up to times √(2) 😉

You want to be using Vector3.ClampMagnitude in order to always have a maximum magnitude of 1f for your input:

void Update()
{
    var input = new Vector3(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
    gameObject.transform.position += Vector3.ClampMagnitude(direction, 1f) * moveSpeed * Time.deltaTime;  
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading