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 megabrobro · Aug 21, 2017 at 02:46 PM · androidunity 5uitouchtouch controls

Why is this raycast still hitting ground below a UI button, even after I pass a check to see if finger is above button (as per Unity tutorial)

Hi there all. I've been reading lots and trying many variations of this method to fix my issue, but cannot quite seem to understand it all. The "...IsPointerOverGameObject(t.fingerid)" part is supposed to stop the touch being counted in my update method (so that it doesnt call the related functions in my 'Goodguys' array.)

The actual result i want is that a touch just to the screen will set the destination of the players NavMesh, then there is a UI button with Events Trigger on it, if the player holds that button in, then also presses somewhere on the screen, that will make the Goodguys shoot their guns.

It all works fine if you dont press the Shoot button. But if you do, the debug.log still states that "object touched is Ground" and the players (/or Goodguys) still just turn around, they will shoot, so know the method is having some effect, but their target underneath the UI button, and AFAIK my code should be stopping that from happening already.

Anyone kind enough so spot what I have done wrong?? Many thanks :)

public class TouchHandler : MonoBehaviour { RaycastHit hit; GameObject objectBeingTouched;

 Camera cam;
 GameObject[] goodguys;


 private void Awake()
 {
     cam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
 }

 void Update()
 {
     if (Input.touchCount > 0) 
     {
         goodguys = GameObject.FindGameObjectsWithTag("Goodguy");
         foreach (Touch t in Input.touches)
         {
             Ray ray = cam.ScreenPointToRay(t.position);

             if (Physics.Raycast(ray, out hit))
             {
                 objectBeingTouched = hit.transform.gameObject;

                 if (t.phase == TouchPhase.Began)
                 {
                     if (!UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject(t.fingerId)) 
                     {
                         foreach (GameObject goodguy in goodguys)
                         {
                             goodguy.SendMessage("OnTouchStart", hit, SendMessageOptions.DontRequireReceiver);
                         }
                     }
                 }
                 if (t.phase == TouchPhase.Moved)
                 {
                     if (!UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject(t.fingerId)) 
                     {
                         foreach (GameObject goodguy in goodguys)
                         {
                             goodguy.SendMessage("OnTouchMoved", hit, SendMessageOptions.DontRequireReceiver);
                         }
                     }
                 }
                 if (t.phase == TouchPhase.Stationary)
                 {
                     if (!UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject(t.fingerId)) 
                     {
                         foreach (GameObject goodguy in goodguys)
                         {
                             goodguy.SendMessage("OnTouchStill", hit, SendMessageOptions.DontRequireReceiver);
                         }
                     }
                 }
                 if (t.phase == TouchPhase.Ended)
                 {
                     if (!UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject(t.fingerId))
                     {
                         foreach (GameObject goodguy in goodguys)
                         {

                             goodguy.SendMessage("OnTouchEnded", hit, SendMessageOptions.DontRequireReceiver);
                         }
                     }
                 }
                 if (t.phase == TouchPhase.Canceled)
                 {
                     if (!UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject(t.fingerId)) 
                     {
                         foreach (GameObject goodguy in goodguys)
                         {
                             goodguy.SendMessage("OnTouchCancelled", hit, SendMessageOptions.DontRequireReceiver);
                         }
                     }
                 }
             }
         }
     }
 }

 // UI button toggles:
 public void UiButtonShootModeToggle()
 {
     if (goodguys != null)
     {
         foreach (GameObject goodguy in goodguys)
         {
             Goodguy gg = goodguy.GetComponent<Goodguy>();
             if (gg.currentControlMode != Goodguy.ControlMode.SHOOT)
             {
                 gg.currentControlMode = Goodguy.ControlMode.SHOOT;
             }
             else
             {
                 gg.currentControlMode = Goodguy.ControlMode.MOVE;
             }
         }
     }
 }

}

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 waizui · Dec 12, 2018 at 12:22 PM

same issue ,how did you fixed it?

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 megabrobro · Dec 14, 2018 at 09:09 PM 0
Share

hi sorry i don't remember. But I know I haven't run into any more problems like it with other projects since then. I think I do the raycast slightly differently now with less lines of code. Something like if (Physics3d.Raycast(ray, out hit, rayLength){ return hit.collider.gameObject} then the hit tells me what object the ray hit first (i think!).

Hope that helps lol :S

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

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

EventSystem.current.IsPointerOverGameObject(t.fingerId) - Not working on Android builds - Unity 2 Answers

Android touch input Null Reference exception error 0 Answers

The bullet doesn't move correctly 1 Answer

Unity Android Change Color of Navigation bar 0 Answers

UI Touch Events not working? 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