Projectile Motion arc with Rigidbody 2D,Projectile motion with 2D physics.
Hey folks, i am having some problems with the 2D physics rn and was wondering if someone had a similar problem and could help.
i am making an parabolic motion simulator where the user gives the angle and initial velocity and then they see an canon shooting the bullet with the infos about the X and Y position alongside a graph showing the equation. But when i try to compare the results of the equation and the real results ingame they differ by around 0.1 ~ 0.2 and i can't figure out what's wrong
The Code for the equation`
var g = -Physics2D.gravity.y;
var height = 0;
var initialVelocityX = (player.velocity * Mathf.Cos(Mathf.Deg2Rad * player.angle));
var initialVelocityY = (player.velocity * Mathf.Sin(Mathf.Deg2Rad * player.angle));
var time = (initialVelocityY + Mathf.Sqrt(Mathf.Pow(initialVelocityY,2) + (2 *g * height) ) ) / g;
Vector2 pos = Vector2.zero;
pos.x = initialVelocityX * time;
pos.y = (initialVelocityY * time) - (g * Mathf.Pow(time, 2) * 0.5f);
Debug.Log("Total Time (Equation): " + time);
Debug.Log("X position:" + pos.x);`
and the launch script is just buInst.rb.velocity = new Vector2(Mathf.Cos(Mathf.Deg2Rad * angle) * (velocity), Mathf.Sin(Mathf.Deg2Rad * angle) * (velocity));
the distance i print on the game screen is the distance that the bullet travels by transform.position.x - initialposition.x
Thanks in advance if someone can help ^ ^
Your answer
![](https://koobas.hobune.stream/wayback/20220612234754im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rigidbody projectile misses collider 1 Answer
Using both RB Velocity and AddForce? 1 Answer
Rigidbody2D falls slowly with MovePosition? 1 Answer
How to make Smooth Gravity Transitions 0 Answers