Jump to content
This site uses cookies! Learn More

This site uses cookies!

armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

  • 0
Sign in to follow this  
zigglr

Why won't this simple raycast work?

Question

I have a UI button, with a box collider 2D attached and is tagged as 'button'. 

 

In a script, which is attached to a different gameObject, I have this:

 
 
Please login or register to see this code.
 
 
 

Please can someone tell me why the message doesn't appear in the console when I click on the button? Would really appreciate it. Thanks :)

Edited by zigglr

Share this post


Link to post
Share on other sites

15 answers to this question

Recommended Posts

  • 0

Bc box collider 2d doesn't interact with normal raycasts. You need to use a raycast2d to interact with 2d colliders

Share this post


Link to post
Share on other sites

  • 0
Please login or register to see this quote.

Do you mean I need to add a Raycast2d component to the button, or do I need to change the code?

Share this post


Link to post
Share on other sites
  • 0

Physics2d Raycast docs are found here Please login or register to see this link. I'm not sure tho if you can cast on the z axis tho. I'm not 100% sure, but you might just be stuck with checking the rects. A big reason why object ui will never be adequate in my book. Ui just doesn't belong in world space.... Ever.

You can check the Rect like this pseudo:

if(Input.GetMouseButtonDown(0))

{

if(imageRect.Contains(mousePositionWorldSpace)) DoSomething();

}

Edit:

Actually, if your using a ui button, why aren't you just using a ui event handler?

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this code.

This example shows you how it works. You need to use a RaycastHit2D with a Collider2D.
In this previous code, the raycast give us the name of the GO which uses a Collider2D ++

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

Oh great, so is there any way of using this on a tagged object? Thanks

Share this post


Link to post
Share on other sites
  • 0

What it means? Do you want use this RaycastHit2D only on a specific tagged object?
So add a simple condition. See this example - the same with a little modification ++

Please login or register to see this code.

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

I try but again, nothing happens when I click it :( It seems no one knows answer for this

Share this post


Link to post
Share on other sites
  • 0

I tested this script at home and... it works :)

Please login or register to see this link.

Do you use a Collider2D? Do you use an orthographic camera?

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

Yes I used both of them. Do I have to attach the script to the object that i'm clicking on?

 

It just says this wherever I click on the screen:

 

(342.3, 52.6)
UnityEngine.Debug:Log(Object)
FlickDetection:Update() (at Assets/ClickDetection.cs:46)
Edited by geckoo

Share this post


Link to post
Share on other sites
  • 0

I put this script on my camera.

Do you use the good tag? What is this last line on your previous explanation? An error I guess...

However, you can send me a link in PM so as to download your project. I will test it today ++

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

It seems I found the problem. When the gameObject is a child of the camera, the click doesn't get recognised. When the gameObject is on it's own and not a child of the camera, it does get recognised. Is there any way to still get it recognised when it is a child of the camera?

 

Thank you for the help so far by the way I really appreciate it!

Share this post


Link to post
Share on other sites
  • 0

OK. I understand more or less clearly your will. However, I found this explanation which could be useful for you :

Please login or register to see this link.

 

So we have this code (I guess that you need to modify it because I have no clue about what you are doing).
In this example, these clickable objects are embedded on the main camera. I hope that it helps you ++

Please login or register to see this code.

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

You are great, thanks so much. You were much more helpful than Unity Answers :) Thanks again

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

By the way does your website have a donate option?

Edited by zigglr

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...