- Home /
How do I check if a character is grounded?
Hi, I'm very new to Unity & I wanted to do a prototype of my game with the things I learned from Create with Code Live. & the controls are fine but I can jump infinitly. I've searched on Youtube but none of them are simple or good for my noob code. I know you have to use a bool but its too complicated for me. Anyone know a way thats not too complicated & works with this:
ublic class PlayerController : MonoBehaviour { float leftRight; float jump; public float speed; public float jumpSpeed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// Assigns inputs
leftRight = Input.GetAxis("Horizontal");
jump = Input.GetAxis("Jump");
// walk
transform.Translate(Vector3.right * Time.deltaTime * speed * leftRight);
// jump
transform.Translate(Vector3.up * Time.deltaTime * jumpSpeed * jump);
}
}
Answer by EliezerYT · Apr 03, 2020 at 01:06 AM
Agrega estas variables
public bool m_Grounded ; public float k_GroundedRadius; public Transform m_GroundCheck; public LayerMask m_WhatIsGround; colliders = Physics.OverlapSphere(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround); for (int i = 0; i < colliders.Length; i++) { if (colliders[i].gameObject != gameObject) m_Grounded = true; }
como usarlo 1- cree un objeto vacio y situelo en los pies de su personaje y este objeto asignelo a m_GroundCheck 2- agregue un radio de verificacion del piso cambiando la float k_GroundedRadius; 3- seleccione la Layer de conque puede saltar su personaje (si pone default podra saltar con cualquier collider que tenga el layer Default puede crear un layer especifico llamado piso y asignarlos a los objetos que quieres que el jugador pueda saltar
3- si esta en el suelo o no se reflejara en el bool m_Grounded ;
Answer by guy1687621 · Apr 03, 2020 at 05:26 AM
Add CharaterController on your player;
private CharacterController cc;
void Start()
{
cc = GetComponent<CharacterController>();
}
void Update()
{
if(cc.isGrounded)
{
//do something
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612230401im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
how to control gravity for addforce ? 1 Answer
Limiting random actions (c#) (newbie) 1 Answer
My Player's Jump and Gravity Speed is not the when how i want it to be 2 Answers