Search Unity

How can i add force to the spaceship to move down slowly and not falling down ?

Discussion in 'Scripting' started by Chocolade, Sep 25, 2017.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    933
    The object is a spaceship. The script attached to it make the spaceship moving to a specific target object.
    But when the spaceship is in a high place and start moving out of the surface it's falling down very fast. What i want is to add some force to it so the falling will be slowly using the engine and engine(1) childs to make it slowly moving down like landing. So the engines should be rotating down i think when it's moving down and depending on the high or something else the engine/s should be change forces and also make the spaceship moving down slower or faster.



    And this is the engine that is attached in the back of the spaceship Inspector:



    The script that attached to the Spaceship:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class FlyToOverTerrain : MonoBehaviour
    7. {
    8.     public Transform target;
    9.     public float desiredHeight = 10f;
    10.  
    11.     public float flightSmoothTime = 10f;
    12.     public float maxFlightspeed = 10f;
    13.     public float flightAcceleration = 1f;
    14.  
    15.     public float levelingSmoothTime = 0.5f;
    16.     public float maxLevelingSpeed = 10000f;
    17.     public float levelingAcceleration = 2f;
    18.    
    19.     private Vector3 flightVelocity = Vector3.zero;
    20.     private float heightVelocity = 0f;
    21.  
    22.     private void LateUpdate()
    23.     {
    24.         Vector3 position = transform.position;
    25.         float currentHeight = position.y;
    26.         if (target && flightAcceleration > float.Epsilon)
    27.         {
    28.             position = Vector3.SmoothDamp(position, target.position, ref flightVelocity, flightSmoothTime / flightAcceleration, maxFlightspeed, flightAcceleration * Time.deltaTime);
    29.         }
    30.  
    31.         if (levelingAcceleration > float.Epsilon)
    32.         {
    33.             float targetHeight = Terrain.activeTerrain.SampleHeight(position) + desiredHeight;
    34.  
    35.             position.y = Mathf.SmoothDamp(currentHeight, targetHeight, ref heightVelocity, levelingSmoothTime / levelingAcceleration, maxLevelingSpeed, levelingAcceleration * Time.deltaTime);
    36.         }
    37.  
    38.         transform.position = position;
    39.     }
    40. }
    41.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    When I needed to do this I applied a constant force going against the direction of gravity. Experiment with it to find the correct amount of force to apply (too little and the ship appears to fall, too much and it actually begins going up instead of down).