9. Unity 5 tutorial for beginners: 2D Platformer - Jumping

9. Unity 5 tutorial for beginners: 2D Platformer - Jumping

inScope Studios

8 лет назад

110,859 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@sabrinahawkesworth2547
@sabrinahawkesworth2547 - 07.07.2022 20:10

FOR THOSE STRUGGLING TO MAKE YOUR CHARACTER JUMP BUT YOUR CODE IS EXACTLY THE SAME - I increased my jump force to a massive value such as 1200 and I found he finally jumps

Ответить
@ogwaffle1364
@ogwaffle1364 - 17.05.2022 09:40

my charcator seems to not wanna jump, any fixes, ive done exactly like the video

Ответить
@fateshow5303
@fateshow5303 - 09.11.2020 17:44

So great at explaining

Ответить
@-Constrick-
@-Constrick- - 18.10.2020 01:34

When I activate Air Control my character can jump and attack but cannot move. If I uncheck it he can move and attack freely but running during jump too. What can it be?

Ответить
@faheemkarim6209
@faheemkarim6209 - 03.07.2020 21:46

im getting this error and i dont understand why
Assets\Scripts\Player.cs(125,18): error CS0102: The type 'Player' already contains a definition for 'isGrounded'

Ответить
@swiertek4057
@swiertek4057 - 05.06.2020 17:49

I've encountered a problem where my character cannot jump while running to the right :/

Ответить
@MeltedCheesyYT
@MeltedCheesyYT - 03.05.2020 12:00

Hello me again! So I put the jump = false; into the ResetValues. I can't jump at all even when I press the spacebar. But if I remove the jump = false; it keeps on jumping. Any way to solve this? Great tutorials btw!

Here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
private Rigidbody2D myRigidbody;

private Animator myAnimator;

[SerializeField]
private float movementSpeed;

private bool facingRight;

[SerializeField]
private Transform[] groundPoints;

[SerializeField]
private float groundRadius;

[SerializeField]
private LayerMask whatIsGround;

private bool isGrounded;

private bool jump;

[SerializeField]
private float jumpForce;

// Start is called before the first frame update
void Start()
{
facingRight = true;
myRigidbody = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
}

// Update is called once per frame

void FixedUpdate()
{
float horizontal = Input.GetAxis("Horizontal");

isGrounded = IsGrounded();

HandleMovement(horizontal);

Flip(horizontal);

HandleInput();

ResetValues();

}

private void HandleMovement(float horizontal)
{
myRigidbody.velocity = new Vector2(horizontal * movementSpeed, myRigidbody.velocity.y);

myAnimator.SetFloat("speed", Mathf.Abs(horizontal));

if (isGrounded && jump)
{
isGrounded = false;
myRigidbody.AddForce(new Vector2(0, jumpForce));

}
}


private void HandleInput()

{
if (Input.GetKeyDown(KeyCode.Space))
{
jump = true;
}
}




private void Flip(float horizontal)
{
if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
{
facingRight = !facingRight;

Vector3 theScale = transform.localScale;

theScale.x *= -1;

transform.localScale = theScale;

}
}



private bool IsGrounded()
{
if (myRigidbody.velocity.y <= 0)
{

foreach (Transform point in groundPoints)
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);

for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
{
return true;
}

}

}

}return false;
}


private void ResetValues()
{
jump = false;
}









}

Ответить
@MeltedCheesyYT
@MeltedCheesyYT - 01.05.2020 09:00

I doubt that anyone's going to find this comment but I really need help. Can someone take a look at my code and tell me whats wrong? I didn't really watch the attack animation and slide animation because I didn't need it. Here's my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
private Rigidbody2D myRigidbody;

private Animator myAnimator;

[SerializeField]
private float movementSpeed;

private bool facingRight;

[SerializeField]
private Transform[] groundPoints;

[SerializeField]
private float groundRadius;

private LayerMask whatIsGround;

private bool isGrounded;

private bool jump;

// Start is called before the first frame update
void Start()
{
facingRight = true;
myRigidbody = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
}

// Update is called once per frame
void FixedUpdate()
{
float horizontal = Input.GetAxis("Horizontal");

isGrounded = IsGrounded();

HandleMovement(horizontal);

Flip(horizontal);
}

private void HandleMovement(float horizontal)
{
myRigidbody.velocity = new Vector2(horizontal * movementSpeed, myRigidbody.velocity.y);

myAnimator.SetFloat("speed", Mathf.Abs(horizontal));
}
if (isGrounded && jump)
{
isGrounded = false;
myRigidbody.AddForce(new Vector2(0, jumpForce));
}

private void HandleInput()
{
if (!Input.GetKeyDown(KeyCode.Space))
{
jump = true
}

}



private void Flip(float horizontal)
{
if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
{
facingRight = !facingRight;

Vector3 theScale = transform.localScale;

theScale.x *= -1;

transform.localScale = theScale;

}
}

private bool IsGrounded()
{
if (myRigidbody.velocity.y <= 0)
{

foreach (Transform point in groundPoints)
{
CircleCollider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);

for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
{
return true;
}

}

}
return false;
}
}
}

Ответить
@Lawbringer.
@Lawbringer. - 25.03.2020 05:23

Hey man i dont know if you still reading comments on this video. but, my character cannot jump and floating in the air. do you know what might causes this ?. thank you

Ответить
@voice6262
@voice6262 - 14.03.2020 18:07

I ran into this Problem: 'PlayerMovement.HandleMovement(float)' is a method, which is not valid in the given context
Can you pls help me? Ty

Ответить
@tbelmut2o997
@tbelmut2o997 - 13.02.2020 19:34

Hi, I deleted the attack codes because I don't need them, but when I added jump codes, it didn't work. Please help me 😭.

Ответить
@michelebonaldi6310
@michelebonaldi6310 - 30.11.2019 01:52

I skip the previous videos, 8 7 6 5 is a problem?

Ответить
@884Emerald
@884Emerald - 17.11.2019 05:50

Help @ inScope Studios!
I followed this tutorial exactly and my player does jump, but when I'm running to the right and hitting the space as well it won't jump. It will however jump when running to the left.
Have any solutions? Thank you!

Ответить
@nizarsadek7193
@nizarsadek7193 - 11.11.2019 16:07

Hey, first of all your tutorials are great !! I have a little problem my player can jump but on the ground he can't move left or right although the animation of running is working. Please help!

Ответить
@mitchanator3183
@mitchanator3183 - 05.11.2019 03:42

i doubt anyone is going to answer but when add the jump my character stays still and its not moving but the run animation is happening

Ответить
@jeremydeguzman6991
@jeremydeguzman6991 - 04.11.2019 13:05

i have an error The type `Player' already contains a definition for `IsGrounded' how to fix that?
help me plss.

Ответить
@ivonedev9658
@ivonedev9658 - 27.10.2019 21:01

is this still working for 2019?

Ответить
@ellimation
@ellimation - 14.10.2019 00:59

hi, can you help me with this error?
Assets/Scripts/NewBehaviourScript.cs(121,18): error CS0102: The type 'NewBehaviourScript' already contains a definition for 'isGrounded'

Ответить
@viahoshi4340
@viahoshi4340 - 07.10.2019 13:14

hi could you pls help me. i'm following all the instructions i also checking several times where did i do wrong but all are the same. my problem is my character keep jumping, yours stop jumping after you put jump = false under ResetValues, i did the same but my character still keep on jumpingㅠㅠ please help me

Ответить
@bjrnkaascorfitz6430
@bjrnkaascorfitz6430 - 04.09.2019 17:16

I made this variable:
[SerializeField] string[] listOfGroundedLayers;

And this function:
private bool IsGrounded()
{
return myRigidBody.IsTouchingLayers(LayerMask.GetMask(listOfGroundedLayers)) ? true : false;

}


IMPORTANT note: You need to manually write in the "layer" in Unity, that you wish your character should be able to walk on. You can just write "Default", if the ground is Default.

Ответить
@marcusmorrow3900
@marcusmorrow3900 - 14.07.2019 19:13

To those having trouble with your player constantly jumping and the (jump = false) in the reset code not working, try setting jump back to false at the end of the movement code.

Ответить
@marcusmorrow3900
@marcusmorrow3900 - 14.07.2019 19:09

Thanks man you helped out again. I was looking through jump codes like crazy and found them to be flawed in that when my characters collider touched a platform in midair he was still able to jump. I found this one to be the best,most useful, precise and maybe not so simple, but you take time to explain it well so I could implement it. thanks.

Ответить
@JaxFellex
@JaxFellex - 21.06.2019 15:57

Jumping button ?

Ответить
@abhaychandavar1766
@abhaychandavar1766 - 23.05.2019 05:21

Is physics 2d.overlap circle faster than detecting collision or using triggers with OnTriggerEnter2d and OnTriggerExit2d, I've tried using triggers but sometimes he doesn't jump, what's the reason, Is it slow?

Ответить
@Marvin-om7iu
@Marvin-om7iu - 22.05.2019 09:23

I think this was one of the harder to follow tutorials because of how you implemented if the player is grounded or not. I was wondering why we couldn't use OnCollisionEnter or OnCollisionExit to detect the player being grounded. I understand that we would have to set up more logic so that we know which collision is considered ground or not. I realize this is an old tutorial and maybe certain things have changed since then. Either way thanks for the great tutorial series. I have learned a lot.

Ответить
@klewkob7024
@klewkob7024 - 04.05.2019 17:58

for anyone who skipped slide and attack and character keeps jumping then put


jump = false


under


myRigidBody.AddForce(new Vector2(0, jumpForce));

Ответить
@klewkob7024
@klewkob7024 - 04.05.2019 17:37

HELP MY CHARACTER KEEPS JUMPING EVEN THOUGH I MADE THE RESET VALUES FUNCTION AND I DONT KNOW WHAT TO DO I SKIPPED THE CROUCH AND ATTACK CUZ I DONT NEED IT FOR MY GAME SO PLS HELP!!!!

Ответить
@SukiCassiopeiaYJ
@SukiCassiopeiaYJ - 23.04.2019 21:27

I don't know why but where it says Player (script), I don't get any movement speed, or any elements in there.

Ответить
@TheLichKng64
@TheLichKng64 - 05.04.2019 08:16

i wish i could give you more than one like

Ответить
@rofeldayao2972
@rofeldayao2972 - 13.03.2019 12:27

Hi man! great tutorial. I just have a little bit of a problem. You see, I completed everything jumping works just fine while standing and while walking on straight platforms. However, when I start to go to a steep platform, player doesn't jump at all. But if going down that steep platform, player can jump just fine. Just wondering what can I do

EDIT:
I figured out that I don't really need to check if my y axis <= 0 inside the IsGrounded function since my y axis will be > 0 everytime i try to walk upwards

Ответить
@lolyouyou2802
@lolyouyou2802 - 07.03.2019 17:52

I can´t jump even if I press space.
Whats wrong? And i dont want to have the attack and slide codes.


using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour
{
private Rigidbody2D myRigidBody;

private Animator myAnimator;

[SerializeField]
private float movementSpeed;

private bool FacingRight;

[SerializeField]
private Transform[] groundPoints;

[SerializeField]
private float groundRadius;

[SerializeField]
private LayerMask whatIsGround;

private bool isGrounded;

private bool jump;

[SerializeField]
private float jumpForce;

// Start is called before the first frame update
void Start()
{
FacingRight = true;
myRigidBody = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
}

// Update is called once per frame
void FixedUpdate()
{
float horizontal = Input.GetAxis("Horizontal");

isGrounded = IsGrounded();

HandleMovement(horizontal);

Flip(horizontal);
}

private void HandleMovement(float horizontal)
{
myRigidBody.velocity = new Vector2(horizontal * movementSpeed, myRigidBody.velocity.y);

myAnimator.SetFloat("speed", Mathf.Abs(horizontal));

if (isGrounded && jump)
{
isGrounded = false;
myRigidBody.AddForce(new Vector2(0, jumpForce));

}

}

private void HandleInput()
{
if (Input.GetKeyDown(KeyCode.Space))
{
jump = true;
}
}

private void Flip(float horizontal)
{
if (horizontal > 0 && !FacingRight || horizontal < 0 && FacingRight)
{
FacingRight = !FacingRight;

Vector3 theScale = transform.localScale;

theScale.x *= -1;

transform.localScale = theScale;
}
}

private bool IsGrounded()
{
if (myRigidBody.velocity.y <= 0)
{
foreach (Transform point in groundPoints)
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);

for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
{
return true;
}
}
}
}
return false;
}
}

namespace Assets.Scripts
{
class Speed
{
}
}

Ответить
@cosmin.grigoroaia3759
@cosmin.grigoroaia3759 - 24.01.2019 23:12

I don't have any errors, but don't jump, i veryfied the ground points, but it's still don't working, please help me

Ответить
@cosmin.grigoroaia3759
@cosmin.grigoroaia3759 - 24.01.2019 22:39

Hei can you please, help me?
I saw full video, and i made all exactly like you, but my character won't start.
PS: I saw the video for movement, and now i am watching at this, because i don.t have animations to slide or crouch..
Please help me

Ответить
@simonthedigger99
@simonthedigger99 - 24.01.2019 18:30

were there any specific something you wrote in the library because im getting this weird error
Assets/PlayerController.cs(114,47): error CS1061: Type `UnityEngine.Collider2D[]' does not contain a definition for `Lenght' and no extension method `Lenght' of type `UnityEngine.Collider2D[]' could be found. Are you missing an assembly reference?

Ответить
@hunterdeloney4711
@hunterdeloney4711 - 03.01.2019 01:07

For some reason I could jump but when I put jump = false in the reset values section I couldn’t jump any more

Ответить
@baldchessman
@baldchessman - 26.12.2018 23:23

didn't work errors

on private bool is grounded
it says the modifier private is not valid for this item

and in spot

if myrigibody.velocity.y<= 0

the name myrigibody does not exist in the current context


and in
if colliders[i]

(local variable) collider2d[]colliders

i wish 4 help

Ответить
@SoutParl
@SoutParl - 23.12.2018 18:45

This could be done way easier by using OnCollisionEnter2D (Collision2D other)

Ответить
@krzysztofpietrzak8357
@krzysztofpietrzak8357 - 17.12.2018 18:04

dont work

Ответить
@dragosudrea6533
@dragosudrea6533 - 16.12.2018 22:48

Hey,

Any idea why my character doesn't jump whenever I press space? It jumps occasionally, as if I have to wait a certain time before jumping again.

Ответить
@nathm2854
@nathm2854 - 16.12.2018 02:14

If you are encountering problems at this step, despite writing the code perfectly, it's because you skipped the previous videos.

You are most likely missing this line:

{
HandleInput();
}

Add it in the FixedUpdate

It should look like this:

void FixedUpdate ()


{
float horizontal = Input.GetAxis("Horizontal");

isGrounded = IsGrounded();

HandleMovement(horizontal);

Flip(horizontal);

HandleInput();

ResetValues();
}

I didn't need an attack animation for my character and I initially skipped the video, only to encounter problems. After watching the other videos, I found out I was missing something.

Ответить
@gomango999
@gomango999 - 01.12.2018 18:21

Coming from a gamemaker background, I was taught a system where u would move the player hitbox one pixel down and check if it was colliding with a wall to see if u r grounded I'm really not sure about this method as the ground points can also detect collision with a wall when u r jumping into one from the side, which makes the player think he is grounded, causing various bugs. ://

Ответить
@bmarch9
@bmarch9 - 24.11.2018 04:30

I have box colliders on both my player and my ground... my player still is falling through... anyone please help me

Ответить
@crispp9240
@crispp9240 - 17.11.2018 10:00

please slow down and explain, i couldnt follow on, but anyway great tutorial :)

Ответить
@P-Bean
@P-Bean - 15.11.2018 11:12

if (isGrounded && jump)
{
Debug.Log ("jump");
isGrounded = false;
myRigidBody.AddForce (new Vector2 (0, jumpForce));
}

the function is being called, i checked using debug.log, yet i still don't jump (jumpForce set to 700)
if anyone can, please help me, i'm stuck

Ответить
@lolwithgames5595
@lolwithgames5595 - 10.11.2018 18:55

i don't have addforce help pls

Ответить
@sapphiretigress1
@sapphiretigress1 - 19.10.2018 01:00

anyone have a rewrite for the new API for the 2D collider?Unity wont let me use the old code to run indexing

Ответить
@mashirokawaii2712
@mashirokawaii2712 - 11.10.2018 02:36

Need a little help on setting the ground points, when playing, and after hitting space my character would shake up and down but its not that noticeable, but still couldnt jump

edit: my bad, my problem is really with the ground points i cant set them properly i did what the other guys suggested in the comments set it a little lower etc, have everything checked too the spellings , hope someone still replies :<<<<<

Ответить