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

Question Head bobbing is faster diagonally

Discussion in 'Scripting' started by SoMir1, Aug 30, 2020.

  1. SoMir1

    SoMir1

    Joined:
    Jan 8, 2020
    Posts:
    2
    I am making a game for a game jam and I'm writing my own FPS controller, however, the movement was faster diagonally because I am using Horizontal and Vertical axis. I fixed it using Vector3.ClampMagnitude, but my head bobbing script on the camera still bobbs faster when I move diagonally. How can I fix this? Here is my script:

    Code (CSharp):
    1. private float timer = 0.0f;
    2.     public float bobbingSpeed = 0.18f;
    3.     public float bobbingAmount = 0.2f;
    4.     public float midpoint = 2.0f;
    5.     private bool bPos = true;
    6.  
    7.     public AudioSource audioSource;
    8.  
    9.     public AudioClip[] stepSounds;
    10.  
    11.     public PlayerMovement playerMovement;
    12.  
    13.     void Start()
    14.     {
    15.         if(playerMovement == null)
    16.             playerMovement = transform.parent.GetComponent<PlayerMovement>();
    17.         if(audioSource == null)
    18.             audioSource = GetComponent<AudioSource>();
    19.     }
    20.  
    21.     void Update ()
    22.     {
    23.         if(playerMovement.isGrounded == false)
    24.             return;
    25.         float waveslice = 0.0f;
    26.         float horizontal = Input.GetAxis("Horizontal");
    27.         float vertical = Input.GetAxis("Vertical");
    28.         float bobbingFactor = horizontal * horizontal + vertical * vertical;
    29.  
    30.         if(Mathf.Abs(horizontal) == 0 && Mathf.Abs(vertical) == 0)
    31.         {
    32.             timer = 0.0f;
    33.         }
    34.         else
    35.         {
    36.             waveslice = Mathf.Sin(timer);
    37.  
    38.             float footfall = Mathf.Cos(timer);
    39.             if(footfall < 0 && bPos)
    40.             {
    41.                 audioSource.clip = stepSounds[Random.Range(0, stepSounds.Length)];
    42.                 audioSource.Play();
    43.             }
    44.  
    45.             bPos = footfall >= 0;
    46.  
    47.             timer = timer + bobbingSpeed * bobbingFactor * Time.deltaTime;
    48.             if (timer > Mathf.PI * 2)
    49.             {
    50.                 timer = timer - (Mathf.PI * 2);
    51.             }
    52.            
    53.         }
    54.         if(waveslice != 0)
    55.         {
    56.             float translateChange = waveslice * bobbingAmount;
    57.             float totalAxes = Mathf.Abs(horizontal) + Mathf.Abs(vertical);
    58.             totalAxes = Mathf.Clamp(totalAxes, 0.0f, 1.0f);
    59.             translateChange = totalAxes * translateChange;
    60.             Vector3 temp = Vector3.zero;
    61.             temp.y = transform.localPosition.y;
    62.             temp.y = midpoint + translateChange;
    63.             transform.localPosition = temp;
    64.         }
    65.         else
    66.         {
    67.             Vector3 temp = Vector3.zero;
    68.             temp.y = transform.localPosition.y;
    69.             temp.y = midpoint;
    70.             transform.localPosition = temp;
    71.         }
    72.     }
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
  3. SoMir1

    SoMir1

    Joined:
    Jan 8, 2020
    Posts:
    2