2D 게임에서 캐릭터가 달리거나 점프를 해서 아이템을 획득하는 등의 플레이를 진행할때 배경이 무한대로 지나가는 모습을 구현할 수 있습니다. 시간차를 주어 각각의 배경레이어를 각기 다른 속도로 조절해서 좀더 입체적인 배경을 만들 수 있습니다.
Mathf.Repeat(value, max);
Mathf.Repeat()는 일정 범위 내에서 값을 반복(순환)시키고 싶은 경우 사용.
value 값은 0보다 작을수 없고 max값보다 클수 없다.
반드시 0 ~ max 범위의 값이 반환. Mathf.Repeat(3, 10)은 0 ~ 10 범위내에 있으므로 3을 반환.
Mathf.Repeat(12, 10)은 Max범위를 넘은 만큼인 2반환(12 - 10 = 2).
Mathf.Clamp(value, min, max);
Mathf.Clamp()는 value값을 최소값(min)에서 최대값(max)범위 내에 강제로 넣기 위해 사용.
value 값이 min ~ max 범위 안에 있을 때는 그대로 값이 반환.
value값이 min보다 작을 때는 min이 반환되고 max보다 클 때는 max가 반환된다.
[RepeatBG.cs]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RepeatBG : MonoBehaviour
{
[SerializeField] [Range(1f, 20f)] float speed = 3f;
[SerializeField] float posValue;
Vector2 startPos;
float newPos;
// Start is called before the first frame update
void Start()
{
startPos = transform.position;
}
// Update is called once per frame
void Update()
{
newPos = Mathf.Repeat(Time.time * speed, posValue);
transform.position = startPos + Vector2.right * newPos;
}
}
[N-Cube채널] www.youtube.com/channel/UCu48WEd7-leQbXoKiA_QM0w?sub_confirmation=1
엔큐브 스튜디오 [N Cube Studio]
www.youtube.com
댓글