- Home /
Slerp and Easing: Attempting to fire a projectile along an arc
I want a character to launch a missile at a foe, and I'm using a small missile mesh to get from one to the other. Now, if the character isn't facing the foe directly, I want the missile to fire out straight away from the character a bit before arcing toward the foe.
My original thought was to have a waypoint in between, so that the missile first moves via Lerp or MoveTowards until it reaches the waypoint, then via Slerp to the foe. However, the result is that once the missile reaches the waypoint, it turns sharply and moves in a slight arc from then on. It looks like it bounced off an invisible wall.
I've looked for some kind of simple-to-use easing function to bridge that waypoint, but I'm not finding much. I'm hoping there's a simpler way to do this then spelling out a bunch of math for how to handle the waypoint transition.
Currently, my Slerp command looks like this:
transform.position = Vector3.Slerp(transform.position,target.transform.position,mySpeed/distance * .025);
Any ideas on how to make something like this work?
Thanks in advance!
Answer by jbalzer · Mar 03, 2011 at 11:22 PM
Well, I think I came up with something that seems to work ok. The following is called via the FixedUpdate:
angleAmount : float = 0.01; // this is actually set whenever the shootWaypoint is reached
if (shootWaypointReached) {
var direction:Vector3 = target.transform.position - transform.position;
var targetRotation = Quaternion.LookRotation(direction);
var angleDifference = Quaternion.Angle(transform.rotation, targetRotation);
if (angleDifference > 0){
angleAmount = Mathf.Lerp(angleAmount,.5,.005);
fireObject.transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, angleAmount);
}
transform.position += (transform.forward * speed);
}
However, I'm open to any wiser advice than this.
Answer by Patyrn · Mar 04, 2011 at 12:05 AM
Might be worth trying this out:
http://unity3d.com/support/documentation/ScriptReference/Vector3.SmoothDamp.html
I haven't actually used this method before, but it's what I was planning on starting with when I write a guided missile.
Answer by tool55 · Mar 04, 2011 at 01:31 AM
I posted this answer for another similar question. Perhaps it will help you:
http://answers.unity3d.com/questions/42653/how-to-control-the-speed-of-look-at/42704#42704
Your answer
Follow this Question
Related Questions
Rotation Ease-In-Out with MAX Rotation Speed 1 Answer
Are there any easing functions built into Unity 5? 1 Answer
Quaternion.Slerp only one axis? 1 Answer
Problem with Lerp/Slerp 1 Answer
Slowing velocity in Rotation? 1 Answer