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

ArrowController
csharp
닫기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
csharp
닫기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);
}
}
}


'유니티 기초 > 복습' 카테고리의 다른 글
챕터6 ClimbCloud [미완] - 복습 (0) | 2023.08.04 |
---|---|
챕터 5 고양이 탈출 복습 - [완] (0) | 2023.08.04 |
[수업과제] 표창 던지기 - 복습 (0) | 2023.08.02 |
자동차 스와이프 Swipe Car - 복습 (0) | 2023.08.02 |
유니티 복습 (0) | 2023.08.01 |