|
I have a turret that fires cannonballs. When the player presses the appropriate button, this line of code is ran: projectile.rigidbody.AddForce(transform.forward * 2000, ForceMode.Impulse); Here's the problem - the cannonball fires just fine, but it takes a very long time before it begins to descend. This causes a huge problem since the cannonballs always fly over the enemy instead of landing near them. I basically want the cannonball to fall faster, but follow a clean arc. Any ideas on how to do this properly?
(comments are locked)
|
|
You must lower the elevation angle. The greatest distance is reached with a 45 degrees elevation, but the ball takes a lot to fall. You can experiment with 30, 20, 15 degrees - but the lower the angle, the higher the force you must apply for the cannon ball to reach the target. It may be easier to set the cannon ball velocity instead of the force applied.
function BallisticVel(target: Transform, angle: float): Vector3 {
var dir = target.position - transform.position; // get target direction
var h = dir.y; // get height difference
dir.y = 0; // retain only the horizontal direction
var dist = dir.magnitude ; // get horizontal distance
var a = angle * Mathf.Deg2Rad; // convert angle to radians
dir.y = dist * Mathf.Tan(a); // set dir to the elevation angle
dist += h / Mathf.Tan(a); // correct for small height differences
// calculate the velocity magnitude
var vel = Mathf.Sqrt(dist * Physics.gravity.magnitude / Mathf.Sin(2 * a));
return vel * dir.normalized;
}
var myTarget: Transform; // drag the target here
var cannonball: GameObject; // drag the cannonball prefab here
var shootAngle: float = 30; // elevation angle
function Update(){
if (Input.GetKeyDown("b")){ // press b to shoot
var ball: GameObject = Instantiate(cannonball, transform.position, Quaternion.identity);
ball.rigidbody.velocity = BallisticVel(myTarget, shootAngle);
Destroy(ball, 10);
}
}
To test it, create an empty object about 2 units above the ground and attach this script. Make sure the cannonball don't touch any collider when instantiated, or it will not reach the target. Drag the target and the cannonball prefab to the corresponding variables and press b to shoot. Drag is not taken into account. When drag is set to for example to .25, the calculated velocity is not enough to reach the target. Any idea on how to take the drag into account and still make the cannonball land on the target position? Btw. Works great if drag=0 ! Thnx.
May 29 '12 at 10:53 PM
Menace
Aldo, Drag is not taken into account. Any idea on what to do if drag is set to for example .25 and still get the right velocity for the projected target distance?
May 29 '12 at 11:11 PM
Menace
This function ignores drag. If we knew the drag equation, maybe it could be taken into account - but I haven't the slightest clue about how it's calculated. In real world physics, it's proportional to the square of the velocity - but this doesn't happen with Unity's drag.
May 30 '12 at 12:28 AM
aldonaletto
Compensation should do the trick. With the right compensation taking the rigidbody.drag into account the cannonball should land on the target again. Any idea?
May 30 '12 at 07:09 AM
Menace
It's generous of Aldo to provide a ballistic formula -- I've noticed many, many people starting out with Unity ask tis question -- and it always leads to vast confusion!
May 30 '12 at 08:28 AM
Fattie
(comments are locked)
|
|
Hey guys I stumbled over this nice thread - which covers exactly what I need, unfortunately everybody is coding in unity script... i tried to translate the code to c# , but I must have made a mistake. this is my translation: public Vector3 BallisticVel(Transform target, float angle){ Vector3 dir = target.position - mySelf.transform.position; float h = dir.y; dir.y =0f; float dist = dir.magnitude; float a = angle * Mathf.Deg2Rad; dir.y = dist * Mathf.Tan(a); dist += h / Mathf.Tan(a); float vel = Mathf.Sqrt(dist*Physics.gravity.magnitude / Mathf.Sin(2 * a)); return vel*dir.normalized; } here I "call" the function : GameObject ball = Instantiate(ArrowObject,_ArrowObjectPos.position,Quaternion.identity)as GameObject; ball.rigidbody.velocity = BallisticVel(enemyObject, shootangle); mySelf is the gameobject the script is attached to ;) and this is the errormessage I get : I am terrible with all those Mathf. stuff. I was able to use the script like this : public Vector3 BallisticVel(Transform target){ Vector3 dir = target.position - mySelf.transform.position; float h = dir.y; dir.y =0; float dist = dir.magnitude; dir.y = dist; dist += h; float vel = Mathf.Sqrt(dist*Physics.gravity.magnitude); return vel*dir.normalized; } but like this - i shoot the cannon way too slow and a bit too far. Any suggestions?
(comments are locked)
|
|
You can add a script to the cannon ball prefabs that adds a constant downward force. By adjusting the amount of the downward force, you should be able to adjust the arc that the cannon ball makes.
(comments are locked)
|

Important ... You absolutely have to be using real sized models in Unity. It's a must, or nothing will work. Your cannonball must be the actual size of a real cannonball, your people must be actually 2m high, everything must be actual size. Then, all masses must be realistic in the real world. Hope it helps!