I have an object A moving to another desired object B’s position. The B object is at a random rotation and I am looking to get the A object to rotate to B’s rotation but continue the rotation until A reaches B position. And B can be at different distances from A’s original position but I want A to end up rotating slower or faster depending upon the distance? I’m having a feeling this is a lot harder than I originally thought. haha
There are many ways to do that, it only depends upon what your style of coding is and how you want the rotation to react. Something very simple like this will work just fine…
C#
public Transform A, B;
private float startDistance = 0;
private bool rotateAtoB = false;
public void Update() {
if (/* some condition is met that triggers rotating A to B */) {
startDistance = Vector3.Distance(A.position, B.position);
rotateAtoB = true;
}
if (rotateAtoB == true) {
float delta = 1 - (Vector3.Distance(A.position, B.position) / startDisatnce);
A.rotation = Quaternion.Slerp(A.rotation, B.rotation, delta);
if (Mathf.Approximately(delta, 1)) // stop rotating when A is rotated approximately equal to B's orientation.
rotateAtoB = false;
}
}
That was quick, thank you! However it’ll rotate at the same speed no matter the distance and seems to be the same duration in result. So if an object is closer to the player than it’ll take for the rotation to complete it won’t finish the rotation. If it is further it’ll complete the rotation far before the player reaches the destination. Any idea why while I tinker with the code?
The interpolation of rotation is linearly proportional to the distance between the objects. So, yes, object A will appear rotate at roughly a constant speed as it approaches B.
Sorry I missed that, but this is easily explained and fixed. If you examine the function that is computing the interpolation parameter…
You see that delta = 0 when the distance between the two objects is equal to the starting distance, and it will approach delta = 1 when the distance between A and B approaches 0. And the range of delta from 0 to 1 is linearly proportional to the distance between A and B. This graph is interpreted as values of delta on the y-axis and values of currentDistance/startDistance on the x-axis.
One way that you can easily modify this to grow faster as the distance between A and B decreases, is by raising the ratio of distance to a fractional power. For example, we’ll raise it to the 5/9ths power…
Now as the distance between A and B approaches zero, this function will grow increasingly faster towards 1 by a function of (1-x5/9). That’s going to result in the following curve for delta:
Not sure I understand what you’re saying there. It will finish the rotation when the distance between them is exactly 0, not before. If you need the rotation to complete when A and B are still some x distance away from each other, because perhaps they cannot intersect, then you need to subtract x from the distance between A and B in the computation of delta.
Wow. That’s an amazing response. haha Delta now does what it’s supposed to but I think it’s something to do with the way Slerp works because it still performs the rotation the same.
And what I meant is that A (being the player) and B (being platform 1 in this case). So if player is moving to platform 1, which is considerably close, delta will reach 1 but the rotation will not complete because it’s reached that destination before Slerp could finish the rotating of player. If player is moving to platform 2, which is considerably far away, the rotation will complete way before it reaches that destination. So I think it has to do with Slerp and not your delta calculations.