Dynamic Jump spot
Posted
by
Pasquale Sada
on Game Development
See other posts from Game Development
or by Pasquale Sada
Published on 2012-09-29T15:41:40Z
Indexed on
2012/09/30
21:51 UTC
Read the original article
Hit count: 252
physics
I have an initial velocity V(Vx,Vy,VZ) and a spot where he stands still at S(Sx,Sy,Sz). What I'm trying to achieve is a jump on a spot E(Ex,Ey,Ez) where you have clicked on(only lower or higher spot, because I've in place a simple steering behavior for even terrains). There are no obstacle around. I've implemented a formula that can make him jump in a precise way on a spot but you need to declare an angle: the problem arise when the selected spot is straight above your head. It' pretty lame that the char hang there and can reach a thing that is 1cm above is head. I'll share the code I'm using:
Vector3 dir = target - transform.position; // get target direction
float h = dir.y; // get height difference
dir.y = 0; // retain only the horizontal direction
float dist = dir.magnitude ; // get horizontal distance
float a = angle * Mathf.Deg2Rad; // convert angle to radians
dir.y = dist * Mathf.Tan(a); // set dir to the elevation angle
dist += h / Mathf.Tan(a); // correct for small height differences
// calculate the velocity magnitude
float vel = Mathf.Sqrt(dist * Physics.gravity.magnitude / Mathf.Sin(2 *a));
return vel * dir.normalized;
© Game Development or respective owner