Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Lerping Jump

Discussion in 'Scripting' started by Exeneva, May 24, 2019.

  1. Exeneva

    Exeneva

    Joined:
    Dec 7, 2013
    Posts:
    432
    I'm implementing a custom character controller that doesn't use rigidbody physics for movement, only transform translation.

    I'm trying to add jump functionality, with the idea that I will lerp the upward and downward movement. Is this the right idea?

    Code (CSharp):
    1.         private IEnumerator JumpPlayer(EntityCollections<PlayerEntityViewStruct, PlayerInputDataStruct>.EntityGroupsIterator.ValueRef jumpablePlayer)
    2.         {
    3.             var startingYPos = jumpablePlayer.Item1.PositionComponent.Position.y;
    4.             var yPos = Mathf.Lerp(jumpablePlayer.Item1.PositionComponent.Position.y,
    5.                 jumpablePlayer.Item1.MovementComponent.JumpHeight,
    6.                 jumpablePlayer.Item1.MovementComponent.JumpSpeed * _time.deltaTime
    7.                 );
    8.             var time = 0.0f;
    9.             while (time < 1.0)
    10.             {
    11.                 time += _time.deltaTime;
    12.                 jumpablePlayer.Item1.TransformComponent.Position = new Vector3(
    13.                     jumpablePlayer.Item1.PositionComponent.Position.x,
    14.                     yPos,
    15.                     jumpablePlayer.Item1.PositionComponent.Position.z);
    16.                 yield return null;
    17.             }
    18.  
    19.             time = 0.0f;
    20.             while (time < 1.0)
    21.             {
    22.                 time += _time.deltaTime;
    23.                 jumpablePlayer.Item1.TransformComponent.Position = new Vector3(
    24.                     jumpablePlayer.Item1.PositionComponent.Position.x,
    25.                     startingYPos,
    26.                     jumpablePlayer.Item1.PositionComponent.Position.z);
    27.                 yield return null;
    28.             }
    29.             jumpablePlayer.Item2.Jump = false;
    30.         }