승리!
Coddy Unity 여정의 물리와 게임플레이 섹션에 포함된 레슨. 71개 중 33번째.
챌린지
중급플레이어가 target점에 도달하면 이기도록 하세요.
Ball.cs에public int target = 10;과bool IsPlaying()을 추가하고,timeLeft > 0f && score < target을 반환하도록 하세요.Update()와FixedUpdate()에서timeLeft > 0f대신IsPlaying()을 사용하고,AddScore를if (!IsPlaying()) return;로 시작하세요.- 점수 줄 다음에
score >= target이면You win!을 로그에 출력하세요.
‘플레이’를 누르고 8점을 모으세요. 그런 다음 ‘확인’을 누르세요.
직접 해보기
using UnityEngine;
public class Ball : MonoBehaviour
{
public float force = 10f;
public int score;
public float timeLimit = 60f;
public float timeLeft;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
timeLeft = timeLimit;
}
void Update()
{
if (timeLeft > 0f)
{
timeLeft -= Time.deltaTime;
if (timeLeft <= 0f)
{
timeLeft = 0f;
Debug.Log("Time's up! Score: " + score);
}
}
}
void FixedUpdate()
{
if (timeLeft > 0f)
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
rb.AddForce(new Vector3(x, 0f, z) * force);
}
}
public void AddScore(int points)
{
score += points;
Debug.Log("Score: " + score);
}
}
물리와 게임플레이의 모든 레슨
직접 연습해 보세요: Unity 플레이그라운드