What I'm doing:
I'm moving a projectile to it's target along a Bézier curve with one control point.
The projectile moves from Transform
A to Transform
B with the curve created by control's Transform
's position. I'm using this method so that I can change how the projectile's arc or path looks depending on the enemy's position.
The issue:
The projectile travels from A to B successfully, however, my issue is depending on the curve the speed can be inconsistent and an unwanted 'easing' effect can occur.
Here's an example in the image below. Because of control's position, you can see the wirespheres begin to bunch up and smoosh together. As the projectile travels from A to B, it will gradually move slower as the wirespheres become closer together.
Here are the two scripts I am using to accomplish this:
public class QuadraticCurve : MonoBehaviour
{
public Transform A;
public Transform B;
public Transform Control;
public Vector3 evaluate(float t)
{
Vector3 ac = Vector3.Lerp(A.position, Control.position, t);
Vector3 cb = Vector3.Lerp(Control.position, B.position, t);
return Vector3.Lerp(ac, cb, t);
}
private void OnDrawGizmos()
{
if(A == null || B == null || Control == null)
{
return;
}
for (int i = 0; i < 20; i++)
{
Gizmos.DrawWireSphere(evaluate(i / 20f), 0.1f);
}
}
}
public class ProjectileAttack: MonoBehaviour
{
public QuadraticCurve curve;
private IEnumerator MoveTheDart()
{
// Set the target
_curve.B.transform.position = ImpactPosition.position;
while (sampleTime < 1f)
{
sampleTime += Time.deltaTime * newSpeed;
projectile.position = curve.evaluate(sampleTime);
yield return null;
}
}
}
What I need assistance with:
I need my projectile to move along the curve from A to B with a consistent speed regardless of how the curve looks.
Going by the Answer provided by Sam Hocevar in this thread: How to achieve uniform speed of movement on a bezier curve?
I believe v1
= ac & v2
= cb. Would L be a number I pick that would decide how much it moves each frame?(almost like speed)
I imagine Length
is separate from L
. If so, what would Length
be?
Thank you so much to anyone taking the time!
I imagine Length is separate from L. If so, what would Length be?
– PayasoPrince Nov 25 '23 at 23:01length
in the denominator is not a variable, but a function. So it should be read asdivide L by length of vector (t * v1 + v2)
.Also, I am not sure why you've assumed v1 = ac & v2 = cb. These vectors are parts of curve's deriviative, and, if I am reading that correctly, should be:
– Daniil Dubrovsky Nov 27 '23 at 00:30v1 = 2 * A.position - 4 * Control.position + 2 * B.position
, andv2 = -2 * A.position + 2 * Control.position