- Home /
Lerp jump using input won't convert to world space
Hi there,
When I jump in a direction, the character jumps relative to itself, rather than the camera I'm looking through. Explained properly below:
My jump function applies jump force like so:
target.Translate(Vector3.Lerp(from, to, elapsed / time) * DeltaTime);
Which is triggered when I press the jump button, however the character jumps according to its local direction, of the camera based directional input. Those values are all fed from the code below, 'from' and 'to' are sent from the MovementInput Vector, Elapsed is calculated when the jump function begins and Time is how long the jump should take. DeltaTime is just a variable for Time.deltaTime. The jump works fine when 'from' and 'to' are Vector3.zero (Making the player jump straight up), but I can't get the directional jump working.
//'test' is the code I tried 'TransformDirection' on, and just replaced 'MovementInput' with it
Vector3 test = Camera.main.transform.TransformDirection(MovementInput);
test.y = 0;
Jump(MovementInput.normalized * 10f, MovementInput.normalized * 10f, jumpTime);
If I'm holding 'right', the character runs to the right of the screen, but if I press jump and the character jumps to its right (toward the camera) rather than jumping to the right of the screen (its forward). I've tried converting the MovementInput (Which is just horizontal and vertical input on the x and z axis) to the camera's transform direction, but that makes the character jump in directions I can't even explain.
I assume I must be doing something wrong with the TransformDirection. I use the exact same Vector3 to move the character according to the camera's direction:
target.Translate(MovementInput * movementDistance, Camera.main.transform);
Any and all help appreciated.
Ah my apologies, that's probably not very clear. Those values are all fed when I run the jump function from the input code I'd provided, I just reworded the question so that it makes more sense. Thanks for pointing that out.
but what we need to know is how those values are being reached.. the problem is mainly based in how you are reaching the value used for "to"
Hi Seth, apologies again, I thought I cleared that up but obviously didn't! The '$$anonymous$$ovementInput' X and Z Vector is set by getting the horizontal and vertical axis, then they're sent in the 'Jump' function in the block of code above. However $$anonymous$$ike is spot on, and I can't believe I didn't read the 'Translate' page properly! Thanks for your help.
Answer by whydoidoit · Jul 30, 2012 at 10:19 AM
Translate works in local coordinates unless you specify Space.world as the second parameter.
Spot on, ashamed that I missed that! Thanks $$anonymous$$ike.