VioletaBabel
34. 3D plane에서 cube 이동 본문
Plane 위에 cube를 두고, cube에 이 스크립트와 rigidbody를 넣는다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; public class cubeMove : MonoBehaviour { Vector3 dest; bool click = false; // Use this for initialization void Start() { dest = transform.position; } // Update is called once per frame void Update() { if (Input.GetKey(KeyCode.A)) { Vector3 a = transform.position; a.x -= Time.deltaTime * 2.5f; transform.position = a; dest = a; } if (Input.GetKey(KeyCode.D)) { Vector3 a = transform.position; a.x += Time.deltaTime * 2.5f; transform.position = a; dest = a; } if (Input.GetKey(KeyCode.W)) { Vector3 a = transform.position; a.z += Time.deltaTime * 2.5f; transform.position = a; dest = a; } if (Input.GetKey(KeyCode.S)) { Vector3 a = transform.position; a.z -= Time.deltaTime * 2.5f; transform.position = a; dest = a; } if (Input.GetMouseButtonDown(0)) {//이건 클릭하면 그 쪽으로 살짝 미는 코드 Vector3 v = Input.mousePosition; Ray r = Camera.main.ScreenPointToRay(v); RaycastHit hit = new RaycastHit(); if (Physics.Raycast(r, out hit)) { dest = hit.point; dest.y = 0.5f; click = true; //Vector3 dir = dest - transform.position; //이것도 미는거 //GetComponent<Rigidbody>().velocity = dir; //이게 미는 역할. rigidbody 필요. } } if (click) if (Vector3.Distance(dest, transform.position) > 0.5f) {//이건 그냥 움직이는 코드. 미는걸 할 때는 주석처리할 것. Vector3 me = transform.position; Vector3 arrow = dest - me; arrow.Normalize(); me += arrow * 0.5f; transform.position = me; } else if (!Vector3.Distance(dest, transform.position).Equals(0)) { transform.position = dest; click = false; } } } | cs |
'BCA > 4. Unity' 카테고리의 다른 글
38. 유니티에 채팅용 서버 기본 연결 (0) | 2018.06.05 |
---|---|
35. Terrain, nav mash (0) | 2018.05.10 |
32. 타워 디펜스게임 (0) | 2018.05.03 |
31. 연출용 툴 이용해 유니티에서 연출 (0) | 2018.04.27 |
29. 테트리스 (0) | 2018.04.04 |
Comments