Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Resolved My character keeps snapping back to its original rotation after I let go of the key.

Discussion in '2D' started by hudstem1, May 19, 2024.

  1. hudstem1

    hudstem1

    Joined:
    Jul 21, 2022
    Posts:
    2
    In my game you are a rocket and you try to orbit a planet. So when you press A or D you should rotate left or right. But when you let go of the key you snap back to your original rotation. I don't know how to fix this or what is wrong.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UIElements;
    5.  
    6.  
    7. public class RocketController : MonoBehaviour
    8. {
    9.     private Rigidbody2D rb;
    10.  
    11.     public float Thrust = 20;
    12.     public float rotation = 5;
    13.     public float rotationSpeed = 2;
    14.     private float speed = 15;
    15.     public float weight = 10;
    16.  
    17.    
    18.     private void Start()
    19.     {
    20.         rb = GetComponent<Rigidbody2D>();
    21.        
    22.     }
    23.     private void Update()
    24.     {
    25.         speed = Thrust / weight ;
    26.  
    27.         rb.AddForce(transform.up * speed );
    28.         rb.MoveRotation(rotation);
    29.  
    30.         if (Input.GetKey(KeyCode.A))
    31.         {
    32.             rb.MoveRotation(rb.rotation - rotationSpeed * Time.fixedDeltaTime);
    33.         }
    34.  
    35.         if (Input.GetKey(KeyCode.D))
    36.         {
    37.             rb.MoveRotation(rb.rotation + rotationSpeed * Time.fixedDeltaTime);
    38.         }
    39.     }
    40.  
    41.    
    42.  
    43.  
    44.     public void AdjustThrust(float newThrust)
    45.     {
    46.         Thrust = newThrust;
    47.     }
    48.     public void AdjustRotate(float newRotate)
    49.     {
    50.         rotation = newRotate;
    51.        
    52.     }
    53. }
     
  2. frasderp

    frasderp

    Joined:
    Oct 6, 2016
    Posts:
    22
    You have most of the code you need!

    This section in particular will help, but you are not using it.


    1. [*] public void AdjustRotate(float newRotate)
      [*] {
      [*] rotation = newRotate;
      [*] }
    So in your update function, rb.MoveRotation(rotation); is always setting rotation back to the original value of 5 (as the rotation float is never being updated.

    You could add AdustRotate(); to your input.getkey functions to update the rotation value, note that you will need to add the newly calculated rotation inside the brackets also.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
    Line 28 is always setting the rotation.

    You're also not updating the rotation, as @frasderp correctly notes above.

    Here's my reference 2D spaceship code:

    https://github.com/kurtdekker/proxi...buttons/Assets/Demo2DMovements/SpaceShip2D.cs

    Full setup in the project itself.

    proximity_buttons is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/proximity_buttons

    https://github.com/kurtdekker/proximity_buttons

    https://gitlab.com/kurtdekker/proximity_buttons

    https://sourceforge.net/projects/proximity-buttons/
     
  4. hudstem1

    hudstem1

    Joined:
    Jul 21, 2022
    Posts:
    2
    Thank You. Line 27 was the problem and its working now.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,761
    MovePosition and MoveRotation do not get actioned there and then, it only happens when the physics runs. Physics runs (by default) during the FixedUpdate so calling them per-frame is pointless. If you have 10 frames between each physics update, only the last call you made will be actioned so this is wasteful and can also lead to errors or false assumptions.

    MovePosition(PositionA)
    MovePosition(PositionB)

    Same as above, multiple calls don't all get actioned, only the last one before the simulation runs does so below, it'll move to PositionB if nothing else is called before the simulation runs.