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
1
Question by zefrof · Apr 24, 2013 at 12:39 AM · c#rigidbodycharactercontrollerjumpingisgrounded

Using isGrounded with a RigidBody

So I needed my PlayerCharacter to have a rigidbody, and when using the built in character controller with a rigidbody it creates weird results. So I set out to create a charactercontroller around a rigidbody. This is what I have so far in C#:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerCharacter : MonoBehaviour {
     
     public CharacterController contoller;
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         
         if(Input.GetKey(KeyCode.W)){
             ForwardMovement ();
         }
         
         if(Input.GetKey(KeyCode.S)){
             BackwardMovement ();
         }
         
         if(Input.GetKey(KeyCode.D)){
             MovingRight ();
         }
         
         if(Input.GetKey(KeyCode.A)){
             MovingLeft ();
         }
         
         if(Input.GetKey (KeyCode.Space)){
             Jump ();
         }
     }
     
     void ForwardMovement (){
         rigidbody.AddForce(transform.forward * 100);
     }
     
     void BackwardMovement (){
         rigidbody.AddForce(-transform.forward * 100);
     }
     
     void MovingRight (){
         rigidbody.AddForce(transform.right * 100);
     }
     
     void MovingLeft (){
         rigidbody.AddForce(-transform.right * 100);
     }
     
     void Jump (){
         rigidbody.AddForce(transform.up * 100);
     }
 }

Moving forward, backward, left, and right are all working fine. Using the MouseLook script that on the original charactercontroller works just fine for looking around with the mouse. The problem i'm having is the jump. It goes up just fine and comes down just fine but I can press spacebar whenever and it'll move up. Is there a way to use charactercontroller variable and then use isGrounded or do I need to do it a different way since it's a rigidbody? Thanks for any and all help!

Comment
Add comment
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

2 Replies

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

Answer by aldonaletto · Apr 24, 2013 at 12:46 AM

You could use a short raycast downwards: take a look at this question.

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 zefrof · Apr 24, 2013 at 12:53 AM 0
Share

Will said raycast still work regardless? For example if the player started at 0 on the y axis but walks up a hill and is at 20 on the y axis then jumps will it still register as grounded?

avatar image aldonaletto · Apr 24, 2013 at 11:01 PM 0
Share

Yes, the raycast is done from the player position in the down direction with a limited range: if nothing is hit inside this range, the rigidbody is considered ungrounded. The range is defined so that it goes only a little below the player's feet, thus the absolute Y of the player doesn't matter.

avatar image ZorNiFieD · Apr 24, 2013 at 11:05 PM 0
Share

but remember, because you are racasting from the rigidbody, it may be hitting the character controller. You shouldn't have a rigidbody and character controller on the same gameobject, this is why you are seeing unpredictable results as my original answer suggested in a shorter way.

avatar image zefrof · Apr 25, 2013 at 04:37 AM 0
Share

Thank you aldonaletto

avatar image
1

Answer by ZorNiFieD · Apr 24, 2013 at 02:21 AM

I thought you weren't supposed to use a rigidbody with a CharacterController?

Comment
Add comment · Show 1 · 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 Loius · Apr 24, 2013 at 02:31 AM 0
Share

yeah - the CC is already a special rigidbody

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

14 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

Related Questions

How to change CC script to Rigidbody script 1 Answer

controller.Move doesn't stay grounded when walking down slope 3 Answers

Fly Mode 2D + Rigid Body vs CharacterController problems 0 Answers

How do I make my Rigidbody player not get stuck on walls? 2 Answers

attached.Rigidbody isn't working with a ChactacterController? -1 Answers


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