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 Paradox486 · Nov 23, 2017 at 10:54 PM · uiraycastspritescollision detection2d sprites

How can I change the Unity 5 UI hitbox/collider to fit my complex image?

Hello! I have been updating my game to unity 5 and in doing so have remade my previous GUI using the new Unity UI system. I move my character by casting a physics raycast on click to get a location on the ground and then my character moves to that location. After adding the HUD element shown below I block a lot of my screen from being clicked. I do want the wooden portion of the image to block ray casting but not the area I've colored red. i am using a "Raw Image" within the canvas. I have tried multiple solutions:

  1. Editing the Physics shape and the outline in the Sprite editor

  2. Tuning off "Raycast Target" option in the Raw Image component and adding a Polygon Collider 2D with a Rigid body 2D

  3. Casting my own graphics raycast and using a list of all objects hit to see if I've clicked it but this doesn't follow the collider

None of these solutions have solved my problem, I have other UI elements that don't have concave shapes and they work wonderfully but I am struggling with this. Any help would be greatly appreciated!

Image with red showing where i want to be able to click: alt text

Physics Shape: alt text

redarea.jpg (447.3 kB)
physi.png (82.7 kB)
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
3
Best Answer

Answer by troien · Nov 24, 2017 at 01:59 PM

In theory, using Image.alphaHitTestMinimumThreshold would be the easiest way. I believe it wasn't implemented in older versions of Unity. It is now as I just checked.

Using it would be something like this:

 using UnityEngine;
 using UnityEngine.UI;
 
 public class Example : MonoBehaviour
 {
     private void Awake()
     {
         Image image = GetComponent<Image>();
         image.alphaHitTestMinimumThreshold = 0.1f;
     }
 }

Note however that your sprite has to be setup correclty for this to work.

In order for greater than 0 to values to work, the sprite used by the Image must have readable pixels. This can be achieved by enabling Read/Write enabled in the advanced Texture Import Settings for the sprite and disabling atlassing for the sprite.

What I had to do to get this to work was:

  • On the sprite, check read/write enabled (this is hidden under a advanced dropdown halfway the settings)

  • In the sprite, set Mesh Type to 'Full Rect' instead of 'Tight'

    You might need to change some other settings aswell.

    There is also a way by implementing ICanvasRaycastFilter.IsRaycastLocationValid. I had an older answer from when Unity UI was just released explaining this. Can't find it anymore though :(

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

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

153 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

Related Questions

Correct use of transform.position of UI element into game screen 1 Answer

Coin collection script (not working) 1 Answer

How to click on irregular buttons in new ui 4 Answers

New UI - Ignore raycast where alpha is 0 2 Answers

Jarring "snap" on 2d Camera raycast collider 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