- Home /
GameObject jumps and spins towards destination
Hi, game developers! I need your help again. I want to make a gameObject jump and rotate to a location. So far I have the basic idea down but don't know how to complete it. Here's my sketch to explain it clearer:
I broke it down to three parts:
Lerp: I want to Lerp the cube from Vector3(8,1,0) to Vector3(1,6,0) in 3 seconds flat.
Height: Depending on distance of the start and finish points, I want to calculate the peak height of the cube, to make it a believable jump.
Rotate: As the cube jumps, it will rotate towards the destination, like doing a front flip. I just need a curve formula to simulate the force and gravity of the jump without the use of physics. Also, the cube MUST start and finish with Quaternion.identity.
Getting the cube to stop at Quaternion.identity I think is the hardest part. In a way it must look seamless, like landing your feet in the same position started of a front flip.
Here is a mock of the function so far. I just need help with it:
// JavaScript
var jumpTime : float;
function jumpTo(destination : Vector3, sec : float, rotate : boolean) {
// 1). Lerp X and Y axis:
jumpTime += Time.deltaTime / sec; // Not sure if this is correct way to calculate seconds.
var lerped = Vector3.Lerp(transform.position, destination, jumpTime);
transform.position.x = lerped.x;
transform.position.y = lerped.y;
// 2). Calculate curve of height depending on distance... (no idea?)
if (rotate) {
// 3). Rotate cube to the direction of destination:
// Challenges: Don't know how to control speed of rotation and rotate to Quaternion.identity as it comes to its destination.
transform.Rotate(destination);
}
// Clear variables when reached destination:
if (transform.position == destination) {
jumpTime = 0;
}
}
Any thoughts, comments or ideas would be helpful. I really appreciate the help of the Unity community!
Answer by robertbu · Jul 23, 2014 at 03:11 PM
Here is a bit of demonstration code that implements your functionality. From watching it work, I would suggest varying the time based on the distance to get something that is a bit more natural. Note I use Mathf.Sin() to calculate a height and Quaternion.AngleAxis() to do the flip. Attach this script to a cube in a new scene. Hit the spacebar to do the jump/flip.
#pragma strict
var heightVsDistanceFactor = 0.5;
var minDistForFlip = 3.0;
private var jumping = false;
function Update () {
if (!jumping && Input.GetKeyDown(KeyCode.Space)) {
var v : Vector3 = Random.insideUnitCircle * 7.0;
v.x = Mathf.Round(v.x);
v.y = Mathf.Round(v.y);
v.z = v.y;
v.y = 0.0;
DoTheJump(v, 3.0);
}
}
function DoTheJump(dest : Vector3, time : float) {
jumping = true;
var timer = 0.0;
var start = transform.position;
var dist = (start-dest).magnitude;
var maxHeight = dist * heightVsDistanceFactor;
var axis : Vector3;
var doSpin = ( dist >= minDistForFlip);
if (doSpin) {
axis = Vector3.Cross(start - dest, Vector3.up);
}
while (timer <= time) {
var vT = Vector3.Lerp(start, dest, timer/time);
vT.y = Mathf.Sin(Mathf.PI * timer/time) * maxHeight;
transform.position = vT;
if (doSpin) {
transform.rotation = Quaternion.AngleAxis(360.0 * timer/time, axis);
}
timer += Time.deltaTime;
yield;
}
transform.rotation = Quaternion.identity;
transform.position = dest;
jumping = false;
}
@robertbu I would bow down and kiss your feet if you were here. THAN$$anonymous$$S!!