- Home /
Is there a way to smoothdamp a lookat?
Right now I am working on an AI scripts and currently have the bot lookat a target once he sees it. The problem is that its done instantly. I would like for it to take a few seconds that would be the reaction time of th ai. Ive used smoothdamp and seen there is a smoothdampangle but wasnt sure how to use it for this purpose. Anyone know how I could acomplish this?
Im using a damping variable with the time.deltatime method..but im not sure of the range for the variable.. is it 0-1... 0r like 1-360? or or what? do lower numbers give you a faster turn? i suppose I could set up a test situation.. but im doing it on fast moving objects, its harder to observe.. I just wish it were here..
Answer by Julien-Lynge · Jan 30, 2013 at 02:40 AM
You can use damping with Slerp. Instead of using LookAt with the transform, work with the Transform's rotation, which is a Quaternion. Use the method SetLookRotation - it will return a quaternion / rotation that's you're target - where you want to look. Then take your current rotation (transform.rotation) and this target rotation, and plug them into Slerp, and that's where you can do smooth damping.
http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.SetLookRotation.html
http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.Slerp.html
Sure thing. You're in for some potentially complicated math working with quaternions, so good luck :)
It's actually not that complicated but you only describe how to actually slerp between two rotations, how do you apply the smooth damp effect?
You can do it a few ways. You could use a library like iTween (which offers a few different damping curves). You could do it yourself with a simple damping function, using Euler's number. You could use http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.SmoothDamp.html
However you do it, you take the output of your damping function and plug that into the slerp as the last argument.
I like to use a float called "Damping" and multiple it with Time.deltaTime that's smooth and a little adjustable.
Answer by idbrii · Mar 28, 2018 at 04:00 PM
Using @Julien-Lynge's answer, I came up with this:
public Transform Target;
public float RotateSmoothTime = 0.1f;
private float AngularVelocity = 0.0f;
// ...
var target_rot = Quaternion.LookRotation(Target.position - transform.position);
var delta = Quaternion.Angle(transform.rotation, target_rot);
if (delta > 0.0f)
{
var t = Mathf.SmoothDampAngle(delta, 0.0f, ref AngularVelocity, RotateSmoothTime);
t = 1.0f - t/delta;
transform.rotation = Quaternion.Slerp(transform.rotation, target_rot, t);
}
Your answer
Follow this Question
Related Questions
Basic AI Locked Axis 1 Answer
my ai wiggles slightly 1 Answer
Random movment, like mobs in wow 1 Answer
Enemy AI don't collide with objects or rotate. 0 Answers
object pauses at the each way points for a vary short time 1 Answer