유니티 기초/복습

챕터5 고양이탈출 - CatEscape - 복습

다모아 2023. 8. 2. 01:55

Gizmo 사용법 - Radius를 수정하면 빨간원이 줄었다 커졌다 한다.

 

ArrowController

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

public class ArrowController : MonoBehaviour
{
    public float speed;

    private GameObject catGo;

    // Start is called before the first frame update
    void Start()
    {
        this.catGo = GameObject.Find("cat");


    }

    private bool IsCollide()
    {
        //두 원사이의 거리
        var dis = Vector3.Distance(this.transform.position, this.catGo.transform.position);

        //반지름의 합
        CatController catController = this.catGo.GetComponent<CatController>();
        float sumRadius = this.radius + catController.radius;

        //두 원 사이의 거리가 두 원의 반지름의 합보다 크면 false (부딪히지 않았다)

        //두 원의 반지름의 합이 두 원 사이의 거리보다 크다면 부딪혔다. true

        return dis < sumRadius;
    }

    public float radius = 1;
    //이벤트 함수
    private void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(this.transform.position, this.radius);
    }

    // Update is called once per frame
    void Update()
    {
        this.transform.Translate(this.speed * Vector3.down * Time.deltaTime);

        if(this.transform.position.y <= -4.562)
        {
            Destroy(this.gameObject);
        }

        if(this.IsCollide())
        {
            Destroy(this.gameObject);
        }
    }
}

 

CatController

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

public class CatController : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    public float radius = 1;
    //이벤트 함수
    private void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(this.transform.position, this.radius);
    }
    // Update is called once per frame
    void Update()
    {
        //왼쪽 키를 누르면
        if(Input.GetKeyDown(KeyCode.LeftArrow))
        {
            this.transform.Translate(-2, 0, 0);
        }
        else if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            this.transform.Translate(2, 0, 0);
        }
    }
}

조금만 기다리면 움직입니다..