# How would I smooth out a Quaternion?

void DoOrientation()
{
if (go = null)
{
Finish ();
return;
}

``````			v1 = leftFront.Value;
v2 = leftRear.Value;
v3 = rightFront.Value;
v4 = rightRear.Value;

Vector3 upDir;
upDir = (Vector3.Cross (v1, v3) +
Vector3.Cross (v3, v4) +
Vector3.Cross (v4, v2) +
Vector3.Cross (v2, v1)).normalized;

go.transform.rotation = Quaternion.FromToRotation(go.transform.up, upDir) * go.transform.rotation;
}
``````

I feel like I’m almost there but I can’t figure out how to use Quaternion.Lerp in this case while I’ve got Quaternion.FromToRotation actually figuring things out.

The use case is I have 4 feet of a Quadruped defined and I’m using the normal average to orient the body of the Agent / Vehicle / Quadruped… I would like this to be smoother because it’s quite jerky as is but I’m fairly burnt out trying to sort out the Vectors and/or Quaternions for this as it really isn’t my forte so many someone can shine some fresh light on my problem :s …

Thanks

i just recently had to figure out Quaternion.lerp as well, and i hope i’m understanding your code right:

you want the rotation of the “vehicle’s” body to match the average direction of its 4 feet?

what you could try is something like this

``````go.transform.rotation = Quaternion.Lerp(go.transform.rotation, targetRotation.rotation, rotationSpeed);
``````

you would need to set the upDir’s averaged Vector3 into a transform i think, because the Quaternion.Lerp function only deals with Quaternions - not Vector3s. and use that as the “targetRotation.rotation”

and as for the rotation speed: 1 is instant rotation i think. so to make it smooth try .1 or so.

void DoOrientation()
{
if (go == null)
{
Finish ();
Debug.LogWarning(“GameObject Missing.”);
return;
}

``````			v1 = leftFront.Value;
v2 = leftRear.Value;
v3 = rightFront.Value;
v4 = rightRear.Value;

Vector3 upDir;
upDir = (Vector3.Cross (v1, v3) +
Vector3.Cross (v3, v4) +
Vector3.Cross (v4, v2) +
Vector3.Cross (v2, v1)).normalized;

// create and define the target rotation using the average direction of the feet
Quaternion targetRotation = Quaternion.FromToRotation(go.transform.up, upDir) * go.transform.rotation;

// apply the lerp from the current rotation toward the target rotation with a speed.
go.transform.rotation = Quaternion.Lerp(go.transform.rotation, targetRotation, speed.Value);

// working // old code, simpler. //
// go.transform.rotation = Quaternion.FromToRotation(go.transform.up, upDir) * go.transform.rotation;
// working //
}
``````

Solved code above.

Had to create a transform as a target which solved the averaging of the direction first, then Lerp between the go rotation and the solved Quaternion.

Thanks for hardmode2236 for pointing this out.