using UnityEngine; 

using System.Collections; 


public class PlayerCtrl : MonoBehaviour 

    public float velocity = 1.0f; 

    public LayerMask layer; 


    private CharacterController _controller; 


    private bool _isMove = false; 

    private Vector3 _destination = new Vector3(0, 0, 0); 


void Start () 

    { 

        _controller = gameObject.GetComponent<CharacterController>(); 

        _isMove = false; 


void Update () 

    { 


        RaycastHit _hit = new RaycastHit(); 


        //화면을 클릭 하면 땅바닥 좌표 저장. 

        if (Input.GetMouseButton(0)) 

        { 

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 


            if (Physics.Raycast(ray, out _hit, Mathf.Infinity, layer)) 

            { 

                _destination = _hit.point; 

                _isMove = true; 

            } 

        } 


        //땅바닥 좌표가 플레이어와 다르면 움직인다. 

        if (_isMove) 

        { 

            Move(); 

        } 


    //움직이는 함수 

    private void Move() 

    { 

        //목적지와 거리가 같으면 안 움직임 

        if (Vector3.Distance(transform.position, _destination) == 0.0f) 

        { 

            _isMove = false; 

            return; 

        } 


        Vector3 direction = _destination - transform.position; 

        direction = Vector3.Normalize(direction); 


        _controller.Move(direction * Time.deltaTime * velocity); 

    } 

'유니티3D > 함수' 카테고리의 다른 글

UNITY3D 자주쓰는 스크립트_4  (0) 2013.10.08
UNITY3D 자주쓰는 스크립트_3  (0) 2013.09.29
UNITY3D 자주쓰는 스크립트_2  (0) 2013.09.27
UNITY3D 자주쓰는 스크립트_1  (0) 2013.09.27
Rigidbody.AddForce()  (0) 2013.08.07

+ Recent posts