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
'Project > 우다다다 고영희' 카테고리의 다른 글
[우다다다 고영희] Unity: UI 화면 구성하기 (0) | 2020.05.25 |
---|---|
[우다다다 고영희] Unity: 오브젝트 풀링을 이용한 무한 장애물 (0) | 2020.05.24 |
[우다다다 고영희] Unity: 플레이어 움직이기 (0) | 2020.05.21 |
[우다다다 고영희] Aseprite: 스프라이트 만들기 (0) | 2020.05.20 |
[우다다다 고영희] Unity: 게임 기획 (0) | 2020.05.19 |