hey guys, 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. here
(https://www.youtube.com/watch?v=IvT8hjy6q4o)
i also uploaded the project in case you wanted to check it out. here
(http://www.mediafire.com/file/z5zfqbcnpznnbsh/arc+luncher.unitypackage)
but there is problem with this script .if i move the target object somewhere above the starting point the curved path will be disappeared 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
(https://ibb.co/vZ5JTZ2)
and here is a gif file to show when the problem shows up (https://i.stack.imgur.com/ibCD9.gif)
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 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?
↧