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:
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;
}
}
}