Always Grounded When Jumping
Hey hey!
I'm following along via the 2dPlatformer tutorial, and can't figure out/understand why my jump method isn't behaving properly.
I think it's due to the "grounded" bool, and the fact that it always return true, even when my player is in air.
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
I'm pretty sure I've set everything up correctly in Unity (child object @ the player Layer); however, it appears to be permanently colliding even upon starting the game (in which my player is falling to the ground)
I'd be super appreciative if anyone can help a rookie out - as I continue to grasp c# and the Unity platform. Thanks again so much!
Full code below:
// moving
public float maxSpeed = 10f;
// which direction i am facing
private bool facingRight = true;
// anim stuff
private Animator anim;
//Grounded / Falling
public Transform groundCheck;
public LayerMask whatIsGround;
private bool grounded = false;
private float groundRadius = 0.2f;
//jumping
public float jumpForce = 200f;
// double jump
private bool doubleJump = false;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void FixedUpdate () {
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
// animation for grounded
anim.SetBool("Ground", grounded);
// doublejump
if (grounded)
{
doubleJump = false;
}
//CHECKING IF GROUNDED UNTIL I CREATE ANIMATIONS
if (grounded = true)
{
Debug.Log("Player is grounded");
}
anim.SetFloat("vSpeed", GetComponent<Rigidbody2D>().velocity.y);
// cannot move left or right when jumping
if (!grounded) return;
// actually move
float move = Input.GetAxis("Horizontal");
// animation for moving
anim.SetFloat("Speed", Mathf.Abs(move));
GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
if (move > 0 && !facingRight)
{
Flip();
} else if(move < 0 && facingRight)
{
Flip();
}
}
void Update()
{
// jumping
if ((grounded || !doubleJump) && Input.GetKeyDown(KeyCode.Space)) // should not do it this way, should map to a jump key so actual key can be changed (currently spacebar)
{
anim.SetBool("Ground", false);
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce));
if (!doubleJump && !grounded)
{
doubleJump = true;
}
}
}
// determine if facing left or right
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
dude your code seems fine,you said grounded bool always return true.maybe you add a big circle collider2D to your GroundCheck object.and it's always colliding with the ground layer.
also your code has some little porblems. you are using GetComponent().velocity.y , and it reduces performance you should use something like this
public Rigidbody2D rig;
void Start(){
rig = GetComponent<Rigidbody2D> ();
}
and then only use rig ins$$anonymous$$d GetComponent () every time you need Rigidbody2D :).
also you can use a moving function like this and you don't have a flip function.
public void moving(){
if (Input.GetAxisRaw ("Horizontal") != 0) {
rig.velocity = new Vector2 (moveSpeed*Input.GetAxisRaw ("Horizontal"),rig.velocity.y);
transform.localScale = new Vector3 (1*Input.GetAxisRaw ("Horizontal"),1,1);
}
else if(Input.GetAxisRaw ("Horizontal") == 0){
rig.velocity=new Vector2(0,rig.velocity.y);
}
}
Answer by Cuttlas-U · Sep 10, 2017 at 07:06 AM
hi; did u set the Ground layer Correctly in the inspector ? ; and try to change the GroundRadiuse maybe its different ;
Answer by AlexanderTheCommon · Sep 10, 2017 at 05:03 PM
Thanks for replying @Cuttlas-U.
The thing is my check for is grounded is firing even before my player hits the ground (falls upon play). I've played around with all sizes for groundRadius, but no improvement I really think I did - but maybe I'm missing something small? Do you see anything off in the screenshots below? (8-bit Jesus IS NOT cooperating)
Player Inspector:
The GroundCheck inspector only has the transform and also has the Layer set to Player.
Any thoughts? Thanks again so much!
Hey Hey!
So I figured out it was actually an error in the doublejump functionality (causing perma jump) while at the same time an error with my isGrounded check (= ins$$anonymous$$d of --_.
After disabling double jump and the check, all is good.
Thanks so much for looking in to this with me - much appreciated!
hey again; tnx for the accept; nice to hear it got fixed; go on problems always happen but they are good because we got happy when we fix them :)