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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Very stuttery movement

Discussion in 'Scripting' started by lanceslances, Sep 7, 2021.

  1. lanceslances

    lanceslances

    Joined:
    Aug 9, 2021
    Posts:
    10
    Hi, I'm trying to make a gun that currently moves around the player when the player rotates. It works currently but is very buggy and moves back and forth extremely snappy. Why is this happening?

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class GunController : MonoBehaviour
    4. {
    5.     [Header("Objects")]
    6.     public Transform camera;
    7.  
    8.     [Header("Gun Settings")]
    9.     //Aiming
    10.     public Vector3 normalLocalPosition;
    11.     public Vector3 aimingLocalPosition;
    12.     public Vector3 weaponDistanceAmount;
    13.  
    14.     public float aimSmoothing = 10;
    15.  
    16.     [Header("Mouse Settings")]
    17.     public float mouseSensitivity = 1;
    18.     private Vector2 _currentRotation;
    19.     public float weaponSwayAmount = 10;
    20.  
    21.     private void Update()
    22.     {
    23.         DetermineRotation();
    24.     }
    25.  
    26.     private void DetermineRotation()
    27.     {
    28.         Vector2 mouseAxis = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
    29.  
    30.         mouseAxis *= mouseSensitivity;
    31.         _currentRotation += mouseAxis;
    32.  
    33.         _currentRotation.y = Mathf.Clamp(_currentRotation.y, -90, 90);
    34.  
    35.         transform.localPosition = (Vector3)mouseAxis + weaponDistanceAmount;
    36.  
    37.         transform.root.localRotation = Quaternion.AngleAxis(_currentRotation.x, Vector3.up);
    38.         camera.localRotation = Quaternion.AngleAxis(-_currentRotation.y, Vector3.right);
    39.  
    40.     }
    41. }
     
  2. On-Stand-By

    On-Stand-By

    Joined:
    Sep 30, 2017
    Posts:
    14
    You should use Time.deltaTime to smooth movements.
    Depending on how you game is running, Update() is called multiple items per seconds. If it's running at 60 fps then it's 60 calls to Update() par second so there's ~ 0.016 second between each call. So a movement of 10 degrees per frame would quickly make you spinning really fast.

    If you multiply mouseAxis by Time.deltaTime, speed will be easier to adjust because you can think about it in a more humain understandable way : If you want your object to rotate at a speed of 90 degrees per second you should add 90 * Time.deltaTime to it's current rotation, giving you a smooth and constant movement.
    _currentRotation += mouseAxis * Time.deltaTime;


    I would also recommend to reorganize your gameobject hierarchy like this :

    You can use Player transform to change position, camera to change rotation and you don't need to rotate the Gun because it will follow the camera's rotation. All you need to do is to place correctly the gun relatively to the camera and to rotate the camera.
     
    lanceslances likes this.
  3. lanceslances

    lanceslances

    Joined:
    Aug 9, 2021
    Posts:
    10
    Thanks! I don't believe that I forgot to do that.