Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 guilhermevianagui23 · Jan 22, 2019 at 04:30 AM · shaderbugglitch

Collision issues SOLVED

when chaging between two Ground tagged objects my isGrounded boolean gets checked as false ,even to its on a ground object ,here is the code:

  public float speed;
     public float horizontalSpeed = 2f;
     public int jumpHeight = 300;
     public bool isGrounded = true;
 
 
     private Animator animator;
     private Rigidbody rigidBody;
 
 
     void Start()
     {
 
         animator = GetComponent<Animator>();
         rigidBody = GetComponent<Rigidbody>();
     }
 
     void Update()
     {
 
         Movememt();
 
     }
 
     void Movememt()
     {
 
         animator.SetFloat("WalkSide", Input.GetAxisRaw("Horizontal"));
         animator.SetFloat("WalkBlend", Input.GetAxisRaw("Vertical"));
         animator.SetFloat("Run", Input.GetAxisRaw("run"));
         animator.SetBool("Jump", isGrounded);
 
         if(Input.GetAxisRaw("Vertical") != 0 || Input.GetAxisRaw("Horizontal") != 0)
         {
             animator.SetBool("Walking", true);
         }
         else
         {
             animator.SetBool("Walking", false);
         }
 
 
         if (animator.GetFloat("Run") > 0)
         {
             speed = 10f;
 
         }
         else
         {
             speed = 5f;
 
         }
 
         //Movement Front Back
         float v = Input.GetAxisRaw("Vertical") * speed * Time.deltaTime;
 
         transform.Translate(0, 0, v);
         //end movement Front back
 
         //Movement Left Right
         float h = Input.GetAxisRaw("Horizontal") * speed * Time.deltaTime;
         transform.Translate(h, 0, 0);
         //End movement left right
 
 
         //Movement Mouse
         float hMouse = horizontalSpeed * Input.GetAxisRaw("Mouse X");
         transform.Rotate(0, hMouse, 0);
         //End Movement mouse
 
         //jump
 
         if(Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
         {
             rigidBody.AddRelativeForce(Vector3.up * jumpHeight);
         }
 
         //end jump
 
 
     }
 
     void OnCollisionEnter(Collision collision)
     {
         if(collision.gameObject.tag == "Ground")
         {
             isGrounded = true;
 
         }
     }
     void OnCollisionExit(Collision collision)
     {
         if(collision.gameObject.tag == "Ground")
         {
             isGrounded = false;
 
         }
     }
 }
 
 
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
1
Best Answer

Answer by Captain_Pineapple · Jan 22, 2019 at 09:19 AM

Hey there,

your issue is that you can be in collision with more that 1 ground-tagged object at once. Now imagine the following: you collide with a ground object. -> isGrounded will be true now. Then you enter a second ground collision somewhere at the edge between the ground objects. -> isGrounded will be set to true but will already be true. Then after this you leave the first ground collision. -> isGrounded will be set to false even though you are on ground.

As a solution: add some List<Collider> myGroundContacts where you add and remove all ground tagged collider that you came in contact with. Only if the list is empty on leaving a collider you will truly be off the ground.

A sidenote: When checking for tags i'd recommend to use the builtin function and write collision.gameObject.CompareTag("Ground") since it is around 10x faster than getting the tag and comparing it. At your current level this will not be an issue but might be when you do hundreds of tag comparisons in one frame.

Hope this helps.

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

Answer by guilhermevianagui23 · Jan 22, 2019 at 03:53 PM

@Captain_Pineapple Thank your very much! , i would never have this idea , thx

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 Captain_Pineapple · Jan 22, 2019 at 05:03 PM 0
Share

Cool that i could help you. Consider marking the post as answer when your question is resolved.

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

110 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

Related Questions

I can't find my asset store purchase and haven't gotten a receipt 1 Answer

Missing objects 1 Answer

asset store login bug? 1 Answer

How can I stop the edges between my vertex-snapped cubes flickering? 0 Answers

My Transparency Shader Is No Longer Working on Mobile? 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