Lerping transform.forward

Hey guys,

I have my character turning around using a Vector3.Lerp between transform.forward towards movementDirection. It works fine, very simple and all… but… theres a problem

Lets take the following example. The enemy detects me, I want it to turn and face me, and keep facing me. My code works fine the way it is, however, if I am EXACTLY behind the enemy, he doesnt turn around, he just stays looking in the opposite direction. And it is also noticiable that the closer to bein exactly precisely behind him, the slower he turns around, as if Lerp were calculating with 2 similar numbers, instead of 2 very different numbers.

Can anyone please enlighten me as of to why this happens?

Also, another somewhat related question, is there a simple “Lerp” algorithm that makes the tween between one value and the other, but trully LINEARLY… I mean, every frame tweening the same amount and not a percentage of the amount required? I use NGUIs tween for most stuff that I can use it to, and I know it works this way, but I would like to have something that worked exactly like Lerp (call it every frame) and it will do it, and it can be used for every variable/number I want… any ideas?

Thanks for the attention,
Best regards,
Allan

Can you show your Lerp code?

Also, this might make more sense in this case since you don’t have to calculate a ‘t’ value for the lerp.

1 Like

Sure, heres the code:

Vector3 targetPos = targetToLookAt.position - myTransform.position;
targetPos.y = myTransform.forward.y;
myTransform.forward = Vector3.Lerp(myTransform.forward, targetPos.normalized, turningSpeed);

The problem happens when, for example, I am Lerping from (0, 0, 1) to (0, 0, -1)… it stays completely still… if I am just a bit to the side, it starts to turn but very slowly at first and then faster… as if the lerp were considering the numbers to be equal…

Edit: Also, what I meant is that I want the object to, for example, rotate at a fixed speed instead of Lerping towards a target rotation… in this case for example, I have a monster with a flamethrower and the player has to rotate around it to avoid the fire… but the closer the monster is to hitting the player (to aiming straight) the slower it turns… this has to be updated every frame because I need to check where the player is (or very often), and rotation speed is also important…

Edit2: Hmmm… RotateTowards really does the trick, awesome! Thanks!

1 Like

Okay cool! Yeah, RotateTowards is exactly what you want since it takes speed * Time.deltaTime.

Lerp, on the other hand, wants a percent complete, not a speed. So I suspect that the “turningSpeed” you’re feeding into a Lerp is calculated wrong.