- Home /
Artillery angle of reach prediction
Hello! I'm trying to predict the angle of reach for my artillery gun for an RTS im making, however i've come across a problem. I'm not sure why, but the angle is always wrong, it either shoots short or too far. Here is my code:
float angle = 0.5f * (Mathf.Acos ((-Physics.gravity.y * Vector3.Distance(muzzle.transform.position, target)) / (velocity * velocity)) * Mathf.Rad2Deg);
Debug.Log("Angle =" + angle * Mathf.Rad2Deg);
gameObject.transform.rotation = Quaternion.Euler(0, 0, (angle * Mathf.Rad2Deg));
if(Input.GetMouseButtonUp(0)){
Instantiate(bullet, muzzle.transform.position, muzzle.transform.rotation);
}
I am attempting to use the steep angle of reach formula from the projectile motion wiki page. Thanks in advance!
Answer by kylecat6330 · Jan 28 at 12:28 AM
Alright if you are just trying to make it look accurate here is what you can do.
So you have your two points, the gun position and the target position. You can set the gun to fire at whatever angle you think is best. Then using the angle you get a position some distance out from the gun.
curvePoint = gun.position + Vector3(Cos(angleInRadians), Sin(angleInRadians), Zdirection?).normalize * someDistance
Once you have that point you can make some lerps.
upwardMotionPoint = Vector3.Lerp(gun.position, curvePoint, speed * Time.deltaTime);
DownwardMotionPoint = Vector3.Lerp(curvePoint, target.position, speed * Time.deltaTime);
projectile.position = Vector3.Lerp(upwardMotionPoint, DownwardMotionPoint, speed * Time.deltaTime);
Note that this is sorta pseudo code or just lazy code. So don't expect to copy it exactly and it works. It is just to give you general idea of a way to do it.
You can make it look good just may take some adjusting. It may do well to make an empty game object to be able to set as the curvePoint just make sure you can keep it in line with the direction of fire.
Answer by kylecat6330 · Jan 27 at 10:23 PM
I may not be following this correctly, but from what I am seeing you have "angle" in degrees, and then you try to convert it into degrees again when you preform your rotation.
If i do not convert it into degrees again, the angle is completely off
How far off and in what way? Is it in the opposite direction? Way too far? Way too close?
In the direction of the target, but it always overshoots and barely adjusts the angle.
Your answer
![](https://koobas.hobune.stream/wayback/20220613052237im_/https://answers.unity.com/themes/thub/images/avi.jpg)