- Home /
Precise projectile launching in 3d world
This is an easy concept yet hard to explain, but I'll try my best.
I'm trying to make a ball be thrown from pretty much any location on a field (300 by 300) from 6 ft high to the point at (0,3,0). It needs to be at a constant speed and follow a gradual arc. (It's a baseball game, so it needs to mimic a players throw to home).
I tried using Vector3.MoveTowards but that attempts to go in a straight line and the ball ends up rolling to the target because gravity pulls it down.
As of now, I'm using a method which calculates an offset direction (x and z) and adds a force in that (x,z) direction and use a constant multiplied by the distance to get the y.
Vector3 dir = new Vector3(0, 3, 0) - transform.position; //
float dist = Mathf.Sqrt((dir.x * dir.x) + (dir.z * dir.z));
float dx = dir.x * 20;
float dz = dir.z * 20;
Vector3 throwforce = new Vector3(dx, dist * 3f, dz);
rigidbody.AddForce(throwforce);
This method, however is normally sufficient at getting the ball kind of where it needs to be, but at variant heights and speeds, as a Vector of (1,1,0) will produce a different speed than (.5,1,.5)
So how can I get the ball to travel at a set speed all the time (or at least start at a set speed), travel in a certain direction (x,z), and arc to its path?
Your answer
Follow this Question
Related Questions
help with making enemy projectile fly at player at one speed 0 Answers
Shotting Or thrwoing Object wtih Vector3.MoveTowards 1 Answer
[VR - Vive] Throwing shurikens with modified speed 0 Answers
Increasing the speed of an object when the scale is decreased and vice versa 0 Answers
Should my ball gameobject have a rigidbody component? 0 Answers