- Home /
Throw Grenade With Definite Speed And on Definite Distance
Hello!
I am trying to build a function that simulates grenade throwing. Grenade is a rigidbody.
I found great function to "throw" rigidbody - Rigidbody.AddForce. Depending on Vector3 value it launches the rigidbody with some angle. But I cant figure out how I can calculate this Vector3 value for AddForce if I need this rigidbody to fly on a definite distance with a definite speed. Can you help with formulas or maybe with other way to simulate this motion?
Answer by Vitamin · Feb 18, 2015 at 11:44 AM
I found what I needed here , but I had to rework it into my needs.
So if you want to "throw" grenade with definite (horizontal) speed and on a definite distance you can use something like this.
float maxDistance = 100; //distance for grenade to to reach
float hSpeed = 100; // horizontal speed
float g = Physics.gravity.magnitude; // get the gravity value
//then we calculating other variables to set our "fly"
float totalTime = maxDistance/hSpeed; //time of fly
float vSpeed = (totalTime*g)/2; //vertical speed
//then we "launch" our grenade
newGrenade.velocity = new Vector3(fTransform.forward.x * hSpeed, vSpeed, fTransform.forward.z*hSpeed); // launch the projectile!
Your answer
Follow this Question
Related Questions
How to make my bullets spread when fired 3 Answers
rolling ball movement on different axis 0 Answers
Trajectory problem with addforce coordinates 1 Answer
AddForce at Local ?? 2 Answers