도로 교통
Coddy Unity 여정의 Unity 스크립팅 기초 섹션에 포함된 레슨. 85개 중 83번째.
챌린지
어려움차들이 도로를 따라가게 하세요.
spawnEvery초마다(-9, 0.5, lanes[Random.Range(0, lanes.Length)])위치에 차를 생성해 목록에 추가하세요.- 각 차를
carSpeed속도로 +x 방향으로 이동시키세요. x가9를 지나면 차를 파괴하고 목록에서 제거하세요. - x축에서
1.5보다 가깝고 z축에서1보다 가까운 차가 있으면Hit!를 기록하고 호퍼를start위치로 되돌리세요.
‘플레이’를 누르세요. 차들이 지나갑니다. 그런 다음 ‘확인’을 누르세요.
직접 해보기
using System.Collections.Generic;
using UnityEngine;
public class Crossing : MonoBehaviour
{
[SerializeField] private GameObject carPrefab;
[SerializeField] private float[] lanes;
[SerializeField] private float spawnEvery = 1f;
[SerializeField] private float carSpeed = 5f;
private Vector3 start;
void Start()
{
start = transform.position;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
transform.position += new Vector3(0f, 0f, 2f);
}
if (Input.GetKeyDown(KeyCode.DownArrow) && transform.position.z > start.z)
{
transform.position -= new Vector3(0f, 0f, 2f);
}
if (transform.position.z >= 4f)
{
Debug.Log("You made it!");
enabled = false;
return;
}
// 자동차: 다음 강의에서 다룹니다.
}
}
Unity 스크립팅 기초의 모든 레슨
직접 연습해 보세요: Unity 플레이그라운드