Last active
April 15, 2019 14:55
-
-
Save peterkimzz/a02f718166ea71d040a669e67cf75b35 to your computer and use it in GitHub Desktop.
캐릭터 땅 클릭으로 이동하기
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| public class PlayerController : MonoBehaviour | |
| { | |
| // 이동속도 | |
| [SerializeField] | |
| private float moveSpeed; | |
| private bool isMoving = false; | |
| // 화면 위에서 클릭하는 마우스 좌표 | |
| private Vector3 mouseHitPos; | |
| // 필요한 컴포넌트 | |
| [SerializeField] | |
| private CharacterController cc; | |
| void Update() | |
| { | |
| TryMove(); | |
| } | |
| private void TryMove() | |
| { | |
| // 마우스 우클릭 | |
| if (Input.GetMouseButton(0)) | |
| { | |
| RaycastHit mouseHit; | |
| // 제대로 된 Ground 태그를 눌러야만 이동을 시킬거임 | |
| if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out mouseHit)) | |
| { | |
| if (mouseHit.transform.tag == "Ground") | |
| { | |
| mouseHitPos = mouseHit.point; | |
| isMoving = true; | |
| } | |
| } | |
| } | |
| if (isMoving) Move(); | |
| } | |
| private void Move() | |
| { | |
| if (Vector3.Distance(transform.position, mouseHitPos) == 0f) | |
| { | |
| isMoving = false; | |
| return; | |
| } | |
| Vector3 dir = mouseHitPos - transform.position; | |
| Vector3 dirXZ = new Vector3(dir.x, 0f, dir.z); | |
| Vector3 targetPos = transform.position + dirXZ; | |
| Vector3 framePos = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime); | |
| Vector3 moveDir = (framePos - transform.position) + Physics.gravity; | |
| cc.Move(moveDir); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
이 방법은 리니지나 마비노기처럼 땅을 한 번 클릭했을 때 그 위치까지 부드럽게 일정한 속도로 이동하는 Script