Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Moving object forward into direction of rotation

Discussion in 'Physics' started by TrickShotMLG, Oct 7, 2019.

  1. TrickShotMLG

    TrickShotMLG

    Joined:
    Jun 3, 2018
    Posts:
    28
    I am trying to create an airplane, but It doesn't move into the direction of the rotation. It always moves forward on x-Axis. How can I solve this?

    Here is my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AirCraftController : MonoBehaviour
    6. {
    7.     public Rigidbody rb;
    8.     public ParticleSystem Smoke;
    9.     public Collider WheelCollider;
    10.  
    11.     public bool isLanding;
    12.     public bool isGrounded = true;
    13.  
    14.  
    15.     public float increment;             //Increment/Decreament of thrust value
    16.     public float AccelerationMultiplier = 0.5f;
    17.     public float BrakeMultiplier = 0.5f;
    18.  
    19.     public float CurrentThrust;         //Current thrust
    20.     public int MaximumThrust;           //Maximum amount of thrust
    21.  
    22.     public float CurrentSpeed;          //Current speed
    23.     public float MaximumCurrentSpeed;   //Speedlimit with the current thrust
    24.     public int MaximumSpeed;            //Maximum speed
    25.  
    26.     public KeyCode Fwd;                 //Increment Key
    27.     public KeyCode Bwd;                 //Decrement Key
    28.     public KeyCode Up;                  //Up Key
    29.     public KeyCode Down;                //Down Key
    30.  
    31.  
    32.     // Start is called before the first frame update
    33.     void Start()
    34.     {
    35.  
    36.     }
    37.  
    38.     public void Landing()
    39.     {
    40.         Smoke.Play();
    41.         Debug.Log("Landed");
    42.     }
    43.  
    44.  
    45.     void OnCollisionEnter(Collision collision)
    46.     {
    47.         if(collision.collider == WheelCollider)
    48.         {
    49.  
    50.         }
    51.         else
    52.         {
    53.             if (isGrounded == false)
    54.             {
    55.                 Landing();
    56.             }
    57.             isGrounded = true;
    58.         }    
    59.     }
    60.  
    61.     private void OnCollisionExit(Collision collision)
    62.     {
    63.         isGrounded = false;
    64.     }
    65.  
    66.     // Update is called once per frame
    67.     void Update()
    68.     {
    69.         CurrentSpeed = rb.velocity.magnitude; // Get current Speed
    70.         MaximumCurrentSpeed = ((CurrentThrust / MaximumThrust) * MaximumSpeed) + 0.2f; //Calculate current speed by current thrust
    71.  
    72.         if (Input.GetKey(Fwd) == true && CurrentThrust <= (MaximumThrust - increment))
    73.         {
    74.             CurrentThrust += increment; //Increase current thrust
    75.         }
    76.  
    77.         if (Input.GetKey(Bwd) == true && CurrentThrust >= (0 + increment))
    78.         {
    79.             CurrentThrust -= increment; //Decrease current thrust
    80.         }
    81.  
    82.         if (Input.GetKey(Up) == true)
    83.         {
    84.          
    85.         }
    86.  
    87.         if (Input.GetKey(Down) == true)
    88.         {
    89.  
    90.         }
    91.  
    92.         if (rb.velocity.magnitude < MaximumCurrentSpeed)
    93.         {
    94.             // Acceleration to maximum amount of speed
    95.             rb.AddRelativeForce(transform.forward * CurrentThrust * AccelerationMultiplier, ForceMode.Impulse);
    96.         }
    97.         else if (MaximumCurrentSpeed < rb.velocity.magnitude)
    98.         {
    99.             // Deacceleration
    100.             rb.AddRelativeForce(transform.forward * ((MaximumThrust - CurrentThrust) * -1 * BrakeMultiplier), ForceMode.Impulse);
    101.         }
    102.  
    103.         if (isLanding)
    104.             Landing();
    105.             isLanding = false;
    106.  
    107.     }
    108.  
    109. }
    110.  
     
  2. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    325
    You are applying the Force in local Coordinates (this is what relatives means) but passing as argument forward in world coordinates

    Try AddForce instead

    Also, select your airplane, and be sure that the forward vector is aligned with the longitudinal axis of the airplane.

    You must be sure that the scene view is set to pivot local

    By the way, you should apply the forces in a FixedUpdate and Impulse Mode is not for what you think it is.

    You should better try either Mode.Force or Mode.Acceleration

    upload_2019-10-8_17-21-58.png
     
  3. TrickShotMLG

    TrickShotMLG

    Joined:
    Jun 3, 2018
    Posts:
    28
    Thanks for your reply.
    I will try it as soon as possible.
     
  4. TrickShotMLG

    TrickShotMLG

    Joined:
    Jun 3, 2018
    Posts:
    28
    It is working now, but when I rotate the plane it is moving directional.
    And not moving in a curve