# help with shooting projectile along a curve path

so i been trying to find a way to shoot projectiles along a curve path . and i found this tutorial on youtube which was really good.

i also uploaded the project in case you wanted to check it out. here

but there is problem with this script .if i move the target object somewhere above the starting point the curved path will be diapered and i get couple of errors.in fact when ever the path is about to become a straight line the errors will pop up . here is the screenshot of errors

and here is a gif file to show when the problem shows up

and finally here is the code responsible for calculating the curve path a lunching projectiles.

 public Transform startPoint;
public Transform target;
public int resolution = 30;
public float curveHight = 25;
public float gravity = -18;

public Rigidbody bullet;
public LineRenderer lineRenderer;
void Start()
{

}

void Update() {

DrawPath();

if (Input.GetKeyDown(KeyCode.Space))
{
Launch();
}

}

void Launch()
{

Rigidbody clone = Instantiate(bullet, startPoint.position, Quaternion.identity);

Physics.gravity = Vector3.up * gravity;

clone.velocity = CalculateLaunchData().initialVelocity;
}

LaunchData CalculateLaunchData()
{
float displacementY = target.position.y - startPoint.position.y;
Vector3 displacementXZ = new Vector3(target.position.x - startPoint.position.x, 0, target.position.z - startPoint.position.z);
float time = Mathf.Sqrt(-2 * curveHight / gravity) + Mathf.Sqrt(2 * (displacementY - curveHight) / gravity);
Vector3 velocityY = Vector3.up * Mathf.Sqrt(-2 * gravity * curveHight);
Vector3 velocityXZ = displacementXZ / time;

return new LaunchData(velocityXZ + velocityY * -Mathf.Sign(gravity), time);
}

void DrawPath()
{
LaunchData launchData = CalculateLaunchData();
Vector3 previousDrawPoint = startPoint.position;

for (int i = 1; i <= resolution; i++)
{
float simulationTime = i / (float)resolution * launchData.timeToTarget;
Vector3 displacement = launchData.initialVelocity * simulationTime + Vector3.up * gravity * simulationTime * simulationTime / 2f;
Vector3 drawPoint = startPoint.position + displacement;
Debug.DrawLine(previousDrawPoint, drawPoint, Color.green);

previousDrawPoint = drawPoint;

lineRenderer.positionCount = resolution;

lineRenderer.SetPosition(i - 1, drawPoint);
}
}

struct LaunchData
{

public LaunchData(Vector3 initialVelocity, float timeToTarget)
{
this.initialVelocity = initialVelocity;
this.timeToTarget = timeToTarget;
} }}


so,any idea what might be causing the the problem?

• Please use some sort of screen cature software, it's hard to tell what's happening on the gif. Here's my recommendation – Bálint May 31 '19 at 13:57
• sure. i updated the post with a new gif file. you can also download the project file it's rather small and open the scene to check the problem – h00man13 May 31 '19 at 14:41

There are a couple of Debug.Assert calls somewhere in your code (you didn't post that part). Those cause the errors. Remove them and replace it, so that the player gets alerted of them (like an in-game message saying "the ball can't reach that spot")
• @h00man13 curveHight in your code should represent the maximum height of the curve. You either increase it dynamically, so that it's always larger than or equal to the difference between the starting and ending point's y coordinate or you use a different equation to calculate the starting speed. the current one uses the distance the ball needs to travel and the maximum height it must (and can) reach, but you need one, that doesn't have a height limit. This one gets to the point in a specified amount of time – Bálint May 31 '19 at 16:35