Question by
CreativeBrainstorm · Aug 02, 2018 at 09:13 PM ·
linerenderertrajectorylaunchcalculatecalculations
Trajectory line throwing downward
Hello,
i've got a ball which i adjust force to. It's very angry birds like. The ball goes in the opposite direction you drag the mouse. (screenshots attached)
For this i want to draw a trajectory line and everything works fine. But when I want to shoot the ball downwards the wrong coordinates for the points of the LineRenderer are calculated.
Maybe you can help me and tell me what I'm doing wrong!
private LineRenderer lr;
public float velocity, angle;
public int resolution = 10;
private float g;
private float radianAngle;
public void RenderArc(float v, float a)
{
velocity = v;
angle = a;
lr.positionCount = resolution + 1;
lr.SetPositions(CalculateArcArray());
}
Vector3[] CalculateArcArray()
{
Vector3[] arcArray = new Vector3[resolution + 1];
radianAngle = Mathf.Deg2Rad * angle;
float maxDistance = (velocity * velocity * Mathf.Sin(2 * radianAngle)) / g;
for (int i = 0; i <= resolution; i++)
{
float t = (float) i / (float) resolution;
arcArray[i] = CalculateArcPoint(t, maxDistance);
}
return arcArray;
}
Vector3 CalculateArcPoint(float t, float maxDistance)
{
float x = t * maxDistance;
float y = transform.position.y + x * Mathf.Tan(radianAngle) -
(g * x * x) / (2 * velocity * velocity * Mathf.Cos(radianAngle) * Mathf.Cos(radianAngle));
x += transform.position.x;
return new Vector3(x, y, 0.0f);
}
screen-1.png
(18.8 kB)
screen-3.png
(19.1 kB)
Comment