블록을 떨어뜨리기
Coddy Unity 여정의 Unity 스크립팅 기초 섹션에 포함된 레슨. 85개 중 77번째.
챌린지
중급목록에 있는 모든 블록이 떨어지도록 하세요. 이전 단계의 내용을 모두 유지하세요.
[SerializeField] private float fallSpeed = 5f;를 추가하세요.Update()의 끝에서foreach (GameObject block in blocks)로 반복문을 만드세요.- 반복문 안에
Vector3.down * fallSpeed * Time.deltaTime을block.transform.position에 더하세요.
‘플레이’를 누르세요. 블록이 러너를 지나 아래로 쏟아집니다. 그런 다음 ‘확인’을 누르세요.
직접 해보기
using System.Collections.Generic;
using UnityEngine;
public class Runner : MonoBehaviour
{
[SerializeField] private float speed = 6f;
[SerializeField] private float laneEdge = 4f;
[SerializeField] private GameObject blockPrefab;
[SerializeField] private float spawnEvery = 1f;
private List<GameObject> blocks = new List<GameObject>();
private float timer;
void Update()
{
Vector3 p = transform.position;
p.x += Input.GetAxisRaw("Horizontal") * speed * Time.deltaTime;
p.x = Mathf.Clamp(p.x, -laneEdge, laneEdge);
transform.position = p;
timer += Time.deltaTime;
if (timer >= spawnEvery)
{
timer -= spawnEvery;
Vector3 spot = new Vector3(Random.Range(-laneEdge, laneEdge), 10f, 0f);
blocks.Add(Instantiate(blockPrefab, spot, Quaternion.identity));
}
}
}
Unity 스크립팅 기초의 모든 레슨
직접 연습해 보세요: Unity 플레이그라운드