Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.08 KB | None | 0 0
  1. public class GroundMakeCube : MonoBehaviour{
  2.    
  3.     private void Update() {
  4.         if (Input.GetKeyDown(KeyCode.Space)) {
  5.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  6.             RaycastHit hit;
  7.             if (Physics.Raycast(ray, out hit)) {
  8.                 Vector3 pos = hit.point;
  9.                 pos.y = 0f;
  10.                 GameObject pref = Resources.Load("Prefabs/Cube") as GameObject;
  11.                 GameObject cubeNew=Instantiate(pref);
  12.                 cubeNew.GetComponent<Renderer>().material.color =
  13.                     UnityEngine.Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
  14.                 cubeNew.transform.position = pos;
  15.             }
  16.         }
  17.     }
  18. }
  19.  
  20.  
  21.  
  22.  
  23.  
  24. public class GroundMoveCube : MonoBehaviour {
  25.     private Vector3 clickPos;
  26.     public GameObject obj;
  27.  
  28.     private void Update() {
  29.         if (Input.GetMouseButton(0)) {
  30.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  31.             RaycastHit hit;
  32.             int layerMask = LayerMask.GetMask("Ground");
  33.             if (obj!=null) {
  34.                 if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask)) {
  35.                     clickPos = hit.point;
  36.                 }
  37.  
  38.                 clickPos.y = 0f;
  39.                 obj.transform.position = clickPos;
  40.            
  41.                 Debug.Log("MOVE"+clickPos);      
  42.             }
  43.         }
  44.  
  45.         if (Input.GetMouseButtonUp(0)) {
  46.             obj = null;
  47.         }
  48.     }
  49. }
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56. public class SelectCube : MonoBehaviour {
  57.     private void Update() {
  58.         if (Input.GetMouseButtonDown(0)) {
  59.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  60.             RaycastHit hit;
  61.             int layerMask = LayerMask.GetMask("Object");
  62.             if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask)) {
  63.                 Debug.Log("Select"+hit.transform.name+" "+hit.transform.GetInstanceID());
  64.                 GameObject.Find("Main Camera").GetComponent<GroundMoveCube>().obj =
  65.                     hit.transform.gameObject;
  66.             }
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement