Hello there !
I have a quite simple question to ask. Here’s my problem : I have an object that navigates between random points and I would like it to move smoothly. Put simply : an object reaches its destination and now have a new one. It turns progressively toward its new destination as it moves, defining a curve.
Anyone’s got an idea on how to implement that ?
Thanks in advance for your answer 
3 Answers
3
If you are trying to lerp rotate only a Vector3, there’s Vector3.Slerp() and Vector3.RotateTowards(). I suggest you look through the Script Reference if you are curious about all the things you can do with a class.
Keep in mind that GameObjects use a class called Transform to describe their orientation in 3D space, so changing a vector might not be what you actually want. Transform’s rotation component is a Quaternion. To spherically interpolate a Quaternion, use Quaternion.Slerp, or any of the other useful functions it has to achieve what you want.
Something like this:
public float smoothingFactor = 3;
public Transform target;
public float speed = 2;
void Update()
{
var directionToTarget = target.position - transform.position;
//If you are on the ground and only rotating about Y then:
directionToTarget.y = 0;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookDirection(directionToTarget, Vector3.up), Time.deltaTime * smoothingFactor);
transform.position += transform.forward * speed;
}
Hey whydoidoit (or anyone really),
What’s the difference between the two rotations below e.g. Quaternion.LookRotation (2 line commented out) vs. Quaternion.Slerp (currently active)
//Move towards new vector position
//this.transform.position = Vector3.MoveTowards(this.transform.position, target, (smoothing * Time.deltaTime));
this.transform.position = Vector3.Lerp(this.transform.position, target, (smoothing * Time.deltaTime));
//Rotate towards new vector position
var dir = target - this.transform.position;
//Example 1 rotation
//var r = Vector3.RotateTowards(this.transform.forward, dir, ((smoothing * 4f) * Time.deltaTime), 0f);
//this.transform.rotation = Quaternion.LookRotation(r);
//Example 2 rotation
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(dir, Vector3.up), ((smoothing * 4f) * Time.deltaTime));
Thanks for your time and advice in advance!
Do you want it to be stopped when it's turning or arc towards the point?
– whydoidoitNope, actually I want it to keep its velocity as it turns.
– DocteurCoxWhat language?
– whydoidoitI use C# for my scripts.
– DocteurCox