- Home /
How can i make the camera to rotate smooth to next target how to move to next target and how to slowly stop before each 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;
public Vector3 distanceFromTarget;
public Vector3 distanceToStop;
private Vector3 flightVelocity = Vector3.zero;
private float heightVelocity = 0f;
private void LateUpdate()
{
Vector3 position = transform.position;
float currentHeight = position.y;
if ((bool)target[0] && flightAcceleration > float.Epsilon)
{
position = Vector3.SmoothDamp(position, target[0].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;
distanceFromTarget = transform.position - target[0].position;
}
}
Now it's moving to target[0] and just before it's reaching it, it's slowly move until it stop. and it stopping about distance 4 before the target.
I added the variable distanceFromTarget to view the distance info in rela time. I also added a variable distanceToStop and i wonder how can i make that for example if the distance from the target is 10 then slowly stop lets say at distance 9-8 or 7. And if the distance if i will set it to be 15 then slowly stop at distance 14-13 or 12.
And then i want that the camera will lookat the target like viewing a bit above and some distance on the target and wait X seconds for example 3 seconds and then smooth slowly will rotate and will face the next target in the array and them will start moving to the next target. And same for all targets in the array.
Answer by XBabineOne · May 20, 2017 at 09:22 AM
Hello,
Maybe read this documentation, it might help you:
Moving a transform to a specifc position: https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html
Rotation a transform to a specifc rotation: https://docs.unity3d.com/ScriptReference/Quaternion.Lerp.html
If you want to move slowly your object when near the target, just do a distance: Vector3.Distance(Pos1, Pos2)
Hop it could help you, Babouche