Slerp look at direction not working...

Yo this is a very simple problem, hopefully.

I can’t get this AI of mine to look at the direction of a collision smoothly. He snaps at it.

My usual code that I have used for the AI following does not work. It gives no errors but the AI does not detect the bullet collision or simply can’t turn.

Code:

// This is the code that works but I cannot get to slerp or be smooth.

void Searching(){

transform.rotation = Quaternion.LookRotation(targetDirection);


}


// This is where the targetDirection comes from.

void OnTriggerEnter(Collider colp){

   targetDirection = colp.transform.postion-transform.postion;



} }

Can’t get the dam thing to slerp.

This does not work:

transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetDirection), navSpeed * Time.deltaTime);

public GameObject Player; // this is the object you will be rotating
public GameObject Target; // this is the object you will look at

void Update(){
     Player.transform.rotation = Quaternion.Slerp(Player.transform.rotation, (Quaternion.LookRotation(Target.transform.position - Player.transform.position)), Time.deltaTime * 2);
}

SLerp Manual

A Slerp, like a Lerp is not performed in one function. You must attend to the function frame after frame gradually changing the value of t. Note that t ONLY works between 0 and 1. So when navSpeed * Time.deltaTime is Greater than 1 the Slerp will be at its destination.
What is the value of navSpeed?

Also your

transform.rotation = Quaternion.LookRotation(targetDirection);

so if you call Searching() before your Slerp is performed then in your Slerp your From and To fields are already equal.

On top of that, I have a feeling that if it did Slerp you wouldnt reach your destination:

transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetDirection), navSpeed * Time.deltaTime);

Each frame your transform.rotation is updated to be the Current Position of the result of the Slerp. This late at night Im not sure but my pulverised brain is telling me you’ll slow down as you get closer? I could be wrong :confused:

as noted for slerp to work it needs to be in update or fixedupdate
heres your correct code. Have a nice day!

bool Search;

void Start()
{
Search = false;
}

void Searching()
{
Search = true;
}

Void FixedUpdate()
{
if(Search)
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetDirection), navSpeed * Time.fixedDeltaTime);
}


if (transform.forward == targetDirection)
search = false;
}