3 분 소요

Ray를 이용해서 마우스 좌측클릭으로 좌표값을 받아옴

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ZombieController : MonoBehaviour
{

    public enum eActionState
    {
        IDEL    =0,
        WALK,
        RUN,
        ATTACK,
        HIT
    }

    eActionState _stateAction;  //나의 액션이 무엇인지를 저장하는 변수

    Animator _ctrlAni;          //처음에 animaotr의 parameter를 가져와야함


    private void Awake()            //내꺼면 start에서 가져오고, 남의것이면 Awake에서 가져오는게 더 편함
    {
        _ctrlAni = GetComponent<Animator>();    // animator 컴퍼넌트를 가져옴
    }

    void Start()
    {
    }

    void Update()
    {
        if(Input.GetButtonDown("Fire1"))
        //if (Input.GetMouseButtonDown(0))    //Fire1의 마우스 좌클릭이나, 마우스버튼의 0번이나 똑같음
        {
            RaycastHit hit;                                          //ray선에 맞는 물체를 불러오는 인자
            Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);      //마우스에서 쭉 나가는 ray선
            if (Physics.Raycast(r, out hit))                     //raycast를  쏠 때 물체에 맞으면 true, hit에 받아옴
            {
                Debug.Log(hit.transform.gameObject.ToString() + " : " + hit.point.ToString());       //좌표 보이게
            }                                               
        }
    }

    void ChangedAction(eActionState state)
    {
        _stateAction = state;
        _ctrlAni.SetInteger("AniSate", (int)_stateAction);
    }

    private void OnGUI()    //GUI를 만들어줌
    {
        if (GUI.Button(new Rect(0, 0, 200, 80), "IDEL"))           // 좌표 X=0, Y-0에 W(넓이):200, H(높이):80의 "WALK"라는 버튼을 만들어줌
        {                                                          // 버튼이 눌리면 true를 반환함
            _ctrlAni.SetInteger("AniSate", (int)eActionState.IDEL);                    //parameter에서 int형으로 만든 "Anistate"의 1번을 가져옴
        }

        if (GUI.Button(new Rect(0, 85, 200, 80), "WALK"))           
        {                                                          
            _ctrlAni.SetInteger("AniSate", (int)eActionState.WALK);     
        }
        if (GUI.Button(new Rect(0, 170, 200, 80), "RUN"))           
        {                                                          
            _ctrlAni.SetInteger("AniSate", (int)eActionState.RUN); 
        }



    }
}

image

받아온 좌표값으로 좀비가 움직이게 함

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ZombieController : MonoBehaviour
{

    public enum eActionState
    {
        IDEL    =0,
        WALK,
        RUN,
        ATTACK,
        HIT
    }

    eActionState _stateAction;  //나의 액션이 무엇인지를 저장하는 변수
    Animator _ctrlAni;          //처음에 animaotr의 parameter를 가져와야함
    Vector3 _posTarget;



    private void Awake()            //내꺼면 start에서 가져오고, 남의것이면 Awake에서 가져오는게 더 편함
    {
        _ctrlAni = GetComponent<Animator>();    // animator 컴퍼넌트를 가져옴
        _posTarget= transform.position;        //최초 위치는 좀비의 위치
    }

    void Start()
    {
    }

    void Update()
    {
        if(Input.GetButtonDown("Fire1"))
        //if (Input.GetMouseButtonDown(0))    //Fire1의 마우스 좌클릭이나, 마우스버튼의 0번이나 똑같음
        {
            RaycastHit hit;                                          //ray선에 맞는 물체를 불러오는 인자
            Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);      //마우스에서 쭉 나가는 ray선
            if (Physics.Raycast(r, out hit))                     //raycast를  쏠 때 물체에 맞으면 true, hit에 받아옴
            {
                //Debug.Log(hit.transform.gameObject.ToString() + " : " + hit.point.ToString());       //좌표 보이게
                _posTarget= hit.point;       //hit.point = 마우스 좌클릭한 지점이 좀비의 위치로 저장(업데이트)됨
            }                                               
        }
        transform.position = Vector3.Lerp(transform.position, _posTarget, Time.deltaTime);
                                    //Lerp(나의현재위치, 목표위치, 시간) time에 의해 현재위치에서 목표위치까지 움직여야 한다는 정보를 현재 물체의 position에 저장(업데이트)
    }

    void ChangedAction(eActionState state)
    {
        _stateAction = state;
        _ctrlAni.SetInteger("AniSate", (int)_stateAction);
    }

    private void OnGUI()    //GUI를 만들어줌
    {
        if (GUI.Button(new Rect(0, 0, 200, 80), "IDEL"))           // 좌표 X=0, Y-0에 W(넓이):200, H(높이):80의 "WALK"라는 버튼을 만들어줌
        {                                                          // 버튼이 눌리면 true를 반환함
            _ctrlAni.SetInteger("AniSate", (int)eActionState.IDEL);                    //parameter에서 int형으로 만든 "Anistate"의 1번을 가져옴
        }

        if (GUI.Button(new Rect(0, 85, 200, 80), "WALK"))           
        {                                                          
            _ctrlAni.SetInteger("AniSate", (int)eActionState.WALK);     
        }
        if (GUI.Button(new Rect(0, 170, 200, 80), "RUN"))           
        {                                                          
            _ctrlAni.SetInteger("AniSate", (int)eActionState.RUN); 
        }



    }
}
Vector3 _posTarget;

void Update()
    {
        if(Input.GetButtonDown("Fire1"))
        //if (Input.GetMouseButtonDown(0))    //Fire1의 마우스 좌클릭이나, 마우스버튼의 0번이나 똑같음
        {
            RaycastHit hit;                                          //ray선에 맞는 물체를 불러오는 인자
            Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);      //마우스에서 쭉 나가는 ray선
            if (Physics.Raycast(r, out hit))                     //raycast를  쏠 때 물체에 맞으면 true, hit에 받아옴
            {
                //Debug.Log(hit.transform.gameObject.ToString() + " : " + hit.point.ToString());       //좌표 보이게
                _posTarget= hit.point;       //hit.point = 마우스 좌클릭한 지점이 좀비의 위치로 저장(업데이트)됨
            }                                               
        }
        transform.position = Vector3.Lerp(transform.position, _posTarget, Time.deltaTime);
                                    //Lerp(나의현재위치, 목표위치, 시간) time에 의해 현재위치에서 목표위치까지 움직여야 한다는 정보를 현재 물체의 position에 저장(업데이트)
    }

속도가 일정하게 만들어주기


[SerializeField] float _movSpeed;

transform.position = Vector3.MoveTowards(transform.position, _posTarget, Time.deltaTime * _movSpeed);

방향전환

transform.LookAt( _posTarget );
  • 업데이트 부분에 transform.LookAt( _posTarget )만 추가하면 됨

최종(애니메이션 추가)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ZombieController : MonoBehaviour
{

    public enum eActionState
    {
        IDLE     =0,
        WALK,
        RUN
    }

    [SerializeField] float _movSpeed;

    eActionState _stateAction;  //나의 액션이 무엇인지를 저장하는 변수
    Animator _ctrlAni;          //처음에 animaotr의 parameter를 가져와야함
    Vector3 _posTarget;
    


    private void Awake()            //내꺼면 start에서 가져오고, 남의것이면 Awake에서 가져오는게 더 편함
    {
        _ctrlAni = GetComponent<Animator>();    // animator 컴퍼넌트를 가져옴
        _posTarget = transform.position;        //최초 위치는 좀비의 위치
    }

    void Start()
    {
    }

    void Update()
    {
        if(Input.GetButtonDown("Fire1"))
        //if (Input.GetMouseButtonDown(0))    //Fire1의 마우스 좌클릭이나, 마우스버튼의 0번이나 똑같음
        {
            RaycastHit hit;                                          //ray선에 맞는 물체를 불러오는 인자
            Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);      //마우스에서 쭉 나가는 ray선
            if (Physics.Raycast(r, out hit))                     //raycast를  쏠 때 물체에 맞으면 true, hit에 받아옴
            {
                //Debug.Log(hit.transform.gameObject.ToString() + " : " + hit.point.ToString());       //좌표 보이게
                _posTarget= hit.point;       //hit.point = 마우스 좌클릭한 지점이 좀비의 위치로 저장(업데이트)됨
                transform.LookAt( _posTarget );
                ChangedAction(eActionState.WALK);
            }
            

        }
        if(Vector3.Distance(transform.position,_posTarget) <= 0.000155f)          // 나의위치와 목표의 위치가 0이 정지한 것을 뜻함,     float은 같다, 같지않다가 존재하지 않고 크다, 작다만 존재함
        {
            ChangedAction(eActionState.IDLE);
        }
        //transform.position = Vector3.Lerp(transform.position, _posTarget, Time.deltaTime);
        //Lerp(나의현재위치, 목표위치, 시간) time에 의해 현재위치에서 목표위치까지 움직여야 한다는 정보를 현재 물체의 position에 저장(업데이트)
        transform.position = Vector3.MoveTowards(transform.position, _posTarget, Time.deltaTime * _movSpeed);
        
    }

    void ChangedAction(eActionState state)
    {
        _stateAction = state;
        _ctrlAni.SetInteger("AniSate", (int)_stateAction);
    }

   
}
  • ChangedAction(eActionState.WALK); 업데이트에 추가
  • 걸어다닐때 walk의 모습이 나오게 speed를 잘 맞춰야함(싱크맞추기)
  • 멈출때 Idel로 바껴야함

댓글남기기