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

SCAW14 commented Feb 5, 2023

I followed the entire tutorial and got everything working but the jump button is not working so unless the jump button is some weird button i would never think to press im confused. I dont have discord so i cant talk on there

@SCAW15
Copy link

SCAW15 commented Feb 5, 2023

I followed the entire tutorial and got everything working but the jump button is not working so unless the jump button is some weird button i would never think to press im confused. I dont have discord so i cant talk on there

So you need help right, ask chat gpt.

@NOTLOSTBIO
Copy link

Hippity Hoppity, thank you for your Coditty <3

you said it wrong

@NOTLOSTBIO
Copy link

hippity hoppity your code is now my property.

@pepa1555
Copy link

Hippity Hoppity Thank you you help me in my game journey

@Antonara
Copy link

Antonara commented Mar 19, 2023

Hippity hopity you code is now my property

@aka-user15
Copy link

aka-user15 commented Mar 25, 2023

Hippity hopity your code is now my property (thx)

@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

@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

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