I’m using this script on a object to move the object from one position to a target position and when the object is above the target i want the object to move directly down.
But instead what it does not is when the object is above the other object the target then he just rotating in the air above the target. I can’t figure out why it’s not moving down and why the object keep rotating above the target.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FlyToOverTerrain : MonoBehaviour
{
public Transform target;
public float desiredHeight = 10f;
public float flightSmoothTime = 10f;
public float maxFlightspeed = 10f;
public float flightAcceleration = 1f;
public float levelingSmoothTime = 0.5f;
public float maxLevelingSpeed = 10000f;
public float levelingAcceleration = 2f;
private Vector3 flightVelocity = Vector3.zero;
private float heightVelocity = 0f;
private RaycastHit hit;
private void Start()
{
}
private void LateUpdate()
{
Vector3 position = transform.position;
float currentHeight = position.y;
if (target && flightAcceleration > float.Epsilon)
{
position = Vector3.SmoothDamp(position, target.position, ref flightVelocity, flightSmoothTime / flightAcceleration, maxFlightspeed, flightAcceleration * Time.deltaTime);
}
if (levelingAcceleration > float.Epsilon)
{
float targetHeight = Terrain.activeTerrain.SampleHeight(position) + desiredHeight;
position.y = Mathf.SmoothDamp(currentHeight, targetHeight, ref heightVelocity, levelingSmoothTime / levelingAcceleration, maxLevelingSpeed, levelingAcceleration * Time.deltaTime);
}
transform.position = position;
if (Physics.Raycast(transform.position, Vector3.down, out hit))
{
if (hit.transform == target)
{
transform.position = Vector3.MoveTowards(transform.position, target.position, 3 * Time.deltaTime);
}
}
else
{
transform.position = position;
}
}
}