I´ve some GUI Buttons. With a mouse-left-click on one button you can choose a game object. With another left-click on the terrain you can place it.
Now i want to see after the click on the button, the game object under my mouse (maybe a bit transparent), so you can place it a lot easier. Here is the the part of the code:
function Update () {
if(Input.GetMouseButtonDown(0))
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,hit,300))
{
Instantiate(buildingType,hit.point,Quaternion.identity);
}
The building should only be under the mouse curser, after clicking the GUI element. It should disappear with the mouse-left click (for placing the object), or the right-mouse to cancel the action.
The example which you can find in the docs hasn´t much to do with that issue, it´s more like a mouse-over.
function OnMouseEnter () {
renderer.material.color = Color.red;
}
#pragma strict
import System;
function Update () {
if(Input.GetMouseButtonDown(0))
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,hit,300))
{
hit.transform.renderer.material.color = Color.red;
}
}
}
That code would make something you click change color. Now its up to you to decide how you should make it change back after you unclick and what not.
I would recommend maybe a list of objects you clicked, then if you clicked the right button, go throw them and change material back?