Skip to content

Instantly share code, notes, and snippets.

@bendux
Created February 3, 2022 20:31
Show Gist options
  • Save bendux/5fab0c176855d4e37bf6a38bb071b4a4 to your computer and use it in GitHub Desktop.
Save bendux/5fab0c176855d4e37bf6a38bb071b4a4 to your computer and use it in GitHub Desktop.
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private float horizontal;
private float speed = 8f;
private float jumpingPower = 16f;
private bool isFacingRight = true;
[SerializeField] private Rigidbody2D rb;
[SerializeField] private Transform groundCheck;
[SerializeField] private LayerMask groundLayer;
void Update()
{
horizontal = Input.GetAxisRaw("Horizontal");
if (Input.GetButtonDown("Jump") && IsGrounded())
{
rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
}
if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
}
Flip();
}
private void FixedUpdate()
{
rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
}
private bool IsGrounded()
{
return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
}
private void Flip()
{
if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
{
isFacingRight = !isFacingRight;
Vector3 localScale = transform.localScale;
localScale.x *= -1f;
transform.localScale = localScale;
}
}
}
@Poly-Guy
Copy link

Poly-Guy commented Oct 3, 2023

The movement works, but it wont jump, I'm not sure what the problem is

add a rigidbody

@Pixel8tedPotato
Copy link

Is there a way I could get a hold of a working combination of the scripts: Movement, Double Jump, Wall Jump and Slide, and Dash by Bendux? I am just starting my Gaming Development Career and am not good at coding with C# yet I can not find anything that works and don't know how to combine them on my own. Thanks!

@ZaNolan
Copy link

ZaNolan commented Oct 26, 2023

Is there a way I could get a hold of a working combination of the scripts: Movement, Double Jump, Wall Jump and Slide, and Dash by Bendux? I am just starting my Gaming Development Career and am not good at coding with C# yet I can not find anything that works and don't know how to combine them on my own. Thanks!

im doing the same, have you found out?

@Pixel8tedPotato
Copy link

Pixel8tedPotato commented Oct 27, 2023 via email

@ZaNolan
Copy link

ZaNolan commented Oct 28, 2023

okay thanks a lot!

@Pixel8tedPotato
Copy link

okay thanks a lot!

Here is the code sorry it took so long
https://gist.github.com/Pixel8tedPotato/ead41c9381b11ce47a3833fdc1198193

@SertanM
Copy link

SertanM commented Jan 29, 2024

Shouldn't the jump be in fixedupdate()?

@gavyeah
Copy link

gavyeah commented Jul 17, 2024

i can't jump

@bendux
Copy link
Author

bendux commented Jul 17, 2024

@gavyeah You probably skipped one or two important steps in the tutorial.

@Blenslo47
Copy link

HELP i can do everything and yeah everything is fine EXCEPT that i cant FRICKING JUMP (pls help) (edit i kept messing with everything until i saw that ground check is not available...)

HOW ON EARTH DO YOU JUMP!!!!!!!!

@Murbious
Copy link

I cant jump i have checked everything and it still wont work someone help

@Murbious
Copy link

Murbious commented Sep 24, 2024

update: I got jumping to work by setting the groundcheck to have a layer if ground and also adding a box colider 2D
the only problem is that it jumps mid air and you can pretty much just fly, I dont know how to fix it

Copy link

ghost commented Oct 13, 2024

*SWIPE~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment