Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.49 KB | None | 0 0
  1.   [SerializeField] private List<GameObject> placeableobjects = new List<GameObject>();
  2.     [SerializeField] private LayerMask terrainlayer;
  3.     private GameObject selectedobject;
  4.  
  5.  
  6.     void Update()
  7.     {
  8.         moveselectedobject();
  9.     }
  10.  
  11.     public void instantiateGameObject(int i)
  12.     {
  13.         selectedobject = Instantiate(placeableobjects[i]);   // another script is passing an int to this method
  14.     }
  15.  
  16.     private void moveselectedobject()
  17.     {
  18.         if (selectedobject != null)
  19.         {
  20.             RaycastHit hit;
  21.             if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 300, terrainlayer))
  22.             {
  23.                 selectedobject.transform.position = new Vector3(hit.point.x, selectedobject.GetComponent<Collider>().bounds.size.y / 2, hit.point.z);
  24.             }
  25.  
  26.             if (Input.GetKey(KeyCode.Y))
  27.             {
  28.                 selectedobject.transform.eulerAngles += new Vector3(0, 1, 0);
  29.             }
  30.  
  31.             if (Input.GetMouseButtonDown(0))
  32.             {
  33.                 GameObject instance = Instantiate(placeableobjects[2], new Vector3(hit.point.x, 0.025f, hit.point.z), Quaternion.identity);
  34.                 instance.transform.rotation = selectedobject.transform.rotation;
  35.                 Destroy(selectedobject);
  36.                 selectedobject = null;
  37.             }
  38.             else if (Input.GetKeyDown(KeyCode.Escape))
  39.             {
  40.                 Destroy(selectedobject);
  41.             }
  42.  
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement