게임개발/게임 클라이언트 프로그래밍

PirateBomb (3) 캡틴 공격하기

pudding81 2024. 2. 4. 13:48

 
 
 

 
 
 
캡틴과 2번 충돌하면 캡틴 사망
 
 
 

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UIElements;

public class BombGuyController : MonoBehaviour
{
    [SerializeField] private Animator anim;
    private Rigidbody2D rb;
    [SerializeField] private float moveforce=10f;
    
    private void Start()
    {
        this.rb = GetComponent<Rigidbody2D>();     
    }
 
    void Update()
    {      
        Move();
        InCamera();
       
    }

    private void InCamera()
    {
        //화면밖으로 나가지않게 하기
        float clampX = Mathf.Clamp(this.rb.transform.position.x, -2.2f, 2.2f);
        Vector3 pos = this.rb.transform.position;
        pos.x = clampX;
        this.rb.transform.position = pos;
    }

    private void Move()
    {
            this.anim.SetInteger("State", 0);//이동중이 아니라면 Idle 애니메이션 실행
        
        int dirX = 0; //방향
        if (Input.GetKey(KeyCode.RightArrow)) //오른쪽 이동
        {
            dirX = 1;

        }
        if (Input.GetKey(KeyCode.LeftArrow)) //왼쪽 이동
        {
            dirX = -1;

        }
        if (dirX != 0) // 이동중이라면
        {
            this.rb.transform.localScale = new Vector3(dirX, 1, 1); // 방향에 따라 바라보는 위치 변경
            this.anim.SetInteger("State", 1); // Run 애니메이션 실행
        }

        this.rb.AddForce(this.transform.right * dirX * moveforce);

    }

}
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;


public class EnemyController : MonoBehaviour
{
    public enum State //애니메이션
    {
        Idle, Hit, Dead
    }

    [SerializeField] private Animator anim;
    [SerializeField] private BombGameDirector gameDirector;
   

    private float hitAnimLength = 0.133f;
    private float dieAnimLength = 0.133f;

    private State state;
    private float delta = 0;
    private int maxHp = 2;
    private int hp;
    private Rigidbody2D rb;
  

    void Start()
    {
        this.rb = GetComponent<Rigidbody2D>();
        this.hp = this.maxHp;      
        this.gameDirector.UpdateHpText(this.hp, this.maxHp);
        this.SetState(State.Idle); // 시작 동작 

      
    }

 
    void Update()
    {

        switch (this.state)
        {
            case State.Idle:
                break;
            case State.Hit:
                this.delta += Time.deltaTime;
                if (this.delta > this.hitAnimLength)
                {
                    this.delta = 0; 
                    if (this.hp <= 0) //hp가 0보다 작으면
                    {
                        this.SetState(State.Dead);
                    }
                    else 
                    {
                        this.SetState(State.Idle);

                    }
                }
                break;

            case State.Dead:
                this.delta += Time.deltaTime;
                if (this.delta > this.dieAnimLength )
                {
                    this.delta = 0;
                    Destroy(this.gameObject);
                }
                
                break;
                
        }

               
    }

    private void SetState(State state)
    {
        if(this.state!= state)
        {
            this.state = state;
            this.anim.SetInteger("State",(int)this.state);
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
      
            this.hp -= 1;
            if (this.hp <= 0)
            {
                this.hp = 0;
            }
            this.gameDirector.UpdateHpText(this.hp, this.maxHp);
            this.SetState(State.Hit);

    }


}

 
 
공격애니  만들어보고 공격중에만 충돌하게 하려고 했지만 아예 충돌처리가 안됌
 
 

 

 public bool isHitting=false; // Hit 중인지
 
 public void Attack()
    {
        if(Input.GetKey(KeyCode.Space))
        {
            isHitting = true; // Hit 상태로 변경
            this.anim.SetInteger("State", 2); //공격
           
        }
        

    }
    // Hit 애니메이션 종료 시 호출되는 함수
    public void HitEnd()
    {
        isHitting = false; // Hit 상태 해제

    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
      BombGuyController player = GetComponent<BombGuyController>();
        if (player.isHitting) // 플레이어가 공격중일때
        {
        

            this.hp -= 1;
            if (this.hp <= 0)
            {
                this.hp = 0;
            }
            this.gameDirector.UpdateHpText(this.hp, this.maxHp);
            this.SetState(State.Hit);

        }
    }