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);
}
}
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.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 ;
Add CharaterController on your player;
private CharacterController cc;
void Start()
{
cc = GetComponent<CharacterController>();
}
void Update()
{
if(cc.isGrounded)
{
//do something
}
}