- Home /
Question by
DevYoung · Jun 01, 2020 at 03:49 AM ·
c#2d-platformervelocity2d-physicsjumping
Mario Styled Jumping
By a Mario style jump I mean you can tap the jump key and the player will do a little hop and if you hold it they will perform a higher jump. I am new to programming, and I've been trying to implement this into my PlayerController2D script for almost a week now.
Here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController2D : MonoBehaviour
{
private float movementInputDirection; // Store what direction the player is trying to move in
private int amountOfJumpsLeft;
private bool isFacingRight = true;
private bool isRunning;
private bool isGrounded;
private bool canJump;
private Rigidbody2D rb2d;
private Animator anim;
public int amountOfJumps = 1;
public float movementSpeed = 10f;
public float jumpForce = 16f;
public float groundCheckRadius;
public Transform groundCheck;
public LayerMask whatIsGround;
// Start is called before the first frame update
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
amountOfJumpsLeft = amountOfJumps;
}
// Update is called once per frame
void Update()
{
CheckInput();
CheckMovementDirection();
UpdateAnimations();
CheckIfCanJump();
}
private void FixedUpdate()
{
ApplyMovement();
CheckSurroundings();
}
private void CheckSurroundings()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
}
private void CheckIfCanJump()
{
if (isGrounded && rb2d.velocity.y <= 0)
{
amountOfJumpsLeft = amountOfJumps;
}
if (amountOfJumpsLeft <= 0)
{
canJump = false;
}
else
{
canJump = true;
}
}
private void CheckMovementDirection() // Check whether or not the player is facing right and moving right and if it's not the same it will flip the character
{
if (isFacingRight && movementInputDirection < 0)
{
Flip();
}
else if (!isFacingRight && movementInputDirection > 0)
{
Flip();
}
if (rb2d.velocity.x != 0)
{
isRunning = true;
}
else
{
isRunning = false;
}
}
private void UpdateAnimations()
{
anim.SetBool("isRunning", isRunning);
anim.SetBool("isGrounded", isGrounded);
anim.SetFloat("yVelocity", rb2d.velocity.y);
}
private void CheckInput() // Check Input that we are expecting from the player
{
movementInputDirection = Input.GetAxisRaw("Horizontal"); // Based off this input we will apply force to the player
if (Input.GetButtonDown("Jump"))
{
Jump();
}
}
private void Jump()
{
if (canJump)
{
rb2d.velocity = new Vector2(rb2d.velocity.x, jumpForce);
amountOfJumpsLeft--;
}
}
private void ApplyMovement()
{
rb2d.velocity = new Vector2(movementSpeed * movementInputDirection, rb2d.velocity.y);
}
private void Flip()
{
isFacingRight = !isFacingRight;
transform.Rotate(0.0f, 180.0f, 0.0f);
}
private void OnDrawGizmos()
{
Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
}
}
Comment
Answer by Kudorado · Jun 01, 2020 at 07:24 AM
I think this might help you. @DevYoung
public float initVelocity = 5; //force when first click
public float continueVelocity = 0.25f; //force when holding mouse button
public float jumpDuration = 2f; //holding button duration
float jumpTime;
Rigidbody2D body2D;
bool isJumping, canJump = true;
void Start()
{
body2D = GetComponent<Rigidbody2D>();
}
void Update()
{
JumpInput();
}
void JumpInput()
{
if (Input.GetMouseButtonDown(0) && canJump)
{
canJump = false; //this might set true when player collide with ground.
jumpTime = 0; //reset jump time
isJumping = true; //player jumping
Vector2 jumpDir = (Vector2.one); //you can change the direction according to your game.
body2D.velocity = jumpDir * initVelocity; //instant add velocity for player
}
if (isJumping && jumpTime < jumpDuration)
{
Vector2 jumpDir = Vector2.one; //you can change the direction according to your game.
body2D.AddForce(jumpDir * continueVelocity, ForceMode2D.Impulse); //add more force to the player.
jumpTime += Time.deltaTime;
}
if (Input.GetMouseButtonUp(0))
{
isJumping = false; //player no longer hold mouse button, ain't jump anymore.
}
}