유니티 엔진 클라이언트/수업내용
[유니티] 엔진 사용 충돌 판정 만들
Bueong_E
2023. 1. 30. 17:01
반응형
SMALL
코드에 들어가기 전 오늘 배운 내용
- 1. 프로젝트 생성후 해야하는것
- 레이아웃 설정
- 플랫폼 변경
- 해상도 설정
- 씬만들기 (GameScene)
- Textures 폴더에 리소스 넣기
- 리소스 배치
- GameObject.find("게임 오브젝트명 문자열로") - 생성되어있는 오브젝트만 불러올수 있다.
- awake - 인스턴스가 생성될때 start 보다 먼저 호출된다.
- AudioSource : 컴포넌트
- this.GetComponent<AudioSource>().Play(); 로 사용 ( 유니티 컴포넌트 UI상에 Play On Awake 체크시 인스턴스가 생성되면 바로 재생됨)
- Input.GetMouseButtonDown &Input.GetMouseButton &Input.GetMouseButtonUp
- 각각 마우스를 누르면 & 마우스를 누르는 동안 & 마우스 버튼을 떼면 이다.
- Input.mousePosition = 마우스가 마지막으로 위치한 스크린 좌표값을 반환
- 자동차 스와이프 프로젝트에서는 이를 이용하여 endPos , startPos 를 Input.mousePosition 에 각각 대입하여 스와이프 입력을 구현하였다.
- 이를 이용하여 마우스 버튼에서 손을 뗀 마지막 스크린 좌표 위치에 마우스를 클릭한 스크린좌표 시작 위치를 빼 두 지점간의 거리를 구해 실수 변수 speed 에 할당하여 얼마만큼 이동할지를 정해주었다.
- 얼마나 이동할지는 객체의 로컬 좌표를 의미하는 translate 메서드를 이용했는데 translate 메서드는 현재 좌표의 상대적인 이동값을 나타내는 것으로 월드 좌표계와는 다르게 현재 객체의 위치에서 얼마나 이동시킬지를 지정해주는 메서드이다 (사용은 this.transform.Translate(0, 0, 0) 형식으로 transform에서 한번 더 접근하여 사용)
- 추가적으로 UI를 나타낼땐 또 다른 좌표계인 Rect Transform 을 사용하는데 이는 위치 회전 크기는 물론이고 피벗(회전)과 앵커(UI리소스를 배치할때 기준이 되는 위치를 지정)또한 변경 가능한 좌표계이다.
- UI를 갱신할땐 this."UI 컴포넌트 명".GetComponent<Text>().text 메서드를 사용하여 UI를 갱신시켜 줄수 있다.
고양이 화살표 피하기 프로젝트 코드
플레이어 컨트롤 스크립트
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
public float radius;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
Debug.Log("왼쪽 화살표 눌림");
this.transform.Translate(-3, 0, 0); //현재 위치에서 -3 유닛만큼 이동
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
Debug.Log("오른쪽 화살표 눌림");
this.transform.Translate(3, 0, 0); //현재 위치에서 +3 유닛만큼 이동
}
}
//이벤트 함수
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(this.transform.position, 1f);
}
}
화살표 스크립트
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ArrowController : MonoBehaviour
{
public float radius;
private GameObject player;
private const float GROUND = -3.978f;
// Start is called before the first frame update
void Start()
{
this.player = GameObject.Find("player");
}
// Update is called once per frame
void Update()
{
this.transform.Translate(0, -0.1f, 0); //아래쪽으로 -0.1 유닛씩 이동
Vector2 p1 = this.transform.position; //화살표의 중심 좌표
Vector2 p2 = this.player.transform.position; //플레이어의 중심좌표
var distance = Vector2.Distance(p1, p2);
var r = this.radius + this.player.GetComponent<PlayerController>().radius;
if (distance < r)
{
//충돌
Destroy(this.gameObject); //게임 오브젝트 제거
}
if (this.transform.position.y < GROUND)
{
Destroy(this.gameObject); //게임 오브젝트 제거
//Destroy(this); //ArrowController의 인스턴스 제거 : 컴포넌트 제거
}
}
//이벤트 함수
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(this.transform.position, this.radius);
}
}
App 스크립트 ( 충돌판정 확인용)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class App : MonoBehaviour
{
private PlayerController cat;
private ArrowController arrow;
// Start is called before the first frame update
void Start()
{
this.cat = GameObject.Find("player").GetComponent<PlayerController>();
this.arrow = GameObject.Find("arrow").GetComponent<ArrowController>();
Debug.Log(this.cat.radius + this.arrow.radius); // 반지름 더한거 r1 + r2
Debug.Log(Vector3.Distance(this.cat.transform.position, this.arrow.transform.position));
}
// Update is called once per frame
void Update()
{
}
}
결과물
반응형
LIST