- Home /
Question by
ThePhantomGoat · Sep 09, 2018 at 02:13 AM ·
collider2djumpinggrounded
2D Charachter infinite jumping
hi, just a simple question from a noob, how do I get this to stop jumping infinitely?
using UnityEngine; using System.Collections;
public class PlayerController : MonoBehaviour {
[HideInInspector]
public bool isFacingRight = true;
[HideInInspector]
public bool isJumping = false;
[HideInInspector]
public bool isGrounded = false;
public GameObject thing;
public GameObject floor;
public GameObject platform;
public float maxSpeed = 7.0f;
public float jumpForce = 650.0f;
public Transform groundCheck;
public LayerMask groundLayers;
private float groundCheckRadius = 0.2f;
void Start()
{ }
void Update()
{
if (Input.GetButtonDown("Jump"))
{
if (isGrounded == true)
{
this.GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, 0);
this.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce));
}
if (floor == thing)
{
this.GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, 0);
this.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce));
}
}
}
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle
(groundCheck.position, groundCheckRadius, groundLayers);
float move = Input.GetAxis("Horizontal");
this.GetComponent<Rigidbody2D>().velocity =
new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
if ((move > 0.0f && isFacingRight == false) || (move < 0.0f && isFacingRight == true))
{
Flip();
}
}
void Flip()
{
isFacingRight = !isFacingRight;
Vector3 playerScale = transform.localScale;
playerScale.x = playerScale.x * -1;
transform.localScale = playerScale;
}
Comment
Your answer
Follow this Question
Related Questions
How to jump on only "ground" colliders 1 Answer
Player can jump on non ground items 1 Answer
Player controller jump not always working 2 Answers
Jumping behavior based on what part of an object you're touching? 0 Answers
Error jumping and moving 2 Answers