- Home /
Projectile trajectory
Hi,
Im new to unity and im trying to make a game quite simple. i got a tower in the middle of the screen on which there a cannon. i need to calculate the trajectory of a cannon ball which i want to land where i click on the field (like a RTS view or D2 view).
i tried :
Vector3 Calculate(Vector3 p1, Vector3 p2) { float initialSpeed = 10.0f; float range = (p2 - p1).magnitude; float gravity = 9.8f;
float angle = 0.5f * (Mathf.Asin(gravity * range / (initialSpeed * initialSpeed)) ;
Vector3 horizontalDirection = (p2 - p1).normalized;
Vector3 verticalDirection = Vector3.up;
Vector3 initialVelocityHorizontal = initialSpeed * Mathf.Cos(angle) * horizontalDirection;
Vector3 initialVelocityVertical = initialSpeed * Mathf.Sin(angle) * verticalDirection;
Vector3 initialVelocity = initialVelocityHorizontal + initialVelocityVertical;
return initialVelocity;
}
void FireBullet() { GameObject Bullet = Instantiate(CannonBall, transform.position, transform.rotation) as GameObject;
RaycastHit hit = new RaycastHit();
Ray ray = Camera.allCameras[0].ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 10000))
{
float Vx, Vz;
Vx = (hit.point.x - transform.position.x) / 0.11111f;
Vz = (hit.point.z - transform.position.z) * 9.0003f;
Debug.Log(Vx + " :: " + Vz);
Debug.Log(hit.point.x.ToString() + " :: " + hit.point.z.ToString());
Bullet.rigidbody.velocity = Calculate(transform.position, hit.point); ;
}
}
but i'm getting angle always = to NaN... so nothing work at all
Hope you can help
Let me get this straight, you want to see a trajectory-like thing spurting out from the tower/cannon so that you can see where the cannon ball will eventually land. Is this correct?
No i only want to know what force or what velocity i have to apply to the ball so it will land where i clicked.
Answer by yosh 1 · Feb 25, 2011 at 01:42 AM
i think ur question same like my question..
http://answers.unity3d.com/questions/43083/lobbing-to-certain-position
to get lobing certain position is worked now.. but its doesnt have physic. maybe u can edit my code..