Lerp & jumping with sin/cos

I’ve just started messing around with moving cubes on a plane in response to keyboard input. I’m trying to keep things simple with no physics, movement of 1 metre/unit at a time etc. Think Frogger :wink:

In Update(), I’m calling GetKeyDown and if an arrow key is pressed, calculating a new Vector3 for the target position and then calling Lerp.

transform.position = Vector3.Lerp (transform.position, target, (Time.time - startTime) / duration);

This works well and my cube slides in the desired direction.

I now want to make the cube “hop” in the air by applying a sin offset to my transform.position.y, but just can’t figure it out. I think I need to track my movement lerp value in either X (for left/right movement) or Z (for north/south movement). From that, I somehow need to multiply by sin or cos to get the Y offset, then apply it to the transform.

Any thoughts or two-line code samples appreciated. I do intend to look at CharacterController tonight btw.

In real trig (the kind sin/cos uses) 0 degrees faces right, goes counter-clockwise, using radians (PI radians is 180 degrees.) Sin is the y-coord, and cos is the x-coord, both between from -1 and 1. So, as you rotate from 0 to PI radians, sin goes from 0 to 1 to 0. This is regular high-school math, or see Wikipedia. It’s a lot easier to tweak if you can visualize it.

The standard way to hop with sin (yes, real hops do make a sin wave) is: y=HT*sin(A) where A goes from 0 to PI. Since you already have a Lerp value going from 0 to 1, you just need to scale it:

y = jumpHT * Mathf.Sin( Mathf.PI * ((Time.time - startTime)/duration) );
                                   ^^^^^ your old lerp value    ^^^^^