I’m having some trouble making my object lerp correctly with rotation. I know there’s some funky business using quaternions an euler angles. I’m not sure what the problem is though, as my translation for lerp works well enough.
public class PlayerScript : MonoBehaviour {
int playerHealth = 100;
float reposStart, furthestDist, furthestRot;
public float collisionDelay = 0.7f, reposSpeed = 1, moonTapped;
Vector3 savedPos, savedRot;
public bool bMoveAbility;
// Use this for initialization
void Start ()
{
// Setting the Civilisation's original position.
savedPos = transform.position;
bMoveAbility = false;
savedRot = transform.eulerAngles;
}
//Called from the 'MoonScript'
public void LoseHealth(int healthLost)
{
// gives a delay before re-aligning the civilisation's position, and sets the start time for reposition as well.
reposStart = Time.time;
moonTapped = collisionDelay;
}
// This puts the civilisation's back in their neutral position after being hit..
void FixedUpdate()
{
// Gives the Civilsations a delay before they reposition
if(moonTapped > 0)
{
// If timer's still higher than requirement, minus time from it.
moonTapped -= Time.deltaTime;
// Gets the furthest distance/rotation travelled before the delay expires.
furthestDist = Vector3.Distance(savedPos, transform.position);
furthestRot = Vector3.Dot(savedRot, transform.eulerAngles);
// Don't let it recalibrate if not ready. Return that shit.
return;
}
if(!bMoveAbility)
{
// Takes the time travelled divided by the total journey left, to get the fraction of the journey travelled.
float timePosCovered = (Time.time - reposStart) * reposSpeed;
float timeRotCovered = (Time.time - reposStart) * (reposSpeed + 2);
float fractureDistJourney = timePosCovered / furthestDist;
float fractureRotJourney = timeRotCovered / furthestRot;
// Repositions the well.. position.
if(savedPos != transform.position)
{
Vector3 currPos = transform.position;
transform.position = Vector3.Lerp(currPos, savedPos, fractureDistJourney);
}
// Repositions the rotation.
if(savedRot != transform.eulerAngles)
{
Vector3 newRot = Vector3.Lerp(transform.eulerAngles, savedRot, fractureRotJourney);
Quaternion rotation = Quaternion.Euler(newRot);
transform.rotation = rotation;
}
}
}
As I said. Translation works absolutely okay. I have an issue with getting the rotation to work. Instead of smoothing to the correct rotation naturally. It jolts there immediately, as if there is no time difference between beginning and end of rotation.
I understand to make the code more efficient I could turn this into a coroutine so I don’t constantly have if statements running in the main game loop, but I will still be coming across this problem.
Quaternion Lerp(Quaternion from, Quaternion to, float t);
Don’t use the current rotation in the ‘from’ parameter. The from is the starting point before you do any rotation and the to is the end point after all rotation is done.
Awesome Mycroft thank you. I now understand that completely. However the code is still on prozak. I now know the exact fault. The while loop turns into a endless loop. The reason is because the object never loses its velocity even though I’m changing his transform in the while loop.
If I force the objects while loop to end after a period of time, it’s exact previous velicty from the hit makes the object continue off into the distance.
This is what I’m using. So I’m assuming I need to give up on Lerp entirely and use velocity instead?