I’m trying to make the right or left side of an object look at another object, unlike the common approach where you would want the front of an object to face another object. But the code below instantly snaps the object to the new rotation, and I’ve been unable to add a gradual rotation (Slerp)
Vector3 direction = (player.position - transform.position).normalized;
transform.right = direction;
Would someone please explain how to add a Slerp in this example?
public float damping;
Quaternion rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
This is took from the old standard assets SmoothLookAt
You should never set the directional vectors of a transform, you will get very strange results sometimes because setting transform.right does not necessarily mean that transform.forward or transform.up will end up where you want them.
You should get the axis to rotate first (if transform.right is exactly 180 degrees away from direction, you will have to rotate just a smidge for this to work) :
if(-transform.right == direction){
transform.Rotate(0, 0.1f, 0); //rotate a smidge on the y axis to get less than 180 degrees
}
Vector3 axis = Vector3.Cross( transform.right, direction );