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
  4. Dismiss Notice

preview placement of gameobject

Discussion in 'Editor & General Support' started by Arno1975, Sep 11, 2018.

  1. Arno1975

    Arno1975

    Joined:
    Aug 14, 2013
    Posts:
    43
    I have this grid where i can place a prefab on. If i click the prefab again it disapears. I got this working.
    But i also want to see a/the prefab as a preview before it is clicked.

    i can show the preview, but when i move to a different part of the grid, i don't know how to destroy the previewed prefab. i know i need to store the prefab and the location..then compare the locations and if they don't match destroy the stored object.

    but i can't figure it out..

    here is my script so far:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CubePlacer : MonoBehaviour
    4. {
    5.     private Grid grid;
    6.  
    7.     [Tooltip("The prefab to place")]
    8.     public GameObject prefabPlacementObject;
    9.    
    10.     private void Awake()
    11.     {
    12.         //get grid from other script
    13.         grid = FindObjectOfType<Grid>();
    14.                
    15.     }
    16.  
    17.     private void Update()
    18.     {
    19.         //shoot a raycast from camera to mouse
    20.         RaycastHit hitInfo;
    21.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    22.        
    23.  
    24.         //see if the raycast hits something
    25.         if (Physics.Raycast(ray, out hitInfo))
    26.         {
    27.             //is the object it hits from the tag Grid
    28.             if (hitInfo.collider.gameObject.tag == "Grid")
    29.             {
    30.                 //do we have a mouse click
    31.                 if (Input.GetMouseButtonDown(0))
    32.  
    33.                 {
    34.                     //execute function placeCubeNear
    35.                     PlaceCubeNear(hitInfo.point);
    36.                 }
    37.                  else
    38.                  {
    39.                     //if mouse isn't clicked execute function previewcuenear
    40.                      PreviewCubeNear(hitInfo.point);
    41.                     Debug.Log("found");
    42.                  }
    43.             }
    44.             //is the object it hits from the tag Mass
    45.             if (hitInfo.collider.gameObject.tag == "Mass")
    46.             {
    47.                 //do we have a mouse click
    48.                 if (Input.GetMouseButtonDown(0))
    49.                 {
    50.                     //destroy the object that is clicked
    51.                     Destroy(hitInfo.collider.gameObject);
    52.                 }
    53.             }
    54.         }
    55.     }
    56.     //function PlacecubeNear..function to place an object
    57.     private void PlaceCubeNear(Vector3 clickPoint)
    58.     {
    59.         //Get the grid position from the script grid
    60.         var finalPosition = grid.GetNearestPointOnGrid(clickPoint);
    61.      
    62.         //place the prefab on the position of the grid
    63.         Instantiate(prefabPlacementObject, finalPosition, Quaternion.Euler(0, 0, 0));
    64.  
    65.             }
    66.     //function PreviewcubeNear..function to place an object
    67.     private void PreviewCubeNear(Vector3 clickPoint)
    68.     {
    69.         //Get the grid position from the script grid
    70.         var tempPosition = grid.GetNearestPointOnGrid(clickPoint);
    71.  
    72.         //place the prefab on the position of the grid
    73.         var placedObject = Instantiate(prefabPlacementObject, tempPosition, Quaternion.Euler(0, 0, 0));
    74.  
    75.        
    76.         Debug.Log("script run" + tempPosition);
    77.        
    78.     }
    79.  
    80.    
    81. }
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    Why don't you use one prefab preview object and just move it around to whatever tile you're hovering?
     
  3. Arno1975

    Arno1975

    Joined:
    Aug 14, 2013
    Posts:
    43
    That is what i want..but don't know how
     
  4. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    Where are you getting stuck?
     
  5. Arno1975

    Arno1975

    Joined:
    Aug 14, 2013
    Posts:
    43
    i am placing a prefab with PreviewCubeNear...but when i move to a new grid location the element i placed at the previous location needs to be destroyed.. Or i need to make it shift position...maybe that is a better idea..
    but then i need to make it into the update function i think
     
  6. Arno1975

    Arno1975

    Joined:
    Aug 14, 2013
    Posts:
    43
    I need to do something with new vector..but i have no clu...please help


    found this:
     
    Last edited: Sep 17, 2018