• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

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

The bullet doesn't move correctly 1 Answer

Failed to re-package resources even I make blank project 3 Answers

Having problems with touch input 1 Answer

OnDrag methods not being called on Android in Unity 2018.3.5f1 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges