• 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 Carbongrip · Nov 30, 2013 at 02:16 AM · cameraraycastmouseselection

Raycast Object Selection

What's wrong here? I have t$$anonymous$$s in the update function and I debuted the t$$anonymous$$ng… it see's the click but never does the if it even when it should be $$anonymous$$tting…

 if (Input.GetMouseButtonDown(0))// when button clicked...
            { 
              gui = "no";
              RaycastHit $$anonymous$$t; // cast a ray from mouse pointer:
              Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
              // if enemy $$anonymous$$t...
              if (Physics.Raycast(ray, out $$anonymous$$t))
              {
                 Debug.Log("selecting");
                DeselectPlanet(); // deselect previous target (if any)...
                selectedPlanet = $$anonymous$$t.transform; // set the new one...
                planetselect(); // and select it
              }
         }

I have t$$anonymous$$s working in another script only t$$anonymous$$ng I can t$$anonymous$$nk is it has to do with the fact that for t$$anonymous$$s one the camera is in a empty object so I can move the camera. Also maybe since the camera is angled?

Comment
Add comment · Show 2
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 robertbu · Nov 30, 2013 at 04:27 AM 0
Share
avatar image Moor · Nov 30, 2013 at 04:59 AM 0
Share

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Tomer-Barkan · Nov 30, 2013 at 05:46 AM

First, do you have just the one camera? If you have two cameras, you might want to use a reference to the camera inside your script instead of Camera.main (in general you might want to use it for performance benefits).

Second, I can recommend a perhaps simpler way to handle clicks on items: OnMouseUpAsButton. T$$anonymous$$s method is invoked in any MonoBehaviour attached to an object that is clicked - meaning button is pressed and released on top of the object (as long as the object has a collider). You also have OnMouseDown to handle button pressed (without releasing), OnMouseDrag to handle holding the button down, OnMouseUp, etc.

Now, I would create a simple script that detects clicks on an object, and calls a method on your HUD script:

public class MapHud : MonoBehaviour { // all your code here

 // handle clicks on objects
 public void ObjectClicked(ClickableObject obj) {
     Debug.Log("selecting");
     DeselectPlanet(); // deselect previous target (if any)...
     selectedPlanet = obj.transform; // set the new one...
     planetselect(); // and select it
 }

}

public class ClickableObject : MonoBehaviour { private MapHud hud;

 public void Start() {
     hud = Object.FindObjectOfType(typeof(MapHud)) as MapHud;
 }

 public void OnMouseUpAsButton() {
     hud.ObjectClicked(t$$anonymous$$s);
 }

}

Then simply attach the ClickableObject script to any object you want selectable. Remember to make sure they all have colliders. In t$$anonymous$$s case it doesn't matter how many cameras you have.

Comment
Add comment · Show 4 · 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 Carbongrip · Nov 30, 2013 at 06:17 AM 0
Share
avatar image Tomer-Barkan · Nov 30, 2013 at 07:11 AM 0
Share
avatar image Carbongrip · Nov 30, 2013 at 07:26 AM 0
Share
avatar image Tomer-Barkan · Nov 30, 2013 at 07:58 AM 0
Share
avatar image
1

Answer by belvita · Nov 30, 2013 at 05:05 AM

Can u try like t$$anonymous$$s: inside update function

if (Input.GetMouseButtonDown(0))

     {


// when button clicked...

         gui = "no";
     }
         RaycastHit $$anonymous$$t; // cast a ray from mouse pointer:

         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

         // if enemy $$anonymous$$t...

         if (Physics.Raycast(ray, out $$anonymous$$t))

         {

            Debug.Log("selecting");

            DeselectPlanet(); // deselect previous target (if any)...
            selectedPlanet = $$anonymous$$t.transform; // set the new one...

            planetselect(); // and select it
         }
     
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
avatar image
0

Answer by golemsmk · Nov 30, 2013 at 04:56 AM

I t$$anonymous$$nk your script is OK, it must be a problem with colliders. Adding a suitable collider to the object will solve your problem. Remember, you can only use the 3D collider, not 2D collider, these 2D colliders won't work. Unity will fix t$$anonymous$$s bug soon. I see someone has reported t$$anonymous$$s to Unity. If t$$anonymous$$s is the right answer, please stick it for other people don't have to look around for the answer.

Comment
Add comment · Show 3 · 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 Tomer-Barkan · Nov 30, 2013 at 05:34 AM 0
Share
avatar image Carbongrip · Nov 30, 2013 at 05:42 AM 0
Share
avatar image golemsmk · Nov 30, 2013 at 04:43 PM 0
Share

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

21 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

Related Questions

How convert the coorardinates of the mouse from one camera to another? 2 Answers

Camera/mouse movement issues "almost but no dice" 1 Answer

Raycast an Object to the mouse position 1 Answer

Raycast from Character to Mouse Position? 2 Answers

Arial camera get selected point 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