- Home /
Question by
unity_bibIFrr_mkm9jw · Nov 23, 2020 at 01:27 PM ·
trajectoryprediction
Trajectory prediction
Hello.
I'm testing trajectory prediction, but i'm facing a little issue.
Let's assume the following capture :
With red dots : current trajectory of my projectiles. The green area is the Line Renderer for showing the prediction to the user. The blacks ones : what i'm expecting.
How can I adjust my line renderer to obtain the expected result ?
Here is my code that manage the prediction:
Vector2 velocity = ShootForce(Input.mousePosition) * Time.fixedDeltaTime / ballBody.mass;
Vector2 mPos = new Vector2(transform.position.x, transform.position.y);
print(mPos);
Vector2[] trajectory = CalculatePath(mbody, mPos, velocity, 500);
lineRenderer.positionCount = trajectory.Length;
Vector3[] positions = new Vector3[trajectory.Length];
for (int i = 0; i < trajectory.Length; i++)
{
positions[i] = trajectory[i];
}
lineRenderer.SetPositions(positions);
Vector3 targetPos = positions.Last() + (transform.position - positions.Last()) / 1.09f;
Vector3 diff = targetPos - transform.position;
diff.Normalize();
float angle = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, angle);
And the associated Vector2 function:
public Vector2[] CalculatePath(Rigidbody2D rigidbody, Vector2 pos, Vector2 velocity, int steps)
{
Vector2[] results = new Vector2[steps];
float timeStep = Time.fixedDeltaTime / Physics2D.velocityIterations;
Vector2 gravityAccel = Physics2D.gravity * rigidbody.gravityScale * timeStep * timeStep;
float drag = 1f - timeStep * rigidbody.drag;
Vector2 moveStep = velocity * timeStep;
print("Move Step: " + moveStep);
for (int i = 0; i < steps; i++)
{
moveStep += gravityAccel;
moveStep *= drag;
pos += moveStep;
results[i] = pos;
}
return results;
}
Any help would be appreciated :)
capture.png
(33.7 kB)
Comment