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!

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.

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.