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

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

enter image description here

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() {


     if (Input.GetKeyDown(KeyCode.Space))


 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 readonly Vector3 initialVelocity;
     public readonly float timeToTarget;

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

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

  • \$\begingroup\$ Please use some sort of screen cature software, it's hard to tell what's happening on the gif. Here's my recommendation \$\endgroup\$
    – Bálint
    May 31 '19 at 13:57
  • \$\begingroup\$ 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 \$\endgroup\$
    – h00man13
    May 31 '19 at 14:41

That happens, because the force/speed wasn't enough to reach the specified point. You need to somehow detect this case and do something.

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")

  • \$\begingroup\$ you know my main concern is about the curve line.i'm wondering why it will be invisible when i move the target above the starting point(and btw the whole project is just a singel c# script that i uploaded nothing esle) \$\endgroup\$
    – h00man13
    May 31 '19 at 15:48
  • \$\begingroup\$ @h00man13 As I stated in my answer, it will disappear, because there is no way to reach that spot with the specified speed. \$\endgroup\$
    – Bálint
    May 31 '19 at 15:57
  • \$\begingroup\$ so do you think there is any solution ?right now the only variable responsible for projectile movement speed is "gravity".and by changing it' value the errors still exist \$\endgroup\$
    – h00man13
    May 31 '19 at 16:17
  • \$\begingroup\$ @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 \$\endgroup\$
    – Bálint
    May 31 '19 at 16:35
  • \$\begingroup\$ i found something. under the video tutorial i saw one of the comments . he says "I would call Mathf.Abs() on every math inside MAthf.Sqrt() and then it can also work if target is lower than ball :) " i'm not sure what he says by that? \$\endgroup\$
    – h00man13
    Jun 1 '19 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.