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
1
Question by raddry · Mar 06, 2013 at 01:29 PM · parentchildselectiononmouseup

Selecting a Game Object with a mouse click on it.

Hello, I'm making a simple RTS game and I need to implement a selecting feature. In fact mu script works very well but the problem is that I also instantiate prefabs. And because these prefabs are buildings on a moving planet, I set the parent of those instantiated prefabs to the Planet they're build on, so they remain on it when the Planet moves.

However, when I do this, and try to select the building, it is the Planet that is selected, and after some testing it would seem that the reason of that is because the building is the child of the planet.

So here's my question; is it possible to make this selecting script select the building and not the Planet is is built on, and if not, is there any way to make the position of the building relative to the Planet unchanged when the Planet moves without making the building a child of the Planet.

The selection is made with OnMouseUp().

--EDIT-- As suggested tried to do with Raycast, here's the code: using UnityEngine; using System.Collections;

 public class SelectingMicros : MonoBehaviour {
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             RaycastHit hitInfo = new RaycastHit();
             if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo) && hitInfo.transform.tag == "Construction")
             {
                 print ("It's working");
             }
         }
     }
 }

This doesn't work when the building is the child of the planet. I click on the building but the message doesn't appear, while if I put the tag of the Planet to "Construction" as well, it works but selects the Planet, not the building.

Comment
Add comment · Show 1
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 Sisso · Mar 06, 2013 at 02:30 PM 0
Share

Trying to be less verbose and more specific in your questions.

3 Replies

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

Answer by Sisso · Mar 06, 2013 at 02:31 PM

Given your question "is it possible", almost everything is possible. Probably your are doing a basic error.Generally input selection there is no influence about your objects positions and hierarchy. Create some prototypes and take a look again in documentation:

http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenToWorldPoint.html

http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html


EDIT: Added more info

raddry, try to put more debugs to know where exactly it is failing. Like:

 void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         Debug.Log("Mouse is down");
         
         RaycastHit hitInfo = new RaycastHit();
         bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
         if (hit) 
         {
             Debug.Log("Hit " + hitInfo.transform.gameObject.name);
             if (hitInfo.transform.gameObject.tag == "Construction")
             {
                 Debug.Log ("It's working!");
             } else {
                 Debug.Log ("nopz");
             }
         } else {
             Debug.Log("No hit");
         }
         Debug.Log("Mouse is down");
     } 
 }

Remember that raycast hit only objects with Colliders, if I have a child with collider, the child.transform will be referenced in RaycastHit.

Example: Given GameObject HQ that have 2 children, Model with a renderer and Bound with Collider, I will got the Bound.

 HQ (tag=construction)
 |- Model (Renderer)
 \- Bound (BoxCollider)

If I raycast this, I will got the Bound object. You can verify parent tag:

 var hitConstruction = hitInfo.transform.parent.gameObject.tag == "Construction"
Comment
Add comment · Show 5 · 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 raddry · Mar 08, 2013 at 02:06 PM 0
Share

Thanks for the suggestion. As I said I was using On$$anonymous$$ouseUp and I can assure you that the hierarchy seems to cause problem about that. Since I can select the building no problem when it's not the child of the Planet. Will try to make the selection system based on raycast then.

avatar image raddry · Mar 08, 2013 at 02:21 PM 0
Share

I updated OP with new code, still doesn't work.

avatar image raddry · Mar 08, 2013 at 05:49 PM 0
Share

It doesn't seem to be working but now I get why. When the raycast hits the building, it acts as if it hit the collider of the Planet because the planet is the Parent. I think it's not possible to make them as separate colliders here.

avatar image Sisso · Mar 08, 2013 at 06:00 PM 0
Share

It is ok to have a hierarchy where each object has its own colliders. Are you using any layer? Something different if you change raycastHint.transform per raycastHint.collider?

avatar image raddry · Mar 08, 2013 at 06:14 PM 0
Share

Working! First when I put collider ins$$anonymous$$d of transform, the debug indicated that the House$$anonymous$$esh was hit ( House$$anonymous$$esh is contained in the actually instantiated object). But it still said "nopz", and I realized that I had to tag the House$$anonymous$$esh for construction to make it work. Perfect now thanks for help.

avatar image
6

Answer by freeclup · May 21, 2013 at 11:46 AM

 // this code show nameobject with click   
    if (Input.GetMouseButtonDown(0))
 {
 //empty RaycastHit object which raycast puts the hit details into
 var hit : RaycastHit;
 //ray shooting out of the camera from where the mouse is
 var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  
 if (Physics.Raycast(ray, hit))
 {
 //print out the name if the raycast hits something
 Debug.Log(hit.collider.name);
 //freeclup= hit.collider.name;
 }
 }
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
5

Answer by elblogdelbeto · Jun 23, 2018 at 01:07 AM

better and easy way, just add box collider and a script with this event to your game object:

OnMouseUpAsButton() { }

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

13 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

Related Questions

Make a simple tree 1 Answer

Child selection forces parent selection 1 Answer

Controlling GameObject selection across Parent/Child 1 Answer

Calling a function of another object's child? 1 Answer

Pick up toy and attach to hand 1 Answer


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