Project/우다다다 고영희
[우다다다 고영희] Unity: 무한 배경 만들기
FiveReptile
2020. 5. 22. 16:27
728x90
백그라운드 스크롤링편에서 다루었던 코드를 그대로 가져와서 실행시켰다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BackgroundScrolling : MonoBehaviour
{
public float speed;
public Transform[] backgrounds;
float leftPosX = 0f;
float rightPosX = 0f;
float xScreenHalfSize;
float yScreenHalfSize;
void Start()
{
yScreenHalfSize = Camera.main.orthographicSize;
xScreenHalfSize = yScreenHalfSize * Camera.main.aspect;
leftPosX = -(xScreenHalfSize * 2);
rightPosX = xScreenHalfSize * 2 * backgrounds.Length;
}
void Update()
{
for(int i = 0; i < backgrounds.Length; i++)
{
backgrounds[i].position += new Vector3(-speed, 0, 0) * Time.deltaTime;
if(backgrounds[i].position.x < leftPosX)
{
Vector3 nextPos = backgrounds[i].position;
nextPos = new Vector3(nextPos.x + rightPosX, nextPos.y, nextPos.z);
backgrounds[i].position = nextPos;
}
}
}
}
|
블로그 하니까 코드 가져와서 쓰기가 편하네 헝헝 :)
728x90