- Home /
problem with ground check
hello, so I have been working on this little project and I am still very new to game development and programming and I have gotten this issue that I can not FIGURE OUT. where I need to put in a float but it wont accept it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private bool isGrounded;
public float jumpForce;
private Rigidbody rb;
public float speed;
private float XInput;
private float ZInput;
public Transform groundCheck;
private float radius;
public LayerMask whatIsGround;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
XInput = Input.GetAxis("Horizontal");
ZInput = Input.GetAxis("Vertical");
rb.velocity = new Vector3(XInput * speed, rb.velocity.y, ZInput * speed);
isGrounded = Physics.OverlapSphere(groundCheck.position, radius, whatIsGround); //<-- it wont accept "radius"
if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
}
}
}
please help
Answer by BKN_Nahar · May 14 at 09:20 AM
Hey, the problem is pretty simple -- OverlapSphere shows all the colliders it collides with while isGrounded is a bool and thus the code doesnt compile... to get a bool you must use "CheckSphere".. I have attached the script below, you will understand by just going through it ( I havent changed anything except the overlapSphere method, so you can copy paste the whole code without any worries). CODE >>>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private bool isGrounded;
public float jumpForce;
private Rigidbody rb;
public float speed;
private float XInput;
private float ZInput;
public Transform groundCheck;
private float radius;
public LayerMask whatIsGround;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
XInput = Input.GetAxis("Horizontal");
ZInput = Input.GetAxis("Vertical");
rb.velocity = new Vector3(XInput * speed, rb.velocity.y, ZInput * speed);
isGrounded = Physics.CheckSphere(groundCheck.position, radius, whatIsGround);
if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
}
}
}
thank you for your response @BKN_Nahar . I am no longer getting any type of errors but still the jump mechanic is still not working. I also do have to note that the project is 3d
You're welcome.. Hmm, that’s weird, maybe try increasing the groundCheck radius and also don’t forget to add the layerMask to your ground.
It will also make more sense if you change “Vector2.up” to “Vector3.up” where you apply the jump force.
Try using these tips, if they still don’t work, then feel free to let me know again.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Missing Reference Exception problem,Junior Programmer course : Missing Reference Exception problem 3 Answers
How to move character controller only on X and Z axis but not on Y. 1 Answer
CharacterController.Move precision 1 Answer
How can I call Application.LoadLevel in a Coroutine? 0 Answers