- Home /
Rigidbody and Charactercontroller.isGrounded not working at all.
So I have a problem where I can't apply any force or use my rigidbody at all. it worked normally in another project but it doesn't work in this one at all, same goes with charactercontroller.isGrounded. and this both of them doesn't work at all. I can't implement jumping in my game.
using System.Collections;
using System.Collections.Generic;
using System.Net.Sockets;
using UnityEngine;
[RequireComponent(typeof(Animator))]
[RequireComponent(typeof(CharacterController))]
public class TPcontrollertest : MonoBehaviour
{
public float movementSpeed = 6f;
public float jumpSpeed = 12f;
public float gravity = -9.81f;
public CharacterController controller;
private Vector3 direction = Vector3.zero;
private Vector3 move;
private Animator anim;
private Rigidbody rbody;
float horizontal;
float vertical;
void Awake()
{
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
rbody = GetComponent<Rigidbody>();
}
void Update()
{
anim.SetFloat("move", direction.z);
anim.SetFloat("turn", direction.x);
//movement
// jump = Input.GetAxisRaw("Jump");
horizontal = Input.GetAxisRaw("Horizontal");
vertical = Input.GetAxisRaw("Vertical");
direction = new Vector3(horizontal, 0f, vertical).normalized;
move = transform.right * horizontal + transform.forward * vertical;// + transform.up * jump;
if (direction.magnitude >= 1.0f)
{
direction = direction.normalized;
}
controller.Move(move * movementSpeed * Time.deltaTime);
//ground check to prevent spam jumps
if ((controller.collisionFlags & CollisionFlags.Below) == 0)
Debug.Log("air");
if (controller.isGrounded)
Debug.Log("onground");
else Debug.Log("air");
Jump();
dodge();
}
void Jump()
{
if (Input.GetButtonDown("Jump") && Input.GetAxisRaw("Horizontal") != 0)
{
anim.SetTrigger("jump");
rbody.AddRelativeForce(0f, 3f, 0f);
}
if (Input.GetButtonDown("Jump") && Input.GetAxisRaw("Vertical") != 0)
{
anim.SetTrigger("jump");
rbody.AddRelativeForce(0f, 3f, 0f);
}
if (Input.GetButtonDown("Jump"))
{
anim.SetTrigger("jump");
rbody.AddRelativeForce(0f, 3f, 0f);
}
}
void dodge()
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
anim.SetTrigger("dodge");
}
}
}
this code contain movement, jump, and dodge. since Rigidbodies isn't working, I can't add force to my jump or dodge. and in the debug log, my character controller always returns "air". I don't know how to fix this tbh. and I seriously need to fix them.
where is your bool isGrounded anyway? I cant see where its being initialised
isn't Controller.isGrounded, and already declared thing ? or do I need to do it first ? if so, would you tell me what I should do then ?
Controller.isGrounded should have a layermask and a groundcheck like so: IsGrounded = Physics.CheckSphere(GroundCheck.position, GroundDistance, groundmask);
The groundcheck is an empty gameobject under the player's feet, which is always touching the ground. The grounddistance is a float (0.4 should be fine) and the groundmask is a public layermask. This is the layer assigned to all ground. So in summary, isGrounded is equal to true when the groundCheck object is touching the floor, and it is touching ground with a specified layer.
Can you show the controller script. controller.isGrounded
is only being called once and doesnt do anything except Debug.Log("onground");
Answer by Llama_w_2Ls · Jul 27, 2020 at 04:37 PM
public CharacterController controller;
public float Walkspeed = 6f;
public float Runspeed = 12f;
public float gravity = -9.81f;
public Transform GroundCheck;
public float GroundDistance = 0.4f;
public LayerMask groundmask;
public float JumpHeight = 3f;
Vector3 velocity;
bool IsGrounded;
// Update is called once per frame
void Update()
{
IsGrounded = Physics.CheckSphere(GroundCheck.position, GroundDistance, groundmask);
if (IsGrounded && velocity.y < 0) //fall
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
if (Input.GetKey("w") || Input.GetKey("s") || Input.GetKey("d") || Input.GetKey("a")) // Walk
{
controller.Move(move * Walkspeed * Time.deltaTime);
}
You dont need a rigidbody and a character controller. Just a character controller should allow you to jump much more simply, without having to specify the exact force and direction required to move your player.
if (Input.GetButtonDown("Jump") && IsGrounded) // Jump and gravity
{
velocity.y = $$anonymous$$athf.Sqrt(JumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.$$anonymous$$ove(velocity * Time.deltaTime);
And heres how you jump (inside of update method)
there's only something that I want to ask. is there a way to prevent the collider from bouncing when it hits the ground after jumping ? cuz I'm putting a jump force of 6-7 and it jumps fine. but it bounces when it reaches the ground.
You could add a physics material to the ground and set the bounciness to 0
Your answer
![](https://koobas.hobune.stream/wayback/20220613004141im_/https://answers.unity.com/themes/thub/images/avi.jpg)