Question by
Yellmo · Apr 07 at 02:21 AM ·
unity 2djumpingplatformerground detectionhitbox
My ground checker only works sometimes
So at the moment when I jump on an object in-game in certain places I jump and land just fine but when I cross over certain areas of my game (with the same tiles as the rest of the space) my character will play her in air animation until I move her out.
I am using edge collider 2d for the character and tilemap collider 2d for the tiles and rigid body 2d for gravity. whilst i assume the problem has more to do with the hitboxes, here is the code i am using: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Movement : MonoBehaviour
{
public float speed;
public float jumpForce;
Rigidbody2D rb;
bool isGrounded = false;
public Transform isGroundedChecker;
public float checkGroundRadius;
public LayerMask groundLayer;
private Animator anim;
public float fallMultiplier = 2.5f;
public float lowJumpMultiplier = 2f;
public float rememberGroundedFor;
float lastTimeGrounded;
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
Move();
Jump();
CheckIfGrounded();
BetterJump();
}
void Move()
{
float moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if (moveInput == 0)
{
anim.SetBool("isRunning", false);
}
else
{
anim.SetBool("isRunning", true);
}
if (moveInput < 0)
{
transform.eulerAngles = new Vector3(0, 180, 0);
}
else if (moveInput > 0)
{
transform.eulerAngles = new Vector3(0, 0, 0);
}
}
void Jump()
{
if (Input.GetKeyDown(KeyCode.Space) && (isGrounded || Time.time - lastTimeGrounded <= rememberGroundedFor))
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
anim.SetTrigger("Jump");
}
}
void BetterJump()
{
if (rb.velocity.y < 0)
{
rb.velocity += Vector2.up * Physics2D.gravity * (fallMultiplier - 1) * Time.deltaTime;
}
else if (rb.velocity.y > 0 && !Input.GetKey(KeyCode.Space))
{
rb.velocity += Vector2.up * Physics2D.gravity * (lowJumpMultiplier - 1) * Time.deltaTime;
}
}
void CheckIfGrounded()
{
Collider2D collider = Physics2D.OverlapCircle(isGroundedChecker.position, checkGroundRadius, groundLayer);
if (collider != null)
{
isGrounded = true;
anim.SetBool("IsJumping", false);
}
else
{
if (isGrounded)
{
lastTimeGrounded = Time.time;
{
isGrounded = false;
anim.SetBool("IsJumping", true);
}
}
}
}
}
ezgifcom-gif-maker.gif
(372.7 kB)
Comment
Your answer
Follow this Question
Related Questions
Compiler error 0 Answers
Jumping with convex ground 0 Answers
Destroying an object with a specific name 0 Answers
Why does my character lose speed while jumping against the wall? 0 Answers