Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
1
Question by CodyCantEatThis · Oct 21, 2019 at 06:00 AM · 3dcharactercontrollerisgrounded

Character Controller isGrounded value flickers on and off

So I am trying to make a 3D platformer game similar to the Mario games. I want my character to be able to run around, jump, and dive. After a dive, the player should slide on the ground while still in the diving animation for a little bit. When the character runs around, isGrounded is always true; I have no problems with this. However, when my character is sliding on the ground after diving, the isGrounded value keeps flickering on and off. I also noticed that the y position of my character will alternate between 0.2049 and 0.204899 when this happens. I've tried different things to try and get it to work but I'm out of ideas. I think it could be the way I set the y velocity to be -4 when I'm grounded to keep my character grounded, or it could be because I changed the size and skinwidth of my character controller for different animations.

This is my code if it helps (sorry if it it messy):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {
     public float moveSpeed;
     public float jumpForce;
     public float doubleJumpForceScalar;
     public CharacterController controller;
     public float gravityScale;
     
     public int jumpBuffer;
     public Animator anim;
     public Transform pivot;
     public float rotateSpeed;
     public GameObject playerModel;
     public float speed;
     public float diveSpeed;
     public float diveForceScalar;
     public int diveCountMax;
     public float ccHeightGrounded;
     public float ccHeightDive;
     public float ccHeightDoubleJump;
     public Vector3 ccCenterGrounded;
     public Vector3 ccCenterDive;
     public Vector3 ccCenterDoubleJump;
     public float ccSkinWidth;
     public float ccSkinWidthDive;
     public float ccRadius;
     public float ccRadiusDive;
     public float diveFlipScalar;
     public float diveSlow;
     public bool divedGroundJump;
     public bool bonk;
     public float bonkSpeed;
 
     public bool grounded;
     public bool dived;
 
     private bool hasDoubleJump;
     private Vector3 moveDirection;
     private bool jumped;
     private int jumpBufferCount = 0;
     private bool divedOutOfJump;
     private int diveCount;
     private bool firstAirFrame;
     private int diveCCCount;
     public bool divedGround;
     public float bonkJumpScalar;
 
     // Start is called before the first frame update
     void Start()
     {
         controller = GetComponent<CharacterController>();
         grounded = controller.isGrounded;
         hasDoubleJump = true;
     }
 
     // Update is called once per frame
     void Update()
     {
         float yStore = moveDirection.y; //Stores the value of the y velocity from last frame
         moveDirection = (transform.forward * Input.GetAxisRaw("Vertical")) + (transform.right * Input.GetAxisRaw("Horizontal"));
         moveDirection = moveDirection.normalized * moveSpeed;
         moveDirection.y = yStore;
 
         grounded = controller.isGrounded;
         if (controller.isGrounded)
         {
             jumped = false;
             hasDoubleJump = true;
 
             if (dived)
             {
                 moveDirection.y = -4f; // <--- I think this might be the problem
                 divedGround = true;
                 divedGroundJump = false;
             }
             else if(bonk) //bonk checks if you hit a wall while diving
             {
                     dived = false;
                     divedGround = false;
                     moveDirection.y = jumpForce * bonkJumpScalar;
                     jumped = true;
                     bonk = false;
             }
             else if(!divedGround) //divedGround is when the character is sliding on the ground after a dive
             {
 
 
                 GetComponent<CharacterController>().height = ccHeightGrounded;
                 GetComponent<CharacterController>().center = ccCenterGrounded;
                 GetComponent<CharacterController>().skinWidth = ccSkinWidth;
                 GetComponent<CharacterController>().radius = ccRadius;
 
                 dived = false;
                 divedGround = false;
                 diveCount = 0;
                 diveCCCount = 0;
                 divedOutOfJump = false;
                 
                 firstAirFrame = true;
                 divedGroundJump = false;
                 bonk = false;
 
                 if (jumpBufferCount < jumpBuffer && jumpBufferCount > 0)
                 {
                     moveDirection.y = jumpForce;
                     jumped = true;
                 }
                 jumpBufferCount = 0;
 
 
                 moveDirection.y = -4f;
 
                 if (Input.GetButtonDown("Jump"))
                 {
                     moveDirection.y = jumpForce;
                     jumped = true;
                 }
 
                 if (Input.GetMouseButtonDown(0) || Input.GetButtonDown("Dive"))
                 {
                     dived = true;
                 }
             }
         }
         else //if isGrounded is false (character in the air)
         {
 
              if (bonk)
             {
                 moveDirection.x = 0.0f;
                 moveDirection.z = 0.0f;
                 moveDirection.y += (Physics.gravity.y * gravityScale * Time.deltaTime);
                 controller.Move(moveDirection * Time.deltaTime);
                 controller.Move(-playerModel.transform.forward * bonkSpeed * Time.deltaTime);
                 GetComponent<CharacterController>().height = ccHeightGrounded;
                 GetComponent<CharacterController>().center = ccCenterGrounded;
                 //GetComponent<CharacterController>().skinWidth = ccSkinWidth;
                 GetComponent<CharacterController>().radius = ccRadius;
             }
             else
             {
 
                 if (moveDirection.y < -0.5f && firstAirFrame)
                 {
                     moveDirection.y = -0.5f;
                     firstAirFrame = false;
                 }
 
                 if (jumpBufferCount > 0 && jumpBufferCount < jumpBuffer)
                 {
                     jumpBufferCount++;
                 }
 
                 if (hasDoubleJump == false)
                 {
                     anim.SetBool("doubleJumped", hasDoubleJump);
                 }
 
                 if (Input.GetButtonDown("Jump") && hasDoubleJump == true)
                 {
                     moveDirection.y = jumpForce * doubleJumpForceScalar;
                     hasDoubleJump = false;
                     anim.SetBool("doubleJumped", !hasDoubleJump);
                     dived = false;
                     GetComponent<CharacterController>().height = ccHeightDoubleJump;
                     GetComponent<CharacterController>().center = ccCenterDoubleJump;
                 }
                 else if (Input.GetButtonDown("Jump") && jumpBufferCount == 0 && hasDoubleJump == false)
                 {
                     jumpBufferCount++;
                 }
 
                 if ((Input.GetMouseButtonDown(0) || Input.GetButtonDown("Dive")) && !dived && !divedOutOfJump)
                 {
                     dived = true;
                     divedOutOfJump = true;
                 }
 
                 moveDirection.y += (Physics.gravity.y * gravityScale * Time.deltaTime);
             }
         }
 
         if(divedGround)
         {
             
 
             if (Input.GetButtonDown("Jump"))
             {
                 dived = false;
                 divedGround = false;
                 moveDirection.y = jumpForce * diveFlipScalar;
                 jumped = true;
                 //divedGroundJump = true;
             }
             
             if(diveSlow > diveSpeed)
             {
                 dived = false;
                 divedGround = false;
                 moveDirection.y = jumpForce * diveFlipScalar;
                 jumped = true;
                 //divedGroundJump = true;
             }
         }
 
 
         if (!dived && !bonk && !divedGroundJump)
         {
             GetComponent<CharacterController>().skinWidth = ccSkinWidth;
             //Move the player in different directions based on the camera look direction
             if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0)
             {
                 transform.rotation = Quaternion.Euler(0f, pivot.rotation.eulerAngles.y, 0f);
                 Quaternion newRotation = Quaternion.LookRotation(new Vector3(moveDirection.x, 0f, moveDirection.z));
                 playerModel.transform.rotation = Quaternion.Slerp(playerModel.transform.rotation, newRotation, rotateSpeed * Time.deltaTime);
             }
 
             controller.Move(moveDirection * Time.deltaTime);
         }
         else if(dived)
         {
             GetComponent<CharacterController>().skinWidth = ccSkinWidthDive;
             GetComponent<CharacterController>().radius = ccRadiusDive;
             if (diveCCCount < 10)
             {
                 GetComponent<CharacterController>().height = ccHeightGrounded;
                 GetComponent<CharacterController>().center = ccCenterGrounded;
                 diveCCCount++;
             }
             else
             {
                 GetComponent<CharacterController>().height = ccHeightDive;
                 GetComponent<CharacterController>().center = ccCenterDive;
             }
 
             if (diveCount < diveCountMax)
             {
                 if (!grounded)
                 {
                     diveCount++;
                 }
                 else
                 {
                     moveDirection.y = -0.5f;
                 }
 
 
                 moveDirection.y += jumpForce * diveForceScalar;
             }
 
 
             moveDirection.x = 0f;
             moveDirection.z = 0f;
             controller.Move(moveDirection * Time.deltaTime);
 
 
             if (!divedGround)
             {
                 diveSlow = 0.1f;
                 controller.Move(playerModel.transform.forward * diveSpeed * Time.deltaTime);
             }
             else
             {
                 if (diveSlow < diveSpeed)
                 {
                     controller.Move(playerModel.transform.forward * (diveSpeed - diveSlow) * Time.deltaTime);
                     diveSlow += 0.025f;
                 }
 
             }
         }
 
         speed =  Mathf.Abs(Input.GetAxisRaw("Vertical")) +  Mathf.Abs(Input.GetAxisRaw("Horizontal"));
 
         anim.SetBool("isGrounded", controller.isGrounded);
         anim.SetFloat("Speed", speed);
         anim.SetBool("jumped", jumped);
         anim.SetBool("dived", dived);
         anim.SetBool("divedGround", divedGround);
         anim.SetBool("bonk", bonk);
     }
 
     void OnTriggerEnter(Collider other)
     {
         if(other.tag == "Ceiling")
         {
             moveDirection.y = 0.75f;
             controller.Move(moveDirection * Time.deltaTime);
         }
     }
 
     void OnControllerColliderHit(ControllerColliderHit hit)
     {
         if(hit.gameObject.tag == "Level" && dived)
         {
             dived = false;
             bonk = true;
         }
 
         if(hit.gameObject.tag == "Respawn")
         {
             transform.position = new Vector3(0f, 2f, 0f);
         }
 
         if (hit.gameObject.tag == "Ceiling")
         {
             moveDirection.y = 1f;
             controller.Move(moveDirection * Time.deltaTime);
         }
     }
 
 
 }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by parag1111 · Aug 02, 2020 at 06:08 PM

At line number 70, You may want to try adding constant push to negative direction of y. E.G. moveDirection.y =- gravityScale * Time.deltaTime;

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

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

227 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Jumping with slight air control while retaining velocity 1 Answer

SmoothDamp function affecting controller.isGrounded 0 Answers

Hold the jump button to jump higher with CharacterController 1 Answer

Help! Collision will not work, but all others will 0 Answers

IsGrounded flickers when hitting a wall 0 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