- Home /
Vector3.Slerp - is there a way to force an arc?
Some background: I have a parent object, which rotates to face an object that moves based on the mouse location, and a child object that represents their attack. The child object is disabled by default, but, when enabled (via clicking, right now), it slerps between an origin and end point; this provides pretty much exactly the dynamic feeling I wanted from melee combat, rather than 'they're in Zone X, they get hit, play the animation and deduct HP.'
Unfortunately, though, when the parent object's y-rotation is between about 101.3 and 281.3, the attack arcs the wrong direction - it goes from the origin to the goal by passing behind the player, instead of in front, more or less. In effect, it takes the wrong way to the right place, rendering the attack unreliable.
So: Is there a way to force the Vector3.Slerp arc to move in a particular direction, or something to modify the Vector3.Slerp with to make it take the 'negative' arc to its destination? I've tried everything I could think of, but I'm stumped.
Here's the code on the slerping object, for reference (not sure why it won't code-format the headers):
using UnityEngine; using System.Collections;
public class RightPunchControl : MonoBehaviour {
public Transform origin;
public Transform goal;
public float interval;
private float starttime;
// Use this for initialization
void OnEnable () //Putting starttime in this position seems to enable resetting effect.
{
starttime = Time.time;
}
// Update is called once per frame
void Update () {
Vector3 center = (origin.position + goal.position) * 0.5F;
center -= new Vector3(0, 0, 0);
Vector3 riseRelCenter = origin.position - center;
Vector3 setRelCenter = goal.position - center;
float fracComplete = (Time.time - starttime) / interval;
transform.position = Vector3.Slerp(riseRelCenter, setRelCenter, fracComplete);
transform.position += center;
if (transform.position == goal.position)
{
transform.position = origin.position;
gameObject.SetActive(false);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613165313im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Moving player in an arc, from startPoint to endPoint 2 Answers
Physic Collider Test Rotation 0 Answers
Moving the player in a ballistic trajectory in 3D 0 Answers
arc object's position using sin 1 Answer