Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mahmoudadel200190 · Jul 26, 2020 at 10:37 PM · rigidbodycollidercharactercontroller

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.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Llama_w_2Ls · Jul 27, 2020 at 01:51 PM 0
Share

where is your bool isGrounded anyway? I cant see where its being initialised

avatar image mahmoudadel200190 Llama_w_2Ls · Jul 27, 2020 at 02:44 PM 0
Share

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 ?

avatar image Llama_w_2Ls mahmoudadel200190 · Jul 27, 2020 at 04:34 PM 0
Share

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.

avatar image segy0296 · Jul 27, 2020 at 03:54 PM 0
Share

Can you show the controller script. controller.isGrounded is only being called once and doesnt do anything except Debug.Log("onground");

avatar image mahmoudadel200190 segy0296 · Jul 27, 2020 at 11:02 PM 0
Share

this was my entire script.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Llama_w_2Ls · Jul 27, 2020 at 04:38 PM 0
Share
 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)

avatar image mahmoudadel200190 Llama_w_2Ls · Jul 27, 2020 at 11:15 PM 0
Share

yep this works. thanks a lot.

avatar image mahmoudadel200190 Llama_w_2Ls · Jul 27, 2020 at 11:46 PM 0
Share

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.

avatar image Llama_w_2Ls mahmoudadel200190 · Jul 28, 2020 at 10:35 AM 0
Share

You could add a physics material to the ground and set the bounciness to 0

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

221 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unparent Character Controller from rigidbody? 1 Answer

RigidBody Character Controller Sinking into Floor? 1 Answer

My Character controller is being push a little bit when i'm about the get the coins. How can i possibly detect collision and destroy the coins without actually getting affected by the colliding physics? 1 Answer

Rigidbody bounce or character controller istrigger issues 0 Answers

Move Player With Capsule Collider 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges