Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by imM4TT · Jul 07, 2018 at 09:51 PM · canvasraycasthit

detect click on canvas

Hello
I'm trying to add a HUD in my project and I want that when I click on the canvas ( which is 20% of the screen ) my player dont move.
It's moving by a raycast statement, so I tryied this but it doesnt work :

         if (Physics.Raycast(ray, out hit) ) {
         
             if(hit.transform.gameObject.layer == 5)
             {
                 print("Ui element was click");
             }
             SetDestination();
          }
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
8
Best Answer

Answer by TreyH · Jul 08, 2018 at 01:32 AM

There's a fairly detailed example in the docs to do just this. I'll clean their example up a bit for you, but the modifications for your exact situation are straightforward.

As mentioned on that page, this will be attached to your Canvas object so that it can access its GraphicRaycaster component.

 using System.Collections.Generic;
 
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 
 public class CheckClicks : MonoBehaviour
 {
     // Normal raycasts do not work on UI elements, they require a special kind
     GraphicRaycaster raycaster;
 
     void Awake()
     {
         // Get both of the components we need to do this
         this.raycaster = GetComponent<GraphicRaycaster>();
     }
 
     void Update()
     {
         //Check if the left Mouse button is clicked
         if (Input.GetKeyDown(KeyCode.Mouse0))
         {
             //Set up the new Pointer Event
             PointerEventData pointerData = new PointerEventData(EventSystem.current);
             List<RaycastResult> results = new List<RaycastResult>();
 
             //Raycast using the Graphics Raycaster and mouse click position
             pointerData.position = Input.mousePosition;
             this.raycaster.Raycast(pointerData, results);
 
             //For every result returned, output the name of the GameObject on the Canvas hit by the Ray
             foreach (RaycastResult result in results)
             {
                 Debug.Log("Hit " + result.gameObject.name);
             }
         }
     }
 }
 


Comment
Add comment · Show 6 · 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 imM4TT · Jul 08, 2018 at 01:54 AM 0
Share

Thanks !
I ill look at this
But as far as I understand, it's on the canvas that i must put this script, so, I have no idea to go forward with my issue which is on my player script

$$anonymous$$y player have a script attached to and on this script I said when I click somewhere, move to this point

I would like to make this function unavailable if the click is on the canvas so my player dont move

Have you any idea ?
Edit : Ok I see why it's not working on my 1st post , do you suggest me a static var to communicate between script to know if the click was on the canvas ?

avatar image TreyH imM4TT · Jul 08, 2018 at 02:31 AM 0
Share

This doesn't necessarily have to be on your canvas. This just has to reference the components on your canvas. There are several ways to go about this, but those are all covered extensively across this forum.

avatar image jorgecastillodp · Jun 26, 2019 at 04:56 PM 1
Share

Amazing! thank you

avatar image PixelFireXY · Feb 21, 2020 at 03:38 PM 1
Share

O$$anonymous$$G this finally works!!! Thank you so much!!! <3

avatar image samra2494 · Sep 09, 2020 at 11:16 AM 1
Share

Thanks a lot, I finally find your solution possible for me :)

avatar image augustomoreirapedro06 · 8 hours ago 0
Share

Hi! Is there any way that this works with buttons?

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

99 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

Related Questions

Get name of UI element by RayCasting 1 Answer

In WebGL, is it possible to make UI Coordinate scale with CSS style? 1 Answer

Raycashit Variable make empty/null/clear? 1 Answer

RayCast to all directions 4 Answers

Is this way of making a raycast correct 2 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