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

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” :slight_smile:

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 :slight_smile:

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

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class collectorHoverManager : MonoBehaviour {
    private GlobalPlayer GP;
    [SerializeField]
    private Terrain terrain;
    Vector3 startPosition;
    float terrainY;
    [SerializeField]
    [Range(1,10)]
    private float hoverLevel = 1;
    [SerializeField]
    [Range(1,100)]
    private float hoverFloatValue = 10;
    private RaycastHit collectorHit;


    // Use this for initialization
    void Start () {
        GP = GameObject.Find("GlobalPlayer").GetComponent<GlobalPlayer>();
        startPosition = transform.position;
    }

    // Update is called once per frame
    void Update () {
        // Collectorn ska endast fungera på dagen.
        // På natten ska den falla till marken och inte kunna röra sig förrän nästa dag.
        if (GP.getSetTimeOfDayManager.Hour > 8 && GP.getSetTimeOfDayManager.Hour < 17) {
            if (transform.GetComponent<Rigidbody>().isKinematic == false) {
                transform.GetComponent<Rigidbody>().isKinematic = true;
            }
            terrainY = terrain.SampleHeight(new Vector3(transform.position.x, 0, transform.position.z));
            float x = transform.position.x;
            float y = Mathf.Sin(Time.timeSinceLevelLoad * 1.5f) / hoverFloatValue;
            float z = transform.position.z;
            transform.position = new Vector3(x, (terrainY + y) + hoverLevel, z);
            if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.back), out collectorHit, 5f)) {
                transform.rotation = Quaternion.FromToRotation(Vector3.forward, collectorHit.normal);
            }
        } else {
//            Debug.Log(transform.GetComponent<Rigidbody>().isKinematic);
            transform.GetComponent<Rigidbody>().isKinematic = false;
        }
    }
}

Can’t you just add some force to the Rigidbody to make it go up?

1 Like

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 :smile:

FIXED IT :slight_smile:

Here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class collectorHoverManager : MonoBehaviour {
    private GlobalPlayer GP;
    [SerializeField]
    private Terrain terrain;
    Vector3 startPosition;
    float terrainY;
    [SerializeField]
    [Range(1,10)]
    private float hoverLevel = 1;
    [SerializeField]
    [Range(1,100)]
    private float hoverFloatValue = 10;
    private RaycastHit collectorHit;

    private bool collectorOnGround = true;
    private float speed = .12f;
    private float step;

    void Start () {
        GP = GameObject.Find("GlobalPlayer").GetComponent<GlobalPlayer>();
        startPosition = transform.position;
    }

    void Update() {
        terrainY = terrain.SampleHeight(new Vector3(transform.position.x, 0, transform.position.z));
        if (collectorOnGround && GP.getSetTimeOfDayManager.Hour > 8 && GP.getSetTimeOfDayManager.Hour < 17) {
            transform.GetComponent<Rigidbody>().isKinematic = true;
            step = speed * Time.deltaTime;
            transform.position = Vector3.MoveTowards(transform.position, new Vector3(transform.position.x, (terrainY + hoverLevel), transform.position.z), step);
            if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.back), out collectorHit, 5f)) {
                transform.rotation = Quaternion.FromToRotation(Vector3.forward, collectorHit.normal);
            }
            if (transform.position.y >= (terrainY + hoverLevel)) {
                collectorOnGround = false;
            }
        } else {
            if (GP.getSetTimeOfDayManager.Hour > 8 && GP.getSetTimeOfDayManager.Hour < 17) {
                if (transform.GetComponent<Rigidbody>().isKinematic == false) {
                    transform.GetComponent<Rigidbody>().isKinematic = true;
                }
                terrainY = terrain.SampleHeight(new Vector3(transform.position.x, 0, transform.position.z));
                float x = transform.position.x;
                float y = Mathf.Sin(Time.timeSinceLevelLoad * 1.5f) / hoverFloatValue;
                float z = transform.position.z;
                transform.position = new Vector3(x, (terrainY + y) + hoverLevel, z);
                if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.back), out collectorHit, 5f)) {
                    transform.rotation = Quaternion.FromToRotation(Vector3.forward, collectorHit.normal);
                }
            } else {
                transform.GetComponent<Rigidbody>().isKinematic = false;
            }
        }
    }

    private void OnCollisionEnter(Collision collision) {
        if (collision.transform.name == "Terrain") {
            collectorOnGround = true;
        }
    }

}

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

The script is placed on the object that should hover btw :slight_smile:

This might help.

1 Like