• 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
Question by nolanspillane · Oct 23, 2019 at 01:35 AM · mobiletouch controlstouchscreenhitboxhit detection

Can't solve hitbox issues in mobile game

I am developing a game where you tap on objects that fall from the top of the screen, and try and tap on them before they reach the bottom of the screen. The code works fine when I played the game in the editor (with the touch controls swapped to mouse controls), except when I run the game on a phone, the game only seems to register a successful $$anonymous$$t if you tap slightly in front of the object in the direction that it is traveling, and does not register a $$anonymous$$t if you tap towards the back end or center of the object. I have built and ran the game over 10 times now, each time trying to fix t$$anonymous$$s issue but not$$anonymous$$ng seems to help. My theory at the moment is that my code for the touch controls have too much going on and/ or have redundancies and by the time it checks whether or not an object is at the position of the touch, the object has moved to a different location. Any thoughts on why the $$anonymous$$t boxes are off, and is there a better way to do $$anonymous$$t detection with touch screen?

T$$anonymous$$s is the code I used for the touch controls:

  void FixedUpdate()
     {
         if (IsTouch())
         {
             CheckTouch(GetTouchPosition());
         }
     }
 
 // Returns true if the screen is touched
     public static bool IsTouch()
     {
         if (Input.touchCount > 0)
         {
             if (Input.GetTouch(0).phase == TouchPhase.Began)
             {
                 return true;
             }
         }
         return false;
     }
 
 // Gets the position the touch
     private Vector2 GetTouchPosition()
     {
         Vector2 touchPos = new Vector2(0f, 0f);
         Touch touch = Input.GetTouch(0);
 
         if (Input.GetTouch(0).phase == TouchPhase.Began)
         {
             touchPos = touch.position;
         }
 
         return touchPos;
     }
 
 // Checks the position of the touch and destroys the ball if the ball is touched
     private void CheckTouch(Vector2 touchPos)
     {
         RaycastHit2D $$anonymous$$t = 
         Physics2D.Raycast(Camera.main.ScreenToWorldPoint(
         (Input.GetTouch(0).position)), Vector2.zero);
 
         if ($$anonymous$$t.collider != null)
          {
                destroy($$anonymous$$t.collider.gameObject);
          }
 }






Comment

People who like this

0 Show 0
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

Answer by mansoor090 · Oct 23, 2019 at 08:28 AM

Hints : 1 - use Input.touches[0].position; instead of GetTouch 2- Use GetMouseButton(0) for mobile input if you need basic inputs. mouse input will automatically be converted to inputs.

Solutions

Solution #1:

     RaycastHit2D x$$anonymous$$t = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition),Vector2.zero);
 if (x$$anonymous$$t .collider != null)
 {
     Destroy(x$$anonymous$$t.collider.gameObject);
 }

Solution #2: void Update() { if (Input.GetMouseButtonDown(0)) { RaycastHit2D rayHit = Physics2D.GetRayIntersection(Camera.main.ScreenPointToRay(Input.mousePosition or use touch position)); Debug.Log(rayHit.transform.name); if (x$$anonymous$$t .collider != null) { destroy($$anonymous$$t.collider.gameObject); } } }

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

163 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

Related Questions

switch keyboard controls to simple tap controls,Android touch controls for simple mobile game testing 0 Answers

Trying to rotate my character to the position of my touch on mobile 1 Answer

How do I move an object with my finger?[C#] 1 Answer

Possible to get touch area data? 1 Answer

CrossPlatformInputManager.GetAxis() always returns 0 5 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