Raycast, Collider, Rigidbody, Joystick 이용 캐릭터 이동하기
Unity/수업내용 2019. 4. 25. 18:11Joystick 이용하여 이동하기
Keyboard(W,A,S,D) 이용하여 이동하기
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class TestRaycastAndCollider : MonoBehaviour { public Text txtMousePoint; public Text txtHitPoint; public GameObject heroGo; private Animator heroAnim; private Coroutine heroRoutine; private Coroutine joystickRoutine; public TestMyJoystick joystick; private void Awake() { Debug.Log("TestRaycastAndCollider Awake!"); } // Start is called before the first frame update void Start() { this.heroAnim = heroGo.GetComponent<Animator>(); Debug.Log("TestRaycastAndCollider Start!"); this.joystick.OnPointerDownHandler = () => { Debug.Log("다운핸들러"); if (this.joystickRoutine != null) { StopCoroutine(this.joystickRoutine); } StartCoroutine(this.UpdateJoystickImpl()); }; this.joystick.OnPointerUpHandler = () => { Debug.Log("업핸들러"); StopAllCoroutines(); this.heroAnim.Play("idle"); }; } private void UpdateJoystick() { Vector3 direction = Vector3.forward * this.joystick.Vertical + Vector3.right * joystick.Horizontal; direction = new Vector3(direction.x, 0, direction.z).normalized; if (direction != Vector3.zero) { this.heroAnim.Play("run"); this.heroGo.transform.position += direction * 1.0f * Time.deltaTime; var rad = Mathf.Atan2(direction.x, direction.z); var dig = Mathf.Rad2Deg * rad; this.heroGo.transform.rotation = Quaternion.LookRotation(direction); //this.heroGo.transform.rotation = Quaternion.Euler(new Vector3(0, dig, 0)); } else { this.heroAnim.Play("idle"); } } private IEnumerator UpdateJoystickImpl() { while (true) { Vector3 direction = (Vector3.forward * this.joystick.Vertical + Vector3.right * joystick.Horizontal).normalized; if (direction != Vector3.zero) { this.heroGo.transform.rotation = Quaternion.LookRotation(direction); this.heroAnim.Play("run"); this.heroGo.transform.position += direction * 2.0f * Time.deltaTime; yield return null; } else { this.heroAnim.Play("idle"); } } } // Update is called once per frame private void UpdateKeyInput() { var keyY = Input.GetAxis("Vertical"); var keyX = Input.GetAxis("Horizontal"); Debug.LogFormat("keyY : {0}, keyX : {1}", keyY, keyX); var moveKey = new Vector3(keyX, 0, keyY).normalized; if (moveKey != Vector3.zero) { this.heroAnim.Play("run"); this.heroGo.transform.position += moveKey * 1.0f * Time.deltaTime; var rad = Mathf.Atan2(keyX, keyY); var dig = Mathf.Rad2Deg * rad; this.heroGo.transform.rotation = Quaternion.Euler(new Vector3(0, dig, 0)); } else { this.heroAnim.Play("idle"); } } private void UpdateMouseInput() { if(Input.GetMouseButton(0)) { //찍은 곳 스크린 좌표 표시 this.txtMousePoint.text = Input.mousePosition.ToString(); //찍은 곳 Ray생성 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //화면 Ray표시 Debug.DrawRay(ray.origin, ray.direction * 1000, Color.red, 5f); RaycastHit hitInfo; if(Physics.Raycast(ray.origin, ray.direction, out hitInfo, 1000)) { Debug.LogFormat("hitInfo.point : {0}", hitInfo.point.ToString()); this.txtHitPoint.text = hitInfo.point.ToString(); this.Move(hitInfo.point); } else { this.txtHitPoint.text = "None"; } } } public void Move(Vector3 targetCoordinate) { if(this.heroRoutine == null) { this.heroAnim.Play("run"); } if(this.heroRoutine != null) { StopAllCoroutines(); } this.heroRoutine = StartCoroutine(this.MoveHero(targetCoordinate, () => { this.heroAnim.Play("idle"); })); } private IEnumerator MoveHero(Vector3 targetCoordinate, System.Action OnCompleteMove) { this.heroGo.transform.LookAt(targetCoordinate); var dir = (targetCoordinate - this.heroGo.transform.position).normalized; var speed = dir * 1.0f * Time.deltaTime; while(true) { var distance = Vector3.Distance(targetCoordinate, this.heroGo.transform.position); this.heroGo.transform.position += speed; if(distance <= 0.01f) { this.heroGo.transform.position = targetCoordinate; break; } yield return null; } OnCompleteMove(); } } | cs |
'Unity > 수업내용' 카테고리의 다른 글
Scroll View 동적생성 (0) | 2019.05.28 |
---|---|
UGUI를 이용해 Scroll View UI 만들기 (0) | 2019.05.24 |