Sunday, November 5, 2017

c# - Unity smooth local rotation around one Axis, oriented toward a target


enter image description here enter image description here


I want to acheive the same rotation you see in the inspector. In short: a smooth local Y rotation, toward a target


I have a Vector3 orientation: Vector3 dirOrientation = targetToLook.position - movingCamera.position;


So, every Update, what should I do ? I try many thing, but I don't realy understand Quaternion...


I just want to rotate localy in Y, toward a precise moving target, NOT in X or Z. and smoothly with some Lerp or Slerp.


Thanks !



Answer




Using the trick discussed in a similar question here and also here, we can define a helper method to point our local Y axis exactly in a particular direction, while turning our local Z axis to face (as close as possible) toward another direction.


Quaternion TurretLookRotation(Vector3 approximateForward, Vector3 exactUp)
{
Quaternion rotateZToUp = Quaternion.LookRotation(exactUp, -approximateForward);
Quaternion rotateYToZ = Quaternion.Euler(90f, 0f, 0f);

return rotateZToUp * rotateYToZ;
}

Now we can use that in Update to face toward our target:



void Update() {
// Form the direction we want to look towards
Vector3 offsetToTarget = target.position - transform.position;

// Preserve our current up direction
// (or you could calculate this as the direction away from the planet's center)
Vector3 up = transform.up;

// Form a rotation facing the desired direction while keeping our
// local up vector exactly matching the current up direction.

Quaternion desiredOrientation = TurretLookRotation(offsetToTarget, up);

// Move toward that rotation at a controlled, even speed regardless of framerate.
transform.rotation = Quaternion.RotateTowards(
transform.rotation,
desiredOrientation,
maxDegreesPerSecond * Time.deltaTime
);

}

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...