Skip to content
Dawnosaur edited this page Jul 26, 2023 · 22 revisions

About

  • What is my platformer movement for?
  • Why should you use it?
  • Why shouldn't you use it?

Getting Started

Run

private void Run(float lerpAmount) { //Calculate the direction we want to move in and our desired velocity float targetSpeed = _moveInput.x * Data.runMaxSpeed; //We can reduce are control using Lerp() this smooths changes to are direction and speed targetSpeed = Mathf.Lerp(RB.velocity.x, targetSpeed, lerpAmount);

	#region Calculate AccelRate
	float accelRate;

	//Gets an acceleration value based on if we are accelerating (includes turning) 
	//or trying to decelerate (stop). As well as applying a multiplier if we're air borne.
	if (LastOnGroundTime > 0)
		accelRate = (Mathf.Abs(targetSpeed) > 0.01f) ? Data.runAccelAmount : Data.runDeccelAmount;
	else
		accelRate = (Mathf.Abs(targetSpeed) > 0.01f) ? Data.runAccelAmount * Data.accelInAir : Data.runDeccelAmount * Data.deccelInAir;
	#endregion

	#region Add Bonus Jump Apex Acceleration
	//Increase are acceleration and maxSpeed when at the apex of their jump, makes the jump feel a bit more bouncy, responsive and natural
	if ((IsJumping || IsWallJumping || _isJumpFalling) && Mathf.Abs(RB.velocity.y) < Data.jumpHangTimeThreshold)
	{
		accelRate *= Data.jumpHangAccelerationMult;
		targetSpeed *= Data.jumpHangMaxSpeedMult;
	}
	#endregion

	#region Conserve Momentum
	//We won't slow the player down if they are moving in their desired direction but at a greater speed than their maxSpeed
	if(Data.doConserveMomentum && Mathf.Abs(RB.velocity.x) > Mathf.Abs(targetSpeed) && Mathf.Sign(RB.velocity.x) == Mathf.Sign(targetSpeed) && Mathf.Abs(targetSpeed) > 0.01f && LastOnGroundTime < 0)
	{
		//Prevent any deceleration from happening, or in other words conserve are current momentum
		//You could experiment with allowing for the player to slightly increae their speed whilst in this "state"
		accelRate = 0; 
	}
	#endregion

	//Calculate difference between current velocity and desired velocity
	float speedDif = targetSpeed - RB.velocity.x;
	//Calculate force along x-axis to apply to thr player

	float movement = speedDif * accelRate;

	//Prevents tiny velocity values after stopping
	if(Mathf.Abs(speedDif) < 0.0001f)
		return;

	//Convert this to a vector and apply to rigidbody
	RB.AddForce(movement * Vector2.right, ForceMode2D.Force);

	/*
	 * For those interested here is what AddForce() will do
	 * RB.velocity = new Vector2(RB.velocity.x + (Time.fixedDeltaTime  * speedDif * accelRate) / RB.mass, RB.velocity.y);
	 * Time.fixedDeltaTime is by default in Unity 0.02 seconds equal to 50 FixedUpdate() calls per second
	*/
}        

Jump

Wall Jump

Dash

Manual

Clone this wiki locally