Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question After transform.Rotate() the Object forwarding is not changed, just reset originally. Help.

Discussion in 'Scripting' started by WENKO, May 26, 2024.

  1. WENKO

    WENKO

    Joined:
    Apr 27, 2019
    Posts:
    53
    Code (CSharp):
    1.                
    2. void Update()
    3. {
    4.                 float rotationDirection = _input.Player.Move.ReadValue<float>();
    5.                 transform.Rotate(Vector3.up * Time.deltaTime * 30 * rotationDirection)
    6. }
    7.  
    8. void FixedUpdate()    
    9. {        
    10.                  if (rb != null)        
    11.                  {            
    12.                                  rb.velocity = transform.forward * speed * Time.deltaTime;        
    13.                   }    
    14. }
    15. ;
    This code keeps the player going straight after turning.

    Code (CSharp):
    1.  
    2.  
    3. void Update()
    4. {
    5.                 float rotationDirection = _input.Player.Move.ReadValue<float>();
    6.                 transform.Rotate(Vector3.up * Time.deltaTime * 30 * rotationDirection);
    7.                 var smooth = 2.0f;
    8.                 var tiltAngle = 30.0f;
    9.  
    10.                 if (rotationDirection > 0)
    11.                 {
    12.                     rotationDirection -= tiltAngle;
    13.                 }
    14.                 else if (rotationDirection < 0)
    15.                 {
    16.                     rotationDirection += tiltAngle;
    17.                 }
    18.                 else
    19.                     rotationDirection += 0;
    20.  
    21.                 var target = Quaternion.Euler(0, 0, rotationDirection);.
    22.                 transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
    23. }

    However, if the player's tilt is added like this, the direction cannot be maintained after rotation and is immediately restored to the initial direction using 'FixedUpdate()'.

    Should I maintain forwarding after transform.Rotate() ?, please.