• 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 strideynet · Dec 25, 2013 at 05:30 PM · physicsgravity

IsGrounded causing problems.

I have looked at the other questions but what they recommend doesnt seem to work. This code is the physics part of my character movement. It just randomly jumps and I put a debug log code into it to monitor the variables and it seems half the time im grounded it thinks im ungrounded? Oh btw, cc is my charecter controller and anim is the animation controller. When I say it randomly jumps I mean the jump animation is being triggered but I'm on the ground. using UnityEngine; using System.Collections;

 public class Newmove : MonoBehaviour {
     
     // This component is only enabled for "my player" 
     
     public float speed = 10f;
     public float jumpSpeed = 6f;
 
     Vector3 direction = Vector3.zero;
     float   verticalVelocity = 0;
     CharacterController cc;
     Animator anim;
     
     // Use this for initialization
     void Start () {
         cc = GetComponent<CharacterController>();
         anim = GetComponent<Animator>();
     }
     
     // Update is called once per frame
     void Update () {
         
         direction = transform.rotation * new Vector3( Input.GetAxis("Horizontal") , 0, Input.GetAxis("Vertical") );
         
 
         if(direction.magnitude > 1f) {
             direction = direction.normalized;
         }
         
         anim.SetFloat("Speed", direction.magnitude);
         
 
         if(cc.isGrounded && Input.GetButton("Jump")) {
             verticalVelocity = jumpSpeed;
         }
     }
     
 
     void FixedUpdate () {
         
 
         Vector3 dist = direction * speed * Time.deltaTime;
         
         if(cc.isGrounded && verticalVelocity < 0) {
 
             anim.SetBool("Jumping", false);
             
             verticalVelocity = Physics.gravity.y * Time.deltaTime;
         }
         else {
 
             if(Mathf.Abs(verticalVelocity) > jumpSpeed*0.75f) {
                 anim.SetBool("Jumping", true);
             }
             
             verticalVelocity += Physics.gravity.y * Time.deltaTime;
         }
         
 
         dist.y = verticalVelocity * Time.deltaTime;
         
 
         cc.Move( dist );
     }
 }
Comment
Add comment · Show 7
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 sdgd · Dec 25, 2013 at 05:32 PM 0
Share

are you by any means trying to see if you are grounded upside down?

like on the other side of the planet?

avatar image strideynet · Dec 25, 2013 at 05:37 PM 0
Share

No. I'm on a totally flat piece of ground. When I'm in scene view and the game is playing the charecter is like hovering ??

avatar image strideynet · Dec 26, 2013 at 07:24 AM 0
Share

Hullo anybody? I'm trying all sorts with the oddest results. This is really painting me for the last hours.

avatar image KellyThomas · Dec 26, 2013 at 07:54 AM 0
Share

It just randomly jumps and I put a debug log code into it to monitor the variables and it seems half the time im grounded it thinks im ungrounded?

Then I think we should start with the code for your ground check, if isGrounded is a property please post that code, if it is a variable please post the code that deter$$anonymous$$es it's value.

avatar image strideynet · Dec 26, 2013 at 08:02 AM 0
Share

It's part of the charecter controller. The default unity one.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by emc233 · Dec 26, 2013 at 08:06 AM

I think you have some errors in your logic. When the character is grounded, vertical velocity should be equal to zero. Do you want to require that the character is grounded before you jump? Make sure the variable direction is normalized.

Comment
Add comment · Show 6 · 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 strideynet · Dec 26, 2013 at 08:12 AM 0
Share

Okay, will try that.

avatar image strideynet · Dec 26, 2013 at 08:17 AM 0
Share

Still gettign weird glitch. Only occurs when I'm in the idle anim state, it just does the jump animation on the ground? When I'm running it does not occur?

avatar image emc233 · Dec 26, 2013 at 05:35 PM 0
Share

If you are on the ground make sure your character is idle state and not jumping?

avatar image strideynet · Dec 27, 2013 at 08:05 AM 0
Share

Here's the basics of my state machine idle to jump = if jump (bool). Jump to idle = if jump(bool) false.

avatar image strideynet · Dec 27, 2013 at 08:08 AM 0
Share

It does a weird switch thing. I'll be idling on the ground and then I will just play the jump animation. But not physically jump upwards, if I'm in state machine view I can see the Jumping variable flickering every now and again. This jumping variable is controlled by the script in the OP.

Show more comments
avatar image
0

Answer by strideynet · Dec 28, 2013 at 07:17 AM

There's nothing wrong with the jump animation. It's that the jump animations is being triggered randomly. What I meant that the physics side isn't jumping with the random jump animations. Meaning the part of the code where it sets anim.SetBool(True) must be broken in some way. The state machine is fine also. What I need to know is why the charecter controller thinks it is grounded half the time and ungrounded the other?

Comment
Add comment · Show 3 · 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 strideynet · Dec 28, 2013 at 11:49 AM 0
Share

Sorry this was meant to be a comment.

avatar image strideynet · Dec 28, 2013 at 01:02 PM 0
Share

Guys this is absolutly desperate. I need to know why my charecter controller is returning trues and falses for IsGrounded even though it seems to be touching the ground?

avatar image NickP_2 · Jan 26, 2014 at 01:54 AM 0
Share

I'm having the same problem, did you find a solution yet?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

How to make telephone wires 1 Answer

Edit this script so that on the second jump change the gravity to 5 2 Answers

How can I make a game object move in parabolic motion as if it were under gravity? 2 Answers

Change direction of gravity for a specific instance of a prefab 1 Answer

Setting a jump force on a rigidbody 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