Home Forums Unity 3D Wall Jump Movement Problem Unity3D Time.deltatime Reply To: Wall Jump Movement Problem Unity3D Time.deltatime

David N
Up
0
Down
::

Well the thing is CharacterController doesn’t have a velocity like the Rigidbody.

If you want to apply a certain movement over a range of time you need to iterate each frame and apply according resulting movement.

Currently your Move is only called exactly once – either with the entire forces – jumps – or with the velocity (after applying Time.deltaTime) but only for a single frame -> barely moves at all.

 

[SerializeField] float slideForce = 3f;

[SerializeField] float wallJumpHeight = 3f;
[SerializeField] float wallJumpRange = 3f;
[SerializeField] float wallJumpGravity = 9.8f;
[SerializeField] float jumpDuration = 2f;

bool doWallJump = false;

private void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.normal.y < 0.3f)
{
Debug.DrawRay(hit.point, hit.normal, Color.red, 1.25f);

if ((Keyboard.current.spaceKey.isPressed || (Gamepad.current != null && Gamepad.current.buttonSouth.isPressed)))
{
var horizontalMovement = hit.normal;
Debug.DrawRay(hit.point, hit.normal, Color.yellow, 1.25f);
_controller.enabled = true;

doWallJump = true;

float verticalVelocity = Mathf.Sqrt(wallJumpHeight * wallJumpRange * wallJumpGravity);

if(currentJumpRoutine!= null) StopCoroutine(currentJumpRoutine);
currentJumpRoutine = StartCoroutine(ApplyJumpOverTime(horizontalMovement, verticalVelocity, jumpDuration));
}
}
}

private IEnuermator ApplyJumpOverTime(Vector3 horizontalMovement, float verticalVelocity, float jumpDuration)
{
for(var timePassed = 0f; timePassed < jumpDuration; timePassed += Time.deltaTime)
{
_controller.Move((horizontalMovement.normalized + new Vector3(0.0f, verticalVelocity, 0.0f)) * Time.deltaTime);
yield return null;
}

currentJumpRoutine = null;
}

 

usually you would have all user input in a single script and locally store an overall applied movement vector where you than apply the user input and gravity etc to. Then in Update (-> every frame) you apply that movement to the CharacterController.Move

New to Communities?

New to Communities?

Ask a Question