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 An issue in 2D airplanes where resting/right turning does not (well!) express emotion at the same t

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

  1. WENKO

    WENKO

    Joined:
    Apr 27, 2019
    Posts:
    53
    On the X-Z plane play screen, the player is in order to implement tilting left and right based on the Z-axis, which is the forward direction, and rotating around the Y-axis by pressing the left(KEY A) and right(KEY S).

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

    To advance the player in the Y-axis direction, the Fixedupdate() part was done as follows.

    Code (CSharp):
    1.     void FixedUpdate()
    2.     {
    3.         if (rb != null)
    4.         {
    5.             rb.velocity = transform.forward * speed * Time.deltaTime;
    6.         }
    7.     }



    When I press KEY A
    upload_2024-5-26_17-40-42.png

    When I release the KEY A
    upload_2024-5-26_17-43-13.png

    When I press KEY S
    upload_2024-5-26_17-44-42.png

    When I release the KEY A
    upload_2024-5-26_17-46-4.png

    It moves like this

    1. After turning left-right, the player's direction is restored to the Z-axis forward direction.
    2. When turning left-right and tilting left or right based on the Z-axis, the Y-axis height change


    Please help~
     

    Attached Files:

    Last edited: May 26, 2024
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,369
    Steps to success on this are to treat the plane's yaw (rotation around the Y axis) separately from its banking (local rotation around the Z axis).

    Set your plane prefab up to be useful to you:

    MyAirplanePrefabThatRotatesAroundY <---- put all scripts here
    VisiblePortionThatCanTiltAroundLocalZAxis


    Then select a bank angle based on input: straight: 0, left -20, right +20 (for example)

    And use that bank angle on the local Z axis.

    To make it smooth, do something like this:

    Smoothing movement between any two particular values:

    https://forum.unity.com/threads/beginner-need-help-with-smoothdamp.988959/#post-6430100

    You have currentQuantity and desiredQuantity.
    - only set desiredQuantity
    - the code always moves currentQuantity towards desiredQuantity
    - read currentQuantity for the smoothed value

    Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

    The code: https://gist.github.com/kurtdekker/fb3c33ec6911a1d9bfcb23e9f62adac4