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 Nolram · Feb 04, 2013 at 05:03 PM · mouseclickselection

How to select object with mouse?

I want to make a board game, but do not know how to select just one piece with the mouse. I've tried this script:

 public var SelectCB1: boolean;
 var LuzSelecao      : GameObject;
 var CavaloCB1       : Transform;
 
 function Start () {
 SelectCB1 = false;
 
 }
 if(Input.GetMouseButtonDown (0)){
         
         if (SelectCB1 == true){
            SelectCB1 = false;
            Luz.DestroyLuz = true;
            }
            else
             if (SelectCB1 == false){
             Luz.DestroyLuz = false;
             Instantiate(LuzSelecao,Luz.posLuz,transform.rotation);
              SelectCB1 = true;
              }
     
    }
 } 

But I click anywhere on the screen, will be selecting the piece. Do you know any script that selects only the piece that is being clicked?

CavaloB1 means HorseWhite1º

I'm Brazilian and I'm using Google translator.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Deeweext · Feb 04, 2013 at 06:40 PM

This is what I use for:

  1. Display name on MouseOver.

  2. Display selection if unit is clicked.

When you hover the mouse over an object, it displays the name of the unit, defined by the variable on it, when you move the mouse out, it fades the title out. You can also add display ALL names by holding down a key, in my case [ALT] but I commented out that part, since you have to add the [ALT] key in input manager for that part to work. When you click an object it will highlight/rotate it. If you click on another object, it will deselect the previous, oh and the rotating is on xy-plane, depending your setup you might have to change that. This script has to be attached to any object that has to have a name or hovering, or you can reverse engeneer the code for your likings. You can see the final result at this youtube video: http://goo.gl/jdtlG and the material I used on my selection plane (Honestly, I used a cylinder though, slimmed and fit in size, and the material I used for it was this: http://i47.tinypic.com/t6f9r6.jpg

For this to work:

  1. You must have a collider on the object.

  2. You must add a 3D text object as child called ObjectTitle.

  3. You must add a plane (or cylinder) as child called ObjectSelection

The code:

             var Highlight_00 : Material;
 
             var Title_Name                 : String = "?";
     private var Title_ColorInitial         : Color = Color(1.0, 1.0, 1.0, 1.0);
     private var Title_ColorFaded         : Color = Color(0.0, 0.0, 0.0, 0.0);
     private var Title_EnabledByMouse     : boolean = false;
     private var Title_EnabledByKey         : boolean = false;
 
     private var myTransform_Main         : Transform;
     private var myTransform_Title         : Transform;
     private var myTransform_Selection    : Transform;
 
     
     function Awake ()
     {
         myTransform_Main                                 = transform;
         myTransform_Title                                 = myTransform_Main.transform.Find("ObjectTitle");
         myTransform_Selection                             = myTransform_Main.transform.Find("ObjectSelection");
         
         myTransform_Title.renderer.material.color         = Title_ColorFaded;
         
         myTransform_Selection.renderer.enabled             = false;
         myTransform_Selection.renderer.material         = Highlight_00;
     }
     
     function Update ()
     {
         if(Input.GetMouseButtonDown(0))
         {
             myTransform_Selection.renderer.enabled = false;
         }
         
         /* TO ENABLE THIS, ASSIGN ALT KEY BY NAME "ShowNamesTest"
         if (Input.GetButtonDown ("ShowNamesTest"))
         {
             Title_EnabledByKey = true;
             Custom_TitleEnable();
         }
         
         if (Input.GetButtonUp ("ShowNamesTest"))
         {
             Title_EnabledByKey = false;
             Custom_TitleDisable();
         }
         */
         
         if(myTransform_Selection.renderer.enabled == true)
         {
             myTransform_Selection.transform.Rotate(Vector3(0,1,0), Time.deltaTime*15);
         }
     }
     
     function OnMouseEnter ()
     {
         Title_EnabledByMouse = true;
 
         Custom_TitleEnable();
     }
     
     function OnMouseExit ()
     {
         Title_EnabledByMouse = false;
         
         if(Title_EnabledByKey == true)
         {    
             return;
         }
         
         Custom_TitleDisable();
     }
     
     function Custom_TitleDisable ()
     {
         for (var a = 0.0; a < 0.5; a += Time.deltaTime)
         {
             if(Title_EnabledByMouse == true || Title_EnabledByKey == true)
             {
                 return;
             }
             else
             {
                 myTransform_Title.renderer.material.color = Color.Lerp (Title_ColorInitial, Title_ColorFaded, a/0.5);
                 yield;
             }
         }
     }
     
     function Custom_TitleEnable ()
     {
         myTransform_Title.renderer.material.color = Title_ColorInitial;
     }
     
     function OnMouseDown()
     {
         yield;
         myTransform_Selection.renderer.enabled = true;
     
     }



Final words:

If you can't get this to work, or don't understand reverse engeneering the code, just ask and I will be more than happy to help you through it. This is the approach I used, not wanting to deal with raycast at all, and sorry if my coding formatting is weird, we all get stupid layout habbits over time.

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 Deeweext · Feb 04, 2013 at 08:02 PM 0
Share

I know he only wanted a script to select a unit, but this fit in, and easily ties into a selection.

avatar image Lantix · May 31, 2013 at 12:56 AM 0
Share

This is excellent, thank you for the incredibly detailed answer, I learned a lot just from reading through the code.

avatar image Deeweext · May 31, 2013 at 01:40 PM 0
Share

It looks like a lot of code, but it gives a nice fade out effect when hovering out, but only if your not using the alt key part of the code, so it's one of those codes made, then realizing some points that needs fixing, and then fixing those. If you still need help just let me know, i dont use too many comments in that code.

avatar image Lantix · May 31, 2013 at 01:54 PM 0
Share

It's pretty clear to be honest, your code is done in a nice self documenting way with function names and what not. And I already got some similar aspects of it working, that alt trick is pretty cool, I forgot about the ability to add buttons as commands like that haha.

avatar image Metalbreath · Jul 13, 2013 at 06:25 PM 1
Share

how can i write the yield in C#? i ve tried everything, and the only problem i get is that it doesnt find the renderer.enabled = true (in On$$anonymous$$ouseDown ())

Show more comments
avatar image
0

Answer by robertbu · Feb 04, 2013 at 05:31 PM

For moving objects, there are two different approaches. The first is to make each piece responsible for moving itself. This approach uses OnMouseDown() OnMouseDrag() and OnMouseUp() in a script that is attached to each game piece.

The second approach is to use Raycasting (Physics.Raycast) to find objects and them a script outside of the game pieces moves them around. If you search this list for the "OnMouseDown" and "Raycasting" you will find numerous examples of each. As a starting point see:

Highlighting and Moving an Object

The script in @KraljCro question works for drag and drop using the OnMOuse* routines, and there is an example of Raycasting further down the page.

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 drod7425 · Feb 04, 2013 at 05:34 PM

It looks like you should use Raycast.

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

Just attach that script to the object you want to be able to select, edit the code to include a check for mouse down, and set the bool to true if everything checks out.

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

Activate gameobject upon mouseclick 1 Answer

Object Selection with large number of objects 1 Answer

Arial camera get selected point 2 Answers

Why Is the game object not selected? I'd like to understand why. 2 Answers

selected object in array lost in translation 0 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