본문 바로가기
KDT/유니티 기초

23/08/06 [주말과제] 캐릭터 위치 이동 및 몬스터 공격(수정)

by 잰쟁 2023. 8. 6.
728x90

스크립트 예상해보기

1. 클릭한 곳을  월드좌표상 Ray객체로 생성

2. 클릭한 곳을 타겟으로 하여 캐릭터 이동시키고 멈추기

3. 몬스터가 타겟이 되면 공격 애니메이션 재생

4. 공격 중에 다른 곳을 클릭하면 클릭한 곳으로 다시 이동후 멈추기

 

1,2) 클릭한 곳까지 캐릭터 이동시키고 도착하면 멈추기

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

public class DogController : MonoBehaviour
{
    public Transform target;
    private Animator anim;
    private bool isMoveStart = false;
    private Vector3 targetPosition;

    // Start is called before the first frame update
    void Start()
    {
        this.anim = this.gameObject.GetComponent<Animator>();
    }

    void Update()
    {
        //마우스를 클릭한 곳을 월드좌표상의 Ray객체로 생성하기
        //왼쪽 마우스 버튼을 누르면
        if (Input.GetMouseButtonDown(0))
        {
            //화면좌표를 월드상의 Ray객체로 만들기
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            //화면에 Ray 출력하기
            Debug.DrawRay(ray.origin, ray.direction * 100f, Color.green, 2f); //2f : 2초동안 보여줌

            //Ray충돌 변수 정의
            RaycastHit hit;
            //Ray와 Collider를 충돌체크 하는 메서드
            if(Physics.Raycast(ray.origin,ray.direction,out hit))
            {
                //충돌 되었을때 충돌 정보가 hit 변수에 담김
                Debug.Log(hit.point);
                //충돌지점으로 캐릭터를 이동
                this.targetPosition = hit.point;
                //해당 방향을 쳐다보고
                this.transform.LookAt(hit.point);
                //걸어가는 애니메이션 실행
                this.anim.SetInteger("State", 1);
                this.isMoveStart = true;
            }
        }

        //실제 이동하기
        if (this.isMoveStart)
        {
            //로컬 좌표계로 정면으로 속도 3으로 이동
            this.transform.Translate(Vector3.forward * 3f * Time.deltaTime);
            //두 지점 간의 거리 체크
            float distance = Vector3.Distance(this.transform.position, this.targetPosition);

            //실제 멈추기
            if (distance <= 0.1f)
            {
                this.isMoveStart = false;
                this.anim.SetInteger("State", 0);
            }
        }        
    }
}

 

3) 몬스터 발견하면 이동 멈추고 공격하기

.

.

공격을 하고 싶어도 collider 처리 되어있어서 튕겨져 나와 공격할수가 없다ㅜㅜ

이런방법 저런방법 써봐도 공격실패,, 어떻게 처리하면 좋을지 더 고민해겠다

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

public class DogController : MonoBehaviour
{
    public Transform target;
    private Animator anim;
    private bool isMoveStart = false;
    private Vector3 targetPosition;
    public Rigidbody rBody;

    // Start is called before the first frame update
    void Start()
    {
        this.anim = this.GetComponent<Animator>();
        this.rBody = this.GetComponent<Rigidbody>();
    }

    void Update()
    {
        //마우스를 클릭한 곳을 월드좌표상의 Ray객체로 생성하기
        //왼쪽 마우스 버튼을 누르면
        if (Input.GetMouseButtonDown(0))
        {
            //화면좌표를 월드상의 Ray객체로 만들기
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            //화면에 Ray 출력하기
            Debug.DrawRay(ray.origin, ray.direction * 100f, Color.green, 2f); //2f : 2초동안 보여줌

            //Ray충돌 변수 정의
            RaycastHit hit;
            //Ray와 Collider를 충돌체크 하는 메서드
            if (Physics.Raycast(ray.origin, ray.direction, out hit))
            {
                //충돌 되었을때 충돌 정보가 hit 변수에 담김
                Debug.Log(hit.point);
                //충돌지점으로 캐릭터를 이동
                this.targetPosition = hit.point;
                //해당 방향을 쳐다보고
                this.transform.LookAt(hit.point);
                //걸어가는 애니메이션 실행
                this.anim.SetInteger("State", 1);
                this.isMoveStart = true;
            }
        }

        //실제 이동하기
        if (this.isMoveStart)
        {
            //로컬 좌표계로 정면으로 속도 3으로 이동
            this.transform.Translate(Vector3.forward * 3f * Time.deltaTime);
            //두 지점 간의 거리 체크
            float distance = Vector3.Distance(this.transform.position, this.targetPosition);

            //실제 멈추기
            if (distance <= 0.1f)
            {
                this.isMoveStart = false;
                if (tag == "Monster")  //태그로 몬스터 찾기
                {
                    Debug.Log("몬스터 발견!");

                    this.anim.SetInteger("State", 2);
                }
                else
                {
                    this.anim.SetInteger("State", 0);
                }
            }
        }
    }
}

흑흑