Hello!
My first post! ![]()
I’m trying to create a simple projectile motion. I’m fairly new to this and I don’t know how to apply all of my calculations to the actual projectile that has been fired. I’d appreciate if someone could point me in the right direction. Oh and btw, I’m a newbie when it comes to programming, here’s the code:
public float theta = 45f; // Angle
public float speed = 10f; // Speed
private float g; // gravity
void Start()
{
//Use unity's gravity setting
g = Physics.gravity.y;
}
void Update()
{
//Initialize velocities
float Vx = speed * (Mathf.Cos(theta * Mathf.Deg2Rad)); //Initial horizontal Velocity /// speed cos theta
float Vy = speed * (Mathf.Sin(theta * Mathf.Deg2Rad)); //Initial vertical Velocity /// speed sin theta
//We know ball begins descent when velocity in Vy = 0 and so we can calculate the time spent in the air
float t = Vy / g; // Time spent in the air
// Total time of flight
float totalT = t * 2;
//Use equation of motion for projectile under constant acceleration
//delta r = ut + 1/2at^2 at each timestep /// r = Vx * 1/2 * a * Totalt
float r = Vx * 0.5f * 0 * Mathf.Pow(totalT, 2);
//How do I apply all of this to my projectile? O.o
GetComponent<Rigidbody2D>().velocity = new Vector2(Vx, Vy);
}