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?
2 Answers
2Using @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);
}
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.
Sure thing. You're in for some potentially complicated math working with quaternions, so good luck :)
– Julien-LyngeIt's actually not that complicated but you only describe how to actually slerp between two rotations, how do you apply the smooth damp effect?
– JGriffithYou 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/Mathf.SmoothDamp.html However you do it, you take the output of your damping function and plug that into the slerp as the last argument.
– Julien-Lynge
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..
– springwater