Hittest.cs
using Sample;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Sample
{
public class ComponentTest : MonoBehaviour
{
//필드
//속도
public float moveSpeed = 5f;
//타겟
public Transform target; //Target 게임 오브젝트의 Transform 객체로 접근
public GameObject gTarget; //Target 게임 오브젝트의 객체로 접근
public TargetTest cTarget; //TargetTest 에 직접 접근
// Start is called before the first frame update
void Start()
{
//문법
//TargetTest tTest = new TargetTest();
//tTest.b = 30;
//target 오브젝트에 트랜스폼에 붙어있는 TargetTest의 객체에 접근
//TargetTest tTest = target.GetComponent<TargetTest>();
//tTest.b = 30; //public 한 필드를 가져와 사용
//Debug.Log(tTest.GetA()); // private a 를 public한 메서드로 접근해서 사용
//ComponentTest 스크립트가 붙어있는 게임 오브젝트의 트랜스폼 객체
//this.transform
//ComponentTest 스크립트가 붙어있는 게임 오브젝트의 객체
//this.gameObject
//target 오브젝트에 붙어있는 TargetTest의 객체에 접근
//TargetTest gTest = gTarget.GetComponent<TargetTest>();
//gTest.b = 50; //public 한 필드를 가져와 사용
//Debug.Log(gTest.GetA());// private a 를 public한 메서드로 접근해서 사용
//Target 게임 오브젝트에 붙어있는 TargetTest의 객체에 직접 접근
cTarget.b = 70;
Debug.Log(cTarget.b);
Debug.Log(cTarget.transform.position);
//플레이어
//ComponentTest 스크립트가 붙어있는 게임 오브젝트의 트랜스폼 객체
//this.transform
//this.transform.GetComponent<>
//ComponentTest 스크립트가 붙어있는 게임 오브젝트의 객체
//this.gameObject
//this.gameObject.GetComponent<>
//this.transform.gameObject //ComponentTest 스크립트가 붙어있는 트랜스폼에 붙어있는 게임오브젝트에 접근
//this.gameObject.transform //ComponentTest 스크립트가 붙어있는 게임오브젝트에 붙어있는 트랜스폼에 접근
}
// Update is called once per frame
void Update()
{
//타겟으로 이동
// 방향 * Time.deltaTime * 속도
Vector3 dir = target.position - this.transform.position;
this.transform.Translate(dir.normalized * Time.deltaTime * moveSpeed);
}
}
}
TargetTest.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static UnityEngine.GraphicsBuffer;
namespace Sample
{
// 사각형 데이터를 관리하는 구조체
public struct BoxData
{
public float x; // 박스의 중심 x 좌표
public float y; // 박스의 중심 y 좌표
public float w; // 박스의 너비
public float h; // 박스의 높이
}
// 원 데이터를 관리하는 구조체
public struct CircleDate
{
public float x; // 원의 중심 x 좌표
public float y; // 원의 중심 y 좌표
public float r; // 원의 높이
}
public class Hittest : MonoBehaviour
{
//필드
//타겟
public Transform target;
//이동 속도
private float moveSpeed = 300;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (CeackPassPosition(target))
{
Debug.Log("충돌");
}
}
// 매개변수로 받은 두 개의 박스가 충돌했는지 체크
// 충돌하면 true, 충돌하지 않으면 false 반환
public bool CheakHitBox(BoxData a, BoxData b)
{
// A와 B의 각 변의 절반 길이를 미리 계산하여 변수에 저장
float halfWidthA = a.w / 2;
float halfHeightA = a.h / 2;
float halfWidthB = b.w / 2;
float halfHeightB = b.h / 2;
// A와 B의 중심 사이의 거리와 각 박스의 절반 너비 및 높이를 비교
// x축에서의 충돌 체크
if (Mathf.Abs(a.x - b.x) > (halfWidthA + halfWidthB))
{
return false;
}
// y축에서의 충돌 체크
if (Mathf.Abs(a.y - b.y) > (halfHeightA + halfHeightB))
{
return false;
}
// 충돌이 발생한 경우
return true;
}
//매개변수로 받은 두개의 원이 충돌했는지 체크
// 충돌하면 true, 충돌하지 않으면 false 반환
public bool CheakHitCircle(CircleDate a, CircleDate b)
{
// x y r
float distX = a.x - b.x;
float distY = a.y - b.y;
//두원의 거리 (Mathf.Sqrt) = 제곱근
float distance = Mathf.Sqrt(distX * distX + distY * distY);
//두원의 반지름의 합
float sumradius = a.r + b.r;
//두원의 거리보다 두원의 반지름의 합보다 작으면 충돌(true)
if (distance <= sumradius)
{
return true;
}
else
{
return false;
}
}
//타겟까지의 거리가 일정 거리안에 있으면 충돌이라고 판정
private bool CheckArrivePosition(Transform target)
{
float distance = Vector3.Distance(this.transform.position, target.position);
if (distance <= 0.5f)
{
return true;
}
else
{
return false;
}
}
//타겟까지 이동할 남은 거리가 이번(한)프레임에 이동하는 거리보다 작을때 충돌이라고 판정
private bool CeackPassPosition(Transform target)
{
//타켓까지 이동할 남은 거리
float distance = Vector3.Distance(this.transform.position, target.position);
//이번(한)프레임에 이동하는 거리
float distanceThisFrame = Time.deltaTime * moveSpeed;
if (distance < distanceThisFrame) //초당속도
{
return true;
}
else
{
return false;
}
}
}
}
ComponentTest.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Sample
{
public class TargetTest : MonoBehaviour
{
//필드
private int a = 10; //
public int b = 20; //
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
//private a를 public한 메서드 반환
public int GetA()
{
return a;
}
}
}
'수업일지 > 유니티_타워디펜스' 카테고리의 다른 글
24-09-06 29일차 카메라제어 ,이동제어 (0) | 2024.09.06 |
---|---|
24-09-05 28일차 오브젝트접근방법, 싱글톤패턴 (0) | 2024.09.05 |
24-09-03 26일차 Rotation- 회전 (0) | 2024.09.04 |
24-09-02 25일차 타이머, 코루틴, UI-Text (0) | 2024.09.02 |
24-08-29 24일차 Transform Translate Time.deltaTime Destroy (0) | 2024.08.29 |