Game Development Reference
In-Depth Information
85.
//Read jump input
86.
if(jump && OnGround()){
87.
//v2^2 - v1^2 = 2as
88.
//v2 is zero (at max height)
89.
//v1^2 = -2as
90.
//v1 = sqrt(-2as)
91.
floatv1=
92.
Mathf.Sqrt(-2
*
Phys-
ics.gravity.y * jumpHeight);
93.
94.
//Use momentum formula p=mv with up as velocity direction
95.
rigidbody.AddForce(
96.
Vector3.up * v1 * rigid-
body.mass,
97.
ForceMode.Impulse);
98.
99.
jump = false;
100.
}
101.
102.
}
103.
104.
//Checks whether the character is on the ground
105.
public bool OnGround(){
106.
//Cast a ray from feet position downwards.
107.
//The length of the ray is 10 cm.
Search WWH ::




Custom Search