Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 badman_city · Jun 27, 2014 at 03:58 AM · raycastupdateraycasthit

Selecting objects using raycasting

I'm working on a 3D towers of hanoi, and trying to specify which ring object the user is currently trying to manipulate. I have the following code within a script on a character controller. The code is in the Update() function. The ringSmall, ringMedium, and ringLarge scripts are attached to each corresponding ring object.

This works, however, I can only select each ring once, and then never again. Any suggestions?

 var fwd = transform.TransformDirection(Vector3.forward); 
 
 var hit : RaycastHit; 
 
 if (Physics.Raycast(transform.position, fwd, hit))
 {
 
              if(hit.distance <= 5) 
         {
                             
                                 if(hit.collider.gameObject.tag == "small")
                                 {
                                     if(Input.GetMouseButtonDown(0))
                                     {
                                         ringSmall.isSelected = true;
                                         print("Small Ring Selected");
                                     }
                                 }
                                 if(hit.collider.gameObject.tag == "medium")
                                 {
                                     if(Input.GetMouseButtonDown(0))
                                     {
                                         ringMedium.isSelected = true;
                                         print("Medium Ring Selected");
                                     }
                                 }
                                 if(hit.collider.gameObject.tag == "large")
                                 {
                                     if(Input.GetMouseButtonDown(0))
                                     {
                                         ringLarge.isSelected = true;
                                         print("Large Ring Selected");
                                     }
             }

}

Comment
Add comment · Show 4
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 AyAMrau · Jun 27, 2014 at 11:49 AM 0
Share

Do you have any code that deselects the rings on some event? Cause from just code they stay selected after clicked once.

Also did you consider using mouse events on the rings and then passing whatever you need to the controller?

avatar image badman_city · Jun 27, 2014 at 02:20 PM 0
Share

Yes, on each ring, I have code to set the other selections to false if that ring is selected. Here is the code snippet for when ringSmall.isSelected is true

 if(isSelected == true)
     {
         ringLarge.isSelected = false;
         ring$$anonymous$$edium.isSelected = false;
     }

Also, what do you mean by mouse events? I'm using Input.Get$$anonymous$$ouseButtonDown(0) currently, but just to do the selecting if the ring is clicked on

avatar image AyAMrau · Jun 27, 2014 at 02:50 PM 0
Share

Just to cover everything: Did you try doing prints to check if the raycast is hitting the rings? Do you do anything to the rings when they are selected or is it just limited to the isSelected bool for now?

As for mouse events, depending on how your game works you could use the On$$anonymous$$ouseDown() and other $$anonymous$$ouse functions on the rings, but that only works if the player is supposed to point at them with the mouse. But if you need to point the camera or the player at them then ignore it.

avatar image badman_city · Jun 27, 2014 at 03:07 PM 0
Share

I just tried adding print statements under the if(hit.collider.gameObject.tag == "small/medium/large") to check if the raycast is hitting the rings, again, it only does each ring once and then is just stuck on whatever the last one is

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Make Raycast ignore anything that "Isn't" my player(Solved) 1 Answer

Something fishy with RaycastHit 1 Answer

Rays do not detect terrain and prefabs do not spawn where wanted 1 Answer

How can I can I cast a ray from a gameobject? 1 Answer

Raycast Not Working 3 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