Kategorier: Alle - gravity - movement - direction - height

av Mike Ton 9 år siden

320

Unity3D : JumpPhysics

The provided code snippet is a function designed to handle various aspects of character movement, specifically focusing on the jumping mechanics in a game. The function checks for user input to initiate a jump and manages different states such as whether the character is on the ground, in the air, or has reached maximum jump height.

Unity3D : JumpPhysics

JumpPhysics

(func)

Vector2
GetJumpPowerVector(){ ... }

return (HasDoubleJumped) ? new Vector2(0, doubleJumpPower) : new Vector2(0, jumpPower);

bool
HitTagOrLayerObject(GameObject collisionObject){ ... }

//???

RaycastCollideVertical(Vector3 direction){ ... }

//ray casting on left and right side

AtMaxVariableJumpHeight(){ ... }

//changes max height based on if double jump is possible or not

(delete)

RemovedJumpInputCheck(){ ... }

JumpInputCheck(){ ... }

void
DebugRays(){ ... }

// draws all rays in up and down direction

CalculateMoveDirection(){ ... }

if(!IsGrounded){ ... }

moveDirection += jumpDirection;

// moveDirection holds final value of movement delta

//apply gravity

else if(IsMaxHeightReached || !IsHoldingJump){ ... }

//else MaxHeight must be reached or no longer holding jump to apply gravity

if (!allowVariableJumpHeight){ ... }

jumpDirection += new Vector2(0, currentGravity);

//if not variable jump, just apply gravity

if (allowVariableJumpHeight) { ... }

if(AtMaxVariableJumpHeight()){ ... }

// check if we're at max height so we can apply gravity

if (bCeiling) { ... }

IsMaxHeightReached = true;

jumpDirection = (jumpDirection.y > 0) ? new Vector2(0, 0) : jumpDirection;

// if hit ceiling, stop all upward movement.

// if airborne, apply gravity to current jump speed then add to movement vector

else if(Input.GetButtonUp("Jump")) { ... }

IsHoldingJump = false;

if(Input.GetButtonDown("Jump")){ ... }

if (!IsHoldingJump && (IsGrounded || (allowDoubleJump && IsDoubleJumping && !HasDoubleJumped)) && !RaycastCollideVertical(Vector3.up)) { ... }

if (allowDoubleJump && !HasDoubleJumped && HasJumpedOnce) { ... }

Update(){ ... }

transform.Translate(moveDirection * Time.deltaTime);

// move via transform component

CalculateMoveDirection();

bool bCeiling = RaycastCollideVertical(Vector2.up);

Awake(){ ... }

(var)