for some reason the isGrounded is always checked off for me
heres the code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
Rigidbody2D rb;
Animator animator;
[SerializeField] Transform groundCheckCollider;
[SerializeField] LayerMask groundLayer;
[SerializeField] const float groundCheckRadius = 0.2f;
[SerializeField] public float speed;
[SerializeField] public float jumpPower;
float horizontalvalue;
bool isRunning;
bool facingRight = true;
float runSpeedModifier = 2f;
[SerializeField] bool isgrounded;
[SerializeField] bool jump;
void Awake()
{
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}
void Update()
{
horizontalvalue = Input.GetAxisRaw("Horizontal");
if(Input.GetKeyDown(KeyCode.LeftShift))
{
isRunning = true;
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
isRunning = false;
}
if (Input.GetKeyDown(KeyCode.Space))
{
jump = true;
}
else if (Input.GetKeyUp(KeyCode.Space))
{
jump = false;
}
}
void FixedUpdate()
{
GroundCheck();
Move(horizontalvalue, jump);
}
void GroundCheck()
{
isgrounded = false;
Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheckCollider.position, groundCheckRadius, groundLayer);
if (colliders.Length > 0)
isgrounded = true;
}
void Move(float dir,bool jumpFlag)
{
if(isgrounded && jumpFlag)
{
isgrounded = false;
jumpFlag = false;
rb.AddForce(new Vector2(0f,jumpPower));
}
#region move & run
float xVal = dir * speed * 100 * Time.fixedDeltaTime;
if (isRunning)
{
xVal *= runSpeedModifier;
}
Vector2 targetVelocity = new Vector2(xVal, rb.velocity.y);
rb.velocity = targetVelocity;
if (facingRight && dir < 0)
{
transform.localScale = new Vector3(-5, 5, 1);
facingRight = false;
}
else if (!facingRight && dir > 0)
{
transform.localScale = new Vector3(5, 5, 1);
facingRight = true;
}
animator.SetFloat("Xvelocity", Mathf.Abs(rb.velocity.x));
#endregion
}
}
Could you please format the code so it's easier to read
@cordial_studios What type of game are you making? can you use raycast instead, this might be a better way of checking if you are grounded.
(i updated the code because i think i chnged it around a little and it didn't work
Try using raycast to detect the ground
Answer by In0sc0p3dJFK · Jul 09, 2021 at 05:09 AM
Look at line 49:
void FixedUpdate()
{
GroundCheck();
Move(horizontalvalue, jump);
}
void GroundCheck()
{
isgrounded = false;
}
Your ground check function is always being called every frame because it's being called inside of FixedUpdate();
. And inside of your ground check, you are always setting your isGrounded bool to false. Thus, your isGrounded bool is going to be false indefinitely. Try taking it out or putting it somewhere else.
where should i move it to because none of the places im moving it to are aworking
Your answer
Follow this Question
Related Questions
Player passenger moving when being pushed by two platforms 0 Answers
Problems with player jumping (Welcome to Unity Answers The best place to ask and answer questions ) 0 Answers
Player gets stuck on moving platform 0 Answers
how to make a plataforme game with 3d depth 1 Answer
platformEffector2D pushing character upward,Platform Effector 2D pushing character if Keypressed 0 Answers