Skip to content

Instantly share code, notes, and snippets.

@bendux
Created February 3, 2022 20:31
Show Gist options
  • Select an option

  • Save bendux/5fab0c176855d4e37bf6a38bb071b4a4 to your computer and use it in GitHub Desktop.

Select an option

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;
}
}
}
@CalculatedChaos44
Copy link

i think im doing it wrong but it keeps giving me errors

@bendux
Copy link
Author

bendux commented Apr 6, 2023

@CalculatedChaos44 Join our Discord server, and let's solve your problem together!

@HydroniumCore
Copy link

It's not working, I can't jump for some reason

@bendux
Copy link
Author

bendux commented May 31, 2023

@HydroniumCore Make sure that the ground check is positioned on the bottom edge of your player's collider.

@demonicDarkk
Copy link

i keep getting errors saying that rb is not defined

@bendux
Copy link
Author

bendux commented Jun 14, 2023

PlayerMovement

@MasterZombieYT
Copy link

MasterZombieYT commented Jun 17, 2023

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...)

@bendux
Copy link
Author

bendux commented Jun 21, 2023

@MasterZombieYT What do you mean?

@TomEXEEE
Copy link

hipitiy hoppity your code is now my property

your, dont you mean "OUR"

@TheDanifan
Copy link

eyyyyy bois wanna steal the fricking code? CUZ HIPPITY HOPPITY ITS MY PROPRTEY BITCH >:)

@iojuedioe9rd
Copy link

eyyyyy bois want to steal the fricking code? BECAUSE HIPPITY HOPPITY ITS MY PROPERTY BITCH >:)

@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

@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~

@Law-cmd
Copy link

Law-cmd commented Apr 21, 2025

@Murbious you dont need the box colider 2d

@MunchiesDevAdventure
Copy link

If you are pulling your hair out asking, "Why can't I jump?"

Check the following things:

1.) Check that your GroundCheck child is at the bottom of the player
2.) Click on ALL your platforms - if they are not all on the Layer 'Ground' you have an issue.

@JIMBOMCBIGGS
Copy link

JIMBOMCBIGGS commented Jul 1, 2025

I have my ground check at the bottom of the player, and all my platforms have the layer ground and i still cant jump!!!!!
never mind i got it working

@notguifi
Copy link

we do a little thieving

@LanaLuci
Copy link

what is the ".y" in line 33 for? I'm a beginner.

@notguifi
Copy link

@LanaLuci It doesn’t really change anything, you just need to add 2 parameters to the Vector2 for it to work

@Rehkmil
Copy link

Rehkmil commented Oct 19, 2025

i think im doing it wrong but it keeps giving me errors

add box collider to ur player

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