Score
Scorer.cs
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);
}
}
문제과 해결방안
해결할 문제 : 개체가 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);
}
}
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");
}
}
}
Rigidbody
Rigidbody컴포넌트의 Use Gravity는 키면 중력을 허용하고 끄면 중력을 허용하지 않는다.
- MeshRenderer컴포넌트처럼 껐다가 킬 수 있음 => 둘 다 꺼서 떨어지기 전엔 안 보이도록 함
캐싱
자주 사용되는 데이터나 정보를 필요할 때 쉽게 접근할 수 있도록 메모리에 저장하는 기술
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;
}
}
}
Directional light
- 위치는 상관 없고 회전이 그림자에 관련 있음
'유데미 강의 > C#과 Unity로 3D 게임 개발하기 : 장애물 코스' 카테고리의 다른 글
회전하는 객체, Prefab, 장애물 코스 만들기 (0) | 2022.08.02 |
---|---|
Tag (0) | 2022.08.02 |
OnCollisionEnter(), GetComponent<>() (0) | 2022.08.02 |
메서드 (0) | 2022.08.02 |
Collision (0) | 2022.07.28 |