hey all,
i was wondering if someone knows how to move an object in a projectile motion from point A to point B, by applying a force to the object with the gravity force on?
thanks in advance
hey all,
i was wondering if someone knows how to move an object in a projectile motion from point A to point B, by applying a force to the object with the gravity force on?
thanks in advance
Have you tried the FPS Tutorial? check the missile chapter
Do you have a known position where the projectile has to land, or do you just want to send it off according to where the player aims?
i have both, the starting and the ending point
the missile chapter creates a missile and shoots it wherever the user is looking, and the arch (or projictile motion) happen naturally because of gravity. What i want to do is to affect a Rigidbody, which is at a starting point (p1), with a force in a certain angle, so that it will hit the target, which is at the ending point (p2).
the easiest thing to do is say:
var forceMagnitude : float = 20.0;
obj.rigidbody.AddForce(forceMagnitude * (p2-p1).normalized);
but the problem with that code is that the object will fall down before it hit the target, due to gravity. So i’m just wondering if there is a way to add a Y value or something to the force so that the object would hit the target, and still make an arch?
the only force present in projectile motion is 1-gravity and 2-air resistence(usually ignored)
so AddForce is wrong in principle.
float initialSpeed = 5.0f;
// p1 and p2 are on the same horizontal plane
Vector3 p1;
Vector3 p2;
float range = (p2 - p1).magnitude;
float gravity = 9.8f;
// calculate shooting angle using the equation:
// equation : R = (v0 squared) * sin(2@) / g
// where :
// R : range, v0 : initial shooting speed of projectile,
// @ : shooting angle theta (the @ sign looks like
// theta, no! ... hehe),
// g : gravitational constant on earth (welcome to our
// planet, or r we developers always on another planet ...lol !!)
float angle = Mathf.Asin(gravity * range / (initialSpeed * initialSpeed)) * 1/2;
Vector3 horizontalDirection = (p2 - p1).normalized;
Vector3 verticalDirection = Vector3.up;
Vector3 initialVelocityHorizontal = initialSpeed * Mathf.Cos(angle) * horizontalDirection;
Vector3 initialVelocityVertical = initialSpeed * Mathf.Sin(angle) * verticalDirection;
Vector3 initialVelocity = initialVelocityHorizontal + initialVelocityVertical;
set the rigid body’s initial velocity to initialVelocity, enable gravity on it and give it a spin
cheers…