승리 또는 패배
Coddy Unity 여정의 물리와 게임플레이 섹션에 포함된 레슨. 63개 중 32번째.
챌린지
중급마지막 단계: 승리하는 방법을 추가합니다. 이전 단계의 내용을 모두 유지하고 Ball에 다음을 추가하세요:
public int target = 10;(Inspector에서 8로 설정합니다).timeLeft가 0보다 크고score가target보다 작은 동안 게임이 진행됩니다. 이를 메서드bool IsPlaying()로 작성하고 사용하세요. 게임이 진행되는 동안에만 시계가 카운트다운되고, 방향키로 공을 움직이며, 코인으로 점수를 올릴 수 있습니다.- 코인을 획득해
score가target이상이 되면 점수 표시 줄 다음에You win!을 로그에 출력하세요. - 시간이 먼저 끝나면
Time's up!줄 다음에You lose!를 로그에 출력하세요.
Play를 누르고 시간이 끝나기 전에 8점을 획득한 다음 Check를 누르세요.
직접 해보기
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);
}
}