Rotate towards somthing slowly

I know this has been posted somewhere else because i remember seeing it, but i cant find it, so im sorry for wasting space

i have the main player doing a melee attack where he jumps at the target to attack

what i want to happen is if he is not facing his target to jump through the air( which i already have him doing) and rotate just slow enough to be facing the target when he hits it

so like, if its really far away, rotate really slowly and if he’s right next to it it just flips him around really quick

but the problem is, i suck at math, and i cant figure out how to make him rotate towards the target, or turn slowly based on distance

i would appreciate some help, thanks

Could you use Transform.LookAt?

–Eric

i tried using look at

it snapped to it instantly and looked really weird, and it was like arguing with the object, so as soon as i let go of the button it snapped right back to the direction it was looking before

I don’t know if there is a more efficient way but I think this should work:

1- find the vector (in the xz plane) pointing from your character to your target
2- calculate a FromToRotation from your character’s forward to this vector
3- get the quaternion = your character’s current rotation pre multiplied by the FromToRotation quaternion u calculated in 2. this is the destination rotation ur character should attain smoothly over time.
4- Lerp or Slerp ur character’s rotation to the quternion u calculated in 3. use different interpolation factors to make the smooth rotation happen very quick (high coefficient) or with a slower pace (low coefficient)

hope this works,
cheers,
Firas.

if you know how long the character will be in the air, it’s pretty easy. Let’s say it’s 1 second.

//this goes in a subroutine that should be called when the jump starts.
var duration : float = 1;
var startRot : Quaternion = transform.rotation;
for (t=0.0;t<duration;t+=Time.deltaTime) {
var targetRot : Quaternion = Quaternion.LookRotation(target.position - transform.position); //recalculate every frame because it can change over the course of the jump
transform.rotation = Quaternion.Slerp(startRot, targetRot, t / duration);
yield;
}