Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Game Object under Mouse after selection

Discussion in 'Scripting' started by Lopster, Dec 15, 2012.

  1. Lopster

    Lopster

    Joined:
    Dec 9, 2012
    Posts:
    8
    Hello,

    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:

    Code (csharp):
    1. function Update () {
    2.  
    3.  
    4.     if(Input.GetMouseButtonDown(0))
    5.     {
    6.  
    7.     var hit : RaycastHit;
    8.     var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    9.  
    10.     if(Physics.Raycast(ray,hit,300))
    11.     {
    12.  
    13.     Instantiate(buildingType,hit.point,Quaternion.identity);
    14.  
    15. }
     
  2. thekill473

    thekill473

    Joined:
    Aug 26, 2012
    Posts:
    9
    Try adding an OnMouseEnter() function into your class.
     
  3. Lopster

    Lopster

    Joined:
    Dec 9, 2012
    Posts:
    8
    Could you describe it a bit more.

    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.

    Code (csharp):
    1. function OnMouseEnter () {
    2.     renderer.material.color = Color.red;
    3. }
     
  4. Lopster

    Lopster

    Joined:
    Dec 9, 2012
    Posts:
    8
    Still couldn´t figure it out.
     
  5. Lopster

    Lopster

    Joined:
    Dec 9, 2012
    Posts:
    8
    /push
     
  6. WendygoGames

    WendygoGames

    Joined:
    Dec 11, 2012
    Posts:
    53
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. import System;
    5.  
    6. function Update () {
    7.     if(Input.GetMouseButtonDown(0))
    8.     {
    9.         var hit : RaycastHit;
    10.         var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    11.         if(Physics.Raycast(ray,hit,300))
    12.         {
    13.             hit.transform.renderer.material.color = Color.red;
    14.         }
    15.     }
    16. }
    17.  
    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?
     
  7. Lopster

    Lopster

    Joined:
    Dec 9, 2012
    Posts:
    8
    Thanks, but i don´t need a color change.

     
  8. Lopster

    Lopster

    Joined:
    Dec 9, 2012
    Posts:
    8
    No tips?