Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Character moving faster in a diagonal direction

Discussion in 'Scripting' started by Rayzr, Dec 12, 2020.

  1. Rayzr

    Rayzr

    Joined:
    Dec 12, 2020
    Posts:
    3
    Hey guys,

    So I've seen so many threads regarding the same issue but I'm unable to find any solution which is specific to my script. I can run forward, backwards, left & right completely fine, but my character decides to turn into Usain Bolt as soon as I want to move diagonally.

    All help is appreciated.


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class ThirdPersonCharacterController : MonoBehaviour
    {
    public float speed;
    Animator anim;
    void Start()
    {
    anim = GetComponent<Animator>();
    }
    void Update()
    {
    // Forward
    if (Input.GetKey(KeyCode.W))
    {
    PlayerMovement();
    anim.SetInteger ("condition", 1);
    }
    if (Input.GetKeyUp(KeyCode.W))
    {
    anim.SetInteger("condition", 0);
    }
    if (Input.GetKey(KeyCode.W) && (Input.GetKey(KeyCode.LeftShift)))
    {
    speed = 4;
    anim.SetInteger("condition", 2);
    }
    if (Input.GetKeyUp(KeyCode.W) && (Input.GetKeyUp(KeyCode.LeftShift)))
    {
    speed = 3;
    anim.SetInteger("condition", 0);
    }
    // Backwards
    if (Input.GetKey(KeyCode.S))
    {
    PlayerMovement();
    anim.SetInteger("condition", 3);
    }
    if (Input.GetKeyUp(KeyCode.S))
    {
    anim.SetInteger("condition", 0);
    }
    if (Input.GetKey(KeyCode.S) && (Input.GetKey(KeyCode.LeftShift)))
    {
    speed = 4;
    anim.SetInteger("condition", 4);
    }
    if (Input.GetKeyUp(KeyCode.S) && (Input.GetKeyUp(KeyCode.LeftShift)))
    {
    speed = 3;
    anim.SetInteger("condition", 0);
    }
    // Left
    if(Input.GetKey(KeyCode.A))
    {
    PlayerMovement();
    speed = 3;
    anim.SetInteger("condition", 0);
    }
    if (Input.GetKeyUp(KeyCode.A))
    {
    anim.SetInteger("condition", 0);
    }
    // Right
    if(Input.GetKey(KeyCode.D))
    {
    PlayerMovement();
    speed = 3;
    anim.SetInteger("condition", 0);
    }
    if (Input.GetKeyUp(KeyCode.D))
    {
    anim.SetInteger("condition", 0);
    }
    }
    void PlayerMovement()
    {
    float hor = (Input.GetAxisRaw("Horizontal"));
    float ver = (Input.GetAxisRaw("Vertical"));
    Vector3 playerMovement = new Vector3(hor, 0f, ver).normalized * speed * Time.deltaTime;
    transform.Translate(playerMovement, Space.Self);
    }
    }

     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    Your Update function is calling PlayerMovement individually when any of your directional keys are pressed -- if you press more than one key at a time, you're calling PlayerMovement more than once per frame -- doubling your speed. If you pressed all four keys at once, you're calling PlayerMovement four times per frame. Only the fact that opposite keys cancel out your axis would save you from becoming Sonic the Hedgehog.

    You don't need to gate PlayerMovement behind a key press, since if you're not pressing anything
    hor
    and
    ver
    will both be zero and your player won't go anywhere. Calling PlayerMovement once at the end of your Update loop, or getting rid of the method altogether and sticking the logic in Update will fix your speed problem.
     
    Rayzr likes this.
  3. Rayzr

    Rayzr

    Joined:
    Dec 12, 2020
    Posts:
    3
    Ahh that's very helpful, as you can tell I'm new to C# but this has helped me out alot. Appreciate this!
     
  4. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    If you ever do want to make the values for the diagonal match the horiz/vert, the magic number to multiply by is half of the square root of 2, or 0.707. But there are other ways to do it as well.