K-digital traning/유니티 기초

[주말과제] 이동 및 몬스터 공격

내꺼블로그 2023. 8. 7. 00:41

1.DogController.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices.WindowsRuntime;
using Unity.VisualScripting;
using UnityEngine;

public class DogController : MonoBehaviour
{
    public float moveSpeed = 1f;
    public int state;
    private int damage = 8;
    private float streamTime;
    private bool isRun;
    private bool isAttack;
    private Vector3 target;
    Animator anim;
    GameObject monsterGo;
    MonsterController monsterController;

    // Start is called before the first frame update
    void Start()
    {
        this.anim = GetComponent<Animator>();
        monsterGo = GameObject.Find("Monster");
        monsterController=monsterGo.GetComponent<MonsterController>();
    }

    // Update is called once per frame
    void Update()
    {
    	//입력받은 마우스버튼 위치로 캐릭터 이동
        if(Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            Debug.DrawRay(ray.origin, ray.direction * 100f, Color.red, 5f);
            RaycastHit hitPosition;
            if(Physics.Raycast(ray.origin, ray.direction*100f, out hitPosition))
            {
                target = hitPosition.point;
                this.transform.LookAt(target);
                Debug.LogFormat("targetposition: {0}", target);
            }
            isRun = true;
            isAttack = false;
        }
        if (isRun)
        {
            this.transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
            state = 1;
            float distance = Vector3.Distance(target, this.transform.position);
            if (distance < 0.1f)
            {
                isRun = false;
                state = 0;
            }
            //만약 이동한 장소에 몬스터가 있을 시 몬스터 공격
            else if (Vector3.Distance(this.transform.position, monsterGo.transform.position) < 1.2f && Vector3.Distance(target, monsterGo.transform.position) < 1f)
            {
                isRun = false;
                isAttack = true;
            }
        }
        if (isAttack)
        {
            Attack();
            
        }
        anim.SetInteger("state", state);
        anim.speed = moveSpeed * Time.deltaTime * 27f;
    }
	
    //몬스터 공격 함수
    void Attack()
    {
        state = 2;
        this.transform.LookAt(monsterGo.transform.position);
        this.streamTime += Time.deltaTime;
        if (this.streamTime > 1f)
        {
            monsterController.HitDamage(this.damage);
            streamTime = 0f;
            //몬스터가 죽으면 IDLE 상태로 transition
            if (monsterController.isDie)
            {
                isAttack = false;
                state = 0;
            }
        }
    }
}

state 0: IDLE, 1: RUN, 2:ATTACK

 

 

2.MonsterController.cs

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

public class MonsterController : MonoBehaviour
{
    private int hp;
    private int maxHp;
    private int state;
    public bool isDie;
    private Animator anim;
    GameObject DogGo;
    DogController dogController;

    // Start is called before the first frame update
    void Start()
    {
        anim=GetComponent<Animator>();
        DogGo = GameObject.Find("Dog");
        dogController = DogGo.GetComponent<DogController>();
        this.maxHp = 30;
        this.hp = maxHp;
    }

    // Update is called once per frame
    void Update()
    {	
    	//죽지 않은 상태
        if (!isDie)
        {	
        	//강아지 공격X라면 IDLE 상태
            if (dogController.state != 2)
                this.state = 0;
        }
        //죽은경우
        else
        {
            this.state = 2;
        }
        anim.SetInteger("state", state);
    }

	//공격당하는 함수
    public void HitDamage(int damage)
    {
        Debug.LogFormat("monsterHp: {0}", this.hp);
        state = 1;
        this.hp -= damage;
        //hp가 0이하일 시 죽음.
        if (this.hp <= 0) {
            this.isDie = true; }
        Debug.LogFormat("monsterState: {0}", this.state);
        
    }
}

state 0: IDLE, 1: HITDAMAGE, 2: DIE

 

 

 

3.Dog animation

 

 

 

4.Monster animation

 

 

 

 

 

 

결과화면

'K-digital traning > 유니티 기초' 카테고리의 다른 글

[주말과제]SimpleRPG  (1) 2023.08.14
몬스터 처지 후 아이템 드롭 + 장착  (0) 2023.08.10
애니메이션 전환 연습  (0) 2023.08.03
캐릭터 이동(달리기) 애니메이션  (0) 2023.08.03
ClimbCloud  (0) 2023.08.02