Search Unity

[SOLVED] Make an object hover up trom terrain and then hover in place

Discussion in 'Physics' started by Inferi, Aug 9, 2017.

  1. Inferi

    Inferi

    Joined:
    Sep 28, 2015
    Posts:
    145
    Hi all.
    I am trying make my model hover from terrain location to its final floating "hover" location. The code I have now only hover it and not "slerp it" up from the terrain "hard to explain" :)

    At a certain hour the object should shut down and fall to the ground "That works". At another hour the object should power up and move up to a location above the terrain and start hover in place. Here is where I am stuck. The object do not move slowly up but just jumps to the end location at once when it "powers up".

    I have tried different ways but nothing works as I want.

    Anyone have any good ideas or even code I can look at. I would like the hoverlocation to be customizable.

    Look at vid below to see what I mean. If anything is unclear, please just shout at me :)

    Code I have is in vid and at bottom of this post:




    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class collectorHoverManager : MonoBehaviour {
    6.     private GlobalPlayer GP;
    7.     [SerializeField]
    8.     private Terrain terrain;
    9.     Vector3 startPosition;
    10.     float terrainY;
    11.     [SerializeField]
    12.     [Range(1,10)]
    13.     private float hoverLevel = 1;
    14.     [SerializeField]
    15.     [Range(1,100)]
    16.     private float hoverFloatValue = 10;
    17.     private RaycastHit collectorHit;
    18.  
    19.  
    20.     // Use this for initialization
    21.     void Start () {
    22.         GP = GameObject.Find("GlobalPlayer").GetComponent<GlobalPlayer>();
    23.         startPosition = transform.position;
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update () {
    28.         // Collectorn ska endast fungera på dagen.
    29.         // På natten ska den falla till marken och inte kunna röra sig förrän nästa dag.
    30.         if (GP.getSetTimeOfDayManager.Hour > 8 && GP.getSetTimeOfDayManager.Hour < 17) {
    31.             if (transform.GetComponent<Rigidbody>().isKinematic == false) {
    32.                 transform.GetComponent<Rigidbody>().isKinematic = true;
    33.             }
    34.             terrainY = terrain.SampleHeight(new Vector3(transform.position.x, 0, transform.position.z));
    35.             float x = transform.position.x;
    36.             float y = Mathf.Sin(Time.timeSinceLevelLoad * 1.5f) / hoverFloatValue;
    37.             float z = transform.position.z;
    38.             transform.position = new Vector3(x, (terrainY + y) + hoverLevel, z);
    39.             if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.back), out collectorHit, 5f)) {
    40.                 transform.rotation = Quaternion.FromToRotation(Vector3.forward, collectorHit.normal);
    41.             }
    42.         } else {
    43. //            Debug.Log(transform.GetComponent<Rigidbody>().isKinematic);
    44.             transform.GetComponent<Rigidbody>().isKinematic = false;
    45.         }
    46.     }
    47. }
    48.  
     
  2. BoogieD

    BoogieD

    Joined:
    Jun 8, 2016
    Posts:
    236
    Can't you just add some force to the Rigidbody to make it go up?
     
    Inferi likes this.
  3. Inferi

    Inferi

    Joined:
    Sep 28, 2015
    Posts:
    145
    Yes, but I need to know far it should go up before the "hover" will begin so it dont looks like it jumps.

    Right now I am trying to check if it is on the ground and if it is, then move it upp slowly and then at a certain height it should start hovering. It should work. Ill report back :D
     
  4. Inferi

    Inferi

    Joined:
    Sep 28, 2015
    Posts:
    145
    FIXED IT :)

    Here is the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class collectorHoverManager : MonoBehaviour {
    6.     private GlobalPlayer GP;
    7.     [SerializeField]
    8.     private Terrain terrain;
    9.     Vector3 startPosition;
    10.     float terrainY;
    11.     [SerializeField]
    12.     [Range(1,10)]
    13.     private float hoverLevel = 1;
    14.     [SerializeField]
    15.     [Range(1,100)]
    16.     private float hoverFloatValue = 10;
    17.     private RaycastHit collectorHit;
    18.  
    19.     private bool collectorOnGround = true;
    20.     private float speed = .12f;
    21.     private float step;
    22.  
    23.     void Start () {
    24.         GP = GameObject.Find("GlobalPlayer").GetComponent<GlobalPlayer>();
    25.         startPosition = transform.position;
    26.     }
    27.  
    28.     void Update() {
    29.         terrainY = terrain.SampleHeight(new Vector3(transform.position.x, 0, transform.position.z));
    30.         if (collectorOnGround && GP.getSetTimeOfDayManager.Hour > 8 && GP.getSetTimeOfDayManager.Hour < 17) {
    31.             transform.GetComponent<Rigidbody>().isKinematic = true;
    32.             step = speed * Time.deltaTime;
    33.             transform.position = Vector3.MoveTowards(transform.position, new Vector3(transform.position.x, (terrainY + hoverLevel), transform.position.z), step);
    34.             if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.back), out collectorHit, 5f)) {
    35.                 transform.rotation = Quaternion.FromToRotation(Vector3.forward, collectorHit.normal);
    36.             }
    37.             if (transform.position.y >= (terrainY + hoverLevel)) {
    38.                 collectorOnGround = false;
    39.             }
    40.         } else {
    41.             if (GP.getSetTimeOfDayManager.Hour > 8 && GP.getSetTimeOfDayManager.Hour < 17) {
    42.                 if (transform.GetComponent<Rigidbody>().isKinematic == false) {
    43.                     transform.GetComponent<Rigidbody>().isKinematic = true;
    44.                 }
    45.                 terrainY = terrain.SampleHeight(new Vector3(transform.position.x, 0, transform.position.z));
    46.                 float x = transform.position.x;
    47.                 float y = Mathf.Sin(Time.timeSinceLevelLoad * 1.5f) / hoverFloatValue;
    48.                 float z = transform.position.z;
    49.                 transform.position = new Vector3(x, (terrainY + y) + hoverLevel, z);
    50.                 if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.back), out collectorHit, 5f)) {
    51.                     transform.rotation = Quaternion.FromToRotation(Vector3.forward, collectorHit.normal);
    52.                 }
    53.             } else {
    54.                 transform.GetComponent<Rigidbody>().isKinematic = false;
    55.             }
    56.         }
    57.     }
    58.  
    59.     private void OnCollisionEnter(Collision collision) {
    60.         if (collision.transform.name == "Terrain") {
    61.             collectorOnGround = true;
    62.         }
    63.     }
    64.  
    65. }
    66.  

    If anyone have a better way of doing it, feel free to tell me :)

    The script is placed on the object that should hover btw :)
     
  5. BoogieD

    BoogieD

    Joined:
    Jun 8, 2016
    Posts:
    236