- Home /
Calculate Height of Trajectory : Unity3d
hello everyone ,
I have done trajectory motion and its working perfectly..
For calculating velocity i have written code like this :
function GetTrajectoryVelocity( startingPosition : Vector3, targetPosition : Vector3, lob : float , gravity : Vector3) : Vector3
{
var physicsTimestep : float = Time.fixedDeltaTime;
var timestepsPerSecond : float = Mathf.Ceil(1f/physicsTimestep);
var n : float = lob * timestepsPerSecond;
var a : Vector3 = physicsTimestep * physicsTimestep * Physics.gravity;
var p : Vector3 = targetPosition;
var s : Vector3 = startingPosition;
var velocity : Vector3 = (s + (((n * n + n) * a) / 2f) - p) * -1 / n;
velocity /= physicsTimestep;
return velocity;
}
The equation for calculating max height is here : Maximum Height
But i to calculate height using this equation ? I am having velocity , angle .
So my problem is how do i make equation in code?
Thanks for your help till now..
Why don't you use the equation h = pow(v sin(θ), 2) / 2g* provided on the site you linked?
Answer by MrVerdoux · Dec 11, 2013 at 08:12 AM
First of all, I don´t think you should recreate the whole physics system, Unity has an amazing built-in physics system, and it´s much more efficient than anything you can come up to.
But, anyway, your question has an answer. I guess you must have an initial velocity, apart form an initial position. You can calculate the angle from that initial velocity:
var initialVelocity : Vector3;
var angle : float;
function Start () {
initialVelocity = new Vector3(1,1,1);
angle = Vector3.Angle(new Vector3(initialVelocity.x,0,initialVelocity.z), initialVelocity);
}
edit: and of course, form that point use the equation pointed by robhuhn, I assumed your problem was with finding the angle.