Search Unity

Question How to handle smooth rotating?

Discussion in 'Scripting' started by ShimmyDev, Aug 5, 2020.

  1. ShimmyDev

    ShimmyDev

    Joined:
    Nov 21, 2016
    Posts:
    11
    Hey Guys.
    I've got a problem with rotation. It looks like the whole objects hitting some kind of barrier.

    It looks like this:


    And here's the code:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.     [SerializeField] private float _speed = 4f;
    9.     [SerializeField] private float _smooth = 1f;
    10.  
    11.    
    12.     [SerializeField] private GameObject _Player;
    13.  
    14.     public Quaternion newRotation;
    15.     public Vector3 newPosition;
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         newPosition = transform.position;
    21.         newRotation = transform.rotation;
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         MovementController();
    28.     }
    29.  
    30.     void MovementController()
    31.     {
    32.  
    33.         if (Input.GetKey(KeyCode.W))
    34.         {
    35.             newPosition += (transform.forward * _smooth);
    36.         }
    37.         if (Input.GetKey(KeyCode.S))
    38.         {
    39.             newPosition += (transform.forward * -_smooth);
    40.         }
    41.         if (Input.GetKey(KeyCode.D))
    42.         {
    43.             newPosition += (transform.right * _smooth);
    44.         }
    45.         if (Input.GetKey(KeyCode.A))
    46.         {
    47.             newPosition += (transform.right * -_smooth);
    48.         }
    49.  
    50.         if (Input.GetKey(KeyCode.Q))
    51.         {
    52.             newRotation *= Quaternion.Euler(Vector3.up * _speed);
    53.  
    54.         }
    55.         else if (Input.GetKey(KeyCode.E))
    56.         {
    57.             newRotation *= Quaternion.Euler(Vector3.down * _speed);
    58.         }
    59.  
    60.         transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime * _smooth);
    61.         transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * _smooth);
    62.  
    63.     }
    64.  
    65.  
    66. }
    67.  
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    It looks to me that the issue is due to you adding to the newRotation too quickly. You are adding a set amount each, regardless of how long that frame took yet you are limiting the amount the object can rotate each frame dependant on the framerate.

    To put it in numbers say the object starts at an angle of 0 degrees, you press the button and a few frames later target rotation is set to 90 degrees, the object starts turning right a limited amount, a few frames later when the object still hasn't reached 90 degrees, the button has been held down and the target rotation is now 360 degrees, so the lerp then starts turning the object left towards it due to the lerp.

    What I would do, is multiple your added rotation by Time.deltaTime so the amount being added is dependant on frame times and then do away with the lerp.
     
    ShimmyDev likes this.
  3. ShimmyDev

    ShimmyDev

    Joined:
    Nov 21, 2016
    Posts:
    11