- Home /
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.
Answer by Deeweext · Feb 04, 2013 at 06:40 PM
This is what I use for:
Display name on MouseOver.
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:
You must have a collider on the object.
You must add a 3D text object as child called ObjectTitle.
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.
I know he only wanted a script to select a unit, but this fit in, and easily ties into a selection.
This is excellent, thank you for the incredibly detailed answer, I learned a lot just from reading through the code.
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.
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.
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 ())
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.
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.