Search Unity

How can i calculate the object movement according to it's speed, distance from target, and physics ?

Discussion in 'Physics' started by haimmoshe, Jun 24, 2017.

  1. haimmoshe

    haimmoshe

    Joined:
    Jun 3, 2017
    Posts:
    237
    What i want is that the spaceship will make a big round turning to the target and then to stand in the air above the target.

    First problem i have is that it's not getting to be above the target and in the code i'm doing that it will rotate to the target direction but since i changed the Math.Min to .2f and the in the Inspector the speed to 120 it's not doing what i want in general but it's not getting to the target.

    What i need to i think is somehow to calculate with the current gameobject movement speed and it's current height and speed and distance from the target how to make the big turn to get above the target(Base, The red cube).

    So if the gameobject start in any height when i click the L key it will turn make a big turn and will move to be above the target.

    So it's doing the turn now but i'm not sure how to calculate all the values to make above the target and not under it like now.

    The gameobject that move have two components Rigidbody(Use Gravity set to false) and a box collider(Is Trigger set to true)

    The target the Base havea box collider and also the Is Trigger set to true.

    Code (csharp):
    1.  
    2. void Update()
    3.     {
    4.         if (landing == false)
    5.         {
    6.             var v3 = new Vector3(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"), 0.0f);
    7.             transform.Rotate(v3 * rotationSpeed * Time.deltaTime);
    8.             transform.position += transform.forward * Time.deltaTime * movementspeed;
    9.  
    10.             if (Input.GetKey(KeyCode.Z))
    11.                 transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);
    12.  
    13.             if (Input.GetKey(KeyCode.R))
    14.                 transform.Rotate(Vector3.right * rotationSpeed * Time.deltaTime);
    15.  
    16.             if (Input.GetKey(KeyCode.P))
    17.             {
    18.                 isPKeyDown = Input.GetKey(KeyCode.P);
    19.                 float distance = Vector3.Distance(previousPosition, transform.position);
    20.                 acceleration = distance / Mathf.Pow(Time.deltaTime, 2);
    21.  
    22.                 previousPosition = transform.position;
    23.                 _rigidbody.AddRelativeForce(0f, 0f, thrust, ForceMode.Acceleration);
    24.             }
    25.         }
    26.         else
    27.         {
    28.             if (Physics.Raycast(transform.position, baseTarget.position, out hit, maxDistance))
    29.             {
    30.                 transform.rotation = Quaternion.identity;
    31.                 _rigidbody.useGravity = false;
    32.             }
    33.             else
    34.             {
    35.                 transform.position += transform.forward * Time.deltaTime * movementspeed;
    36.                 float distance = Vector3.Distance(transform.position, baseTarget.position);
    37.                 var targetRotation = Quaternion.LookRotation(baseTarget.position - transform.position);
    38.                 var str = Mathf.Min(.2f * Time.deltaTime, 1);
    39.                 transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, str);
    40.             }
    41.         }
    42.  
    43.         if (Input.GetKey(KeyCode.L))
    44.         {
    45.             landing = true;
    46.             lastPosition = transform.position;
    47.         }
    48.     }
    49.  
    The part with the Physics.Raycast should make the object to stand in the air above the target in specific height in this case maxDistance is set to 500.

    The else part is the first part and it's doing the gameobject to turn to the target(baseTarget).

    I recorded a short video clip showing how it's working now.
    I also showing the inspector of the gameobject that move to the target and the inspector of the target(Base).