1. ObjectHit 스크립트에서 태그로 충돌을 검사하여 Player일 때 색을 변경하고 자신의 Tag를 Hit으로 변경하기
2. Scorer 스크립트에서 태그로 충돌을 검사하여 Hit인지 검사하고 Hit이 아니면 점수를 올리기
ObjectHit.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectHit : MonoBehaviour
{
private void OnCollisionEnter(Collision collision)
{
// 충돌한 객체의 게임 오브젝트의 태그가 Player이면
if (collision.gameObject.tag == "Player")
{
GetComponent<MeshRenderer>().material.color = new Color(1, 0, 0, 1); // red
// ObjectHit이 있는 해당 오브젝트의 tag를 Hit으로 변경함
gameObject.tag = "Hit";
}
}
}
Scorer.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Scorer : MonoBehaviour
{
// 점수를 담을 변수
int hits = 0;
private void OnCollisionEnter(Collision collision)
{
// 충돌한 객체의 태그가 Hit이면(이미 충돌한 객체이면) 스킵
// if (collision.gameObject.tag != "Hit")과 반대
if (collision.gameObject.tag == "Hit")
return;
hits++;
// 문자열 + 변수이름 하면 붙어서 출력 (문자열변수 이런 식)
Debug.Log($"You've bumped into a thing this many times: " + hits);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Scorer : MonoBehaviour
{
// 점수를 담을 변수
int hits = 0;
private void OnCollisionEnter(Collision collision)
{
// hits가 hits에 1을 더한 값
// hits = hits + 1;와 동치
hits++;
// 문자열 + 변수이름 하면 붙어서 출력 (문자열변수 이런 식)
Debug.Log($"You've bumped into a thing this many times: " + hits);
}
}
Score
문제과 해결방안
해결할 문제 : 개체가 3초 정도 이후에 떨어지게 만드는 것
해결책 :
1. 타이머 - Time.time
2. 3초가 지나면 무엇인가를 하는 메커니즘 - if문
3. 개체를 3초 이후에 떨어지게 하는 방법 - disable / enable gravity (3초 이후에 중력 활성화)
Time.time
게임이 시작하고 난 이후부터 소요된 초 단위의 시간
- Time클래스의 프로퍼티 중 하나
- 읽기전용(접근은 가능해도 변경할 수 없음)
Dropper.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dropper : MonoBehaviour
{
void Start()
{
}
void Update()
{
Debug.Log(Time.time);
}
}
Debug Time.time
If
Dropper.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dropper : MonoBehaviour
{
[SerializeField]
private float seconds;
void Start()
{
// bool 값이 true(참)이면 if문 코드 실행 아니면 스킵
/*if (bool 값)
{
무언가를 하시오
}*/
}
void Update()
{
// 조건
if (Time.time > seconds)
{
Debug.Log($"{seconds} seconds has left");
}
}
}
if문과 Serialized 변수
Rigidbody
Rigidbody컴포넌트의 Use Gravity는 키면 중력을 허용하고 끄면 중력을 허용하지 않는다.
- MeshRenderer컴포넌트처럼 껐다가 킬 수 있음 => 둘 다 꺼서 떨어지기 전엔 안 보이도록 함
Rigidbody
캐싱
자주 사용되는 데이터나 정보를 필요할 때 쉽게 접근할 수 있도록 메모리에 저장하는 기술
캐싱
Dropper.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dropper : MonoBehaviour
{
// Meshrenderer형을 담는 renderer
MeshRenderer renderer;
Rigidbody rigid;
[SerializeField]
private float seconds;
void Start()
{
// GetComponent<MeshRenderer>().enabled = false;
// 캐싱
renderer = GetComponent<MeshRenderer>();
renderer.enabled = false;
rigid = GetComponent<Rigidbody>();
rigid.useGravity = false;
}
void Update()
{
// 조건
if (Time.time > seconds)
{
Debug.Log($"{seconds} seconds has left");
renderer.enabled = true;
rigid.useGravity = true;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectHit : MonoBehaviour
{
// private은 이 함수가 이 클래스 내에서만 접근할 수 있다는 말임
// Collision은 부딪힌 대상에 대한 정보가 들어있음
// 들어왔을 때 충돌을 감지함 (들어올 시점에 1번) - 떨어졌다 다시 부딪치면 다시 감지
private void OnCollisionEnter(Collision collision)
{
Debug.Log("Bumped into a wall");
// GetComponent메서드
GetComponent<MeshRenderer>().material.color = new Color(1, 0, 0, 1); // red
}
}