Menu
CoddyTech

별을 하나씩

Coddy Unity 여정의 Unity 스크립팅 기초 섹션에 포함된 레슨. 85개 중 53번째.

challenge icon

챌린지

중급

별을 잡거나 놓칠 때마다 새 별을 보내세요.

  1. void ResetStar()를 작성하여 star.position = new Vector3(Random.Range(-4f, 4f), 6f, 0f);를 설정하고, 잡았을 때 기존 줄 대신 이 함수를 호출하세요.
  2. 잡았을 때의 if 뒤에 else if (star.position.y < 0f)를 추가하여 ResetStar();를 호출하세요. else if는 그 앞의 if가 거짓일 때만 실행됩니다.

‘플레이’를 누르세요. 별이 새로 생성될 때마다 새로운 x 위치에서 떨어집니다. 그런 다음 ‘확인’을 누르세요.

직접 해보기

using UnityEngine;

public class Catcher : MonoBehaviour
{
    [SerializeField] private float speed = 6f;
    [SerializeField] private Transform star;
    [SerializeField] private float fallSpeed = 3f;
    private int score;

    void Update()
    {
        float x = transform.position.x;
        if (Input.GetKey(KeyCode.RightArrow))
        {
            x += speed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            x -= speed * Time.deltaTime;
        }
        x = Mathf.Clamp(x, -4f, 4f);
        transform.position = new Vector3(x, transform.position.y, transform.position.z);

        star.position += Vector3.down * fallSpeed * Time.deltaTime;

        if (Vector3.Distance(transform.position, star.position) < 1f)
        {
            score++;
            Debug.Log("Caught! Score: " + score);
            star.position = new Vector3(star.position.x, 6f, star.position.z);
        }
    }
}

Unity 스크립팅 기초의 모든 레슨

직접 연습해 보세요: Unity 플레이그라운드