Question by
no00ob · Jul 02, 2018 at 09:25 PM ·
2d2d-platformer2d-physicsplayer movementstandard-assets
Why did my character start jumping infinitely
I had a perfectly working code that did NOT allow the player to jump infinitely but suddenly after I started doing something else I noticed my player can jump infinitely and no I did not forgot to add the layers to the layer mask obviusly cuz it was working earlier.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace UnityStandardAssets._2D
{
public class Player : MonoBehaviour
{
[SerializeField]
private float jumpForce = 800f;
[Range(0, 1)]
[SerializeField]
private float crouchSpeed = .36f;
[SerializeField]
private bool airControl = true;
[SerializeField]
private LayerMask whatIsGround;
public float maxSpeed = 10f;
public bool isMovingDown;
public bool powerUp = false;
private Transform groundCheck;
const float groundedRadius = .2f;
private bool grounded;
private Transform ceilingCheck;
const float ceilingRadius = .01f;
private Animator anim;
private Rigidbody2D m_Rigidbody2D;
private bool facingRight = true;
private float travel;
private float currentheight;
private float previousheight;
private void Awake()
{
isMovingDown = false;
groundCheck = transform.Find("GroundCheck");
ceilingCheck = transform.Find("CeilingCheck");
anim = GetComponent<Animator>();
m_Rigidbody2D = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
grounded = false;
// Check if colliding with ground.
Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheck.position, groundedRadius, whatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
{
grounded = true;
}
}
anim.SetBool("Ground", grounded);
anim.SetFloat("vSpeed", m_Rigidbody2D.velocity.y);
}
void Update()
{
currentheight = transform.position.y;
travel = currentheight - previousheight;
if (!grounded && travel < 0)
{
isMovingDown = true;
}
else if (grounded || travel >= 0)
{
isMovingDown = false;
}
previousheight = currentheight;
}
public void Move(float move, bool crouch, bool jump)
{
// If crouching, check to see if the character can stand up
if (!crouch && anim.GetBool("Crouch"))
{
// If the character has a ceiling preventing them from standing up, keep them crouching
if (Physics2D.OverlapCircle(ceilingCheck.position, ceilingRadius, whatIsGround))
{
crouch = true;
}
}
anim.SetBool("Crouch", crouch);
if (grounded || airControl)
{
move = (crouch ? move*crouchSpeed : move);
// The Speed animator parameter is set to the absolute value of the horizontal input.
anim.SetFloat("Speed", Mathf.Abs(move));
m_Rigidbody2D.velocity = new Vector2(move*maxSpeed, m_Rigidbody2D.velocity.y);
if (move > 0 && !facingRight)
{
Flip();
}
else if (move < 0 && facingRight)
{
Flip();
}
}
if (grounded && jump && anim.GetBool("Ground"))
{
grounded = false;
anim.SetBool("Ground", false);
m_Rigidbody2D.AddForce(new Vector2(0f, jumpForce));
PlaySound.Play(5);
}
}
private void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
}
Comment