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 Jotunn05 · May 25, 2017 at 02:57 PM · scripting problemgroundedground detection

Player keeps switching between "grounded" and "not grounded" while running

When I start running in either way the player sometimes detects he is touching the ground while other times he doesn't. so he constantly keeps switching between running and jumping animations. I have two bools set, One for jumping and the other for running. I have also set the transitions correctly, triple checked everything.

these are the scripts I'm using:

_This one is linked to the player:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerManager : MonoBehaviour
 {
     Animator anim;
     public ButtonMovement leftButton, rightButton; 
     int buttonL, buttonR, lRSum;
     public bool onGround = false;
 
 
     void Start ()
     {
         anim = this.GetComponent<Animator>();
     }
     
     void FixedUpdate () 
     {
         buttonL = leftButton.isOn;
         buttonR = rightButton.isOn;
         lRSum = buttonL + buttonR; 
 
         if (lRSum > 0 && onGround)
         {
             anim.SetBool("Move", true);
         } else if (lRSum == 0 && onGround)
         { 
             anim.SetBool("Move", false);
         }
     }
 
     void OnCollisionEnter2D (Collision2D obj)
     {
         if (obj.gameObject.tag == "ground")
         {
             anim.SetBool("Jump", false);
             onGround = true;
         }
     }
 
     void OnCollisionExit2D(Collision2D obj)
     {
         if (obj.gameObject.tag == "ground")
         {
             anim.SetBool("Jump", true);
             onGround = false;
         }
     }
 }

I have all floor objects tagged "ground" just like shown on script.

_This one is linked to the buttons on screen since I'm making a mobile game:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ButtonMovement : TouchManager
 {
     public enum type {LeftButton, RightButton, JumpButton};
     public type buttonType;
 
     public int isOn = 0;
     public float jumpHeight = 0.0f, moveSpeed = 0.0f;   
 
     public GameObject playerObject = null;
     Rigidbody2D playerRigidBody;
 
     public GUITexture buttonTexture = null;
 
     void Start ()
     {
         playerRigidBody = playerObject.GetComponent<Rigidbody2D>();
     }
     
     void Update ()
     {
         TouchInput(buttonTexture);
     }
 
     void OnFirstTouchBegan ()
     {
         switch (buttonType)
         {
             case type.JumpButton:
                 if (playerObject.GetComponent<PlayerManager>().onGround)
                 {
                     playerRigidBody.AddForce(Vector2.up * jumpHeight, ForceMode2D.Impulse);
                 }
                 break;
         }
     }
 
     void OnSecondTouchBegan () 
     {
         switch (buttonType)
         {
             case type.JumpButton:
                 if (playerObject.GetComponent<PlayerManager>().onGround)
                 {
                     playerRigidBody.AddForce(Vector2.up * jumpHeight, ForceMode2D.Impulse);
                 }
                 break;
         }
     }
 
     void OnFirstTouch () 
     {
         switch (buttonType)
         {
 
             case type.LeftButton:
                 playerObject.transform.eulerAngles = new Vector2(0, 180);
                 playerObject.transform.Translate(Vector2.right * moveSpeed * Time.deltaTime);
                 isOn = 1;
                 break;
             case type.RightButton:
                 playerObject.transform.eulerAngles = new Vector2(0, 0);
                 playerObject.transform.Translate(Vector2.right * moveSpeed * Time.deltaTime);
                 isOn = 1;
                 break;
         }
     }
 
     void OnSecondTouch ()
     {
         switch (buttonType)
         {
 
             case type.LeftButton:
                 playerObject.transform.eulerAngles = new Vector2(0, 180);
                 playerObject.transform.Translate(Vector2.right * moveSpeed * Time.deltaTime);
                 isOn = 1;
                 break;
             case type.RightButton:
                 playerObject.transform.eulerAngles = new Vector2(0, 0);
                 playerObject.transform.Translate(Vector2.right * moveSpeed * Time.deltaTime);
                 isOn = 1;
                 break;
         }
     }
 
     void OnFirstTouchEnded ()
     {
         isOn = 0;
     }
 
     void OnSecondTouchEnded ()
     {
         isOn = 0;
     }
 }     
 

I also have a Touch manager script set up :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TouchManager : MonoBehaviour
 {
 
     public static bool guiTouch = false;
 
     public void TouchInput(GUITexture texture)
     {
         if (texture.HitTest(Input.GetTouch(0).position))
         {
             switch (Input.GetTouch(0).phase)
             {
                 case TouchPhase.Began:
                     SendMessage("OnFirstTouchBegan",SendMessageOptions.DontRequireReceiver); 
                     SendMessage("OnFirstTouch",SendMessageOptions.DontRequireReceiver);
                     guiTouch = true;
                     break;
                 case TouchPhase.Stationary:
                     SendMessage("OnFirstTouchStayed",SendMessageOptions.DontRequireReceiver);
                     SendMessage("OnFirstTouch",SendMessageOptions.DontRequireReceiver);
                     guiTouch = true;
                     break;
                 case TouchPhase.Moved:
                     SendMessage("OnFirstTouchMoved",SendMessageOptions.DontRequireReceiver);
                     SendMessage("OnFirstTouch",SendMessageOptions.DontRequireReceiver);
                     guiTouch = true;
                     break;
                 case TouchPhase.Ended:
                     SendMessage("OnFirstTouchEnded",SendMessageOptions.DontRequireReceiver);
                     guiTouch = false;
                     break;
             }
         }
         if (texture.HitTest(Input.GetTouch(1).position))
         {
             switch (Input.GetTouch(1).phase)
             {
                 case TouchPhase.Began:
                     SendMessage("OnSecondTouchBegan",SendMessageOptions.DontRequireReceiver);
                     SendMessage("OnSecondTouch",SendMessageOptions.DontRequireReceiver);
                     break;
                 case TouchPhase.Stationary:
                     SendMessage("OnSecondTouchStayed",SendMessageOptions.DontRequireReceiver);
                     SendMessage("OnSecondTouch",SendMessageOptions.DontRequireReceiver);
                     break;
                 case TouchPhase.Moved:
                     SendMessage("OnSecondTouchMoved",SendMessageOptions.DontRequireReceiver);
                     SendMessage("OnSecondTouch",SendMessageOptions.DontRequireReceiver);
                     break;
                 case TouchPhase.Ended:
                     SendMessage("OnSecondTouchEnded",SendMessageOptions.DontRequireReceiver);
                     break;
             }
         }
     }
 
 }
 

I checked everything and I still don't know what did I do wrong, If anyone can help out I'd be truly grateful. Thanks in advance.

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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

inconstant ground check on two identical platforms 1 Answer

Check if player is grounded and play a sound 1 time 1 Answer

I have a issue with Player Grounding. 1 Answer

Why does this type of ground check not working? 0 Answers

How do I check if my player is grounded with physics? 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