- Home /
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);
err...
Searching(); is called in Update. Also like Following();
Following works fine as following.
It seems like the Vector3 of targetDirection maybe?
This is a late reply... Busy reading the answers.
transform.rotation = Quaternion.LookRotation(targetDirection);
so
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetDirection), navSpeed * Time.deltaTime);
=
transform.rotation = Quaternion.Slerp(transform.rotation, transform.rotation, navSpeed * Time.deltaTime);
All Lerps and Slerps go
(From, To, t)
Quaternion.LookRotation(targetDirection); should be fine as 'To' but you are already making transform.rotation equal to it in one step before you do the Slerp.
Ooooooooooooh...
Ok well my brain is dead for today.
Thanks for help so far I will test tomorrow.
Answer by sdgd · Aug 23, 2013 at 12:32 PM
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);
}
Thanks, though I didn't do it exactly as that, my target was a vector and transform already.
Anyway it worked.
am thanks, ... could you accept the previous answer please, ...
you closed the Q and said you accepted it but you didn't, ...
I'll keep an eye on you few more times so I can help you more :)
and gain more karma with being specific and accurate with answer.
Answer by meat5000 · Aug 22, 2013 at 11:51 PM
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 :/
Answer by sparkzbarca · Aug 22, 2013 at 11:57 PM
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;
}