blob: 2776a6d91ddea314aa2e5027692bc6f5533d0584 (
plain)
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Parallax : MonoBehaviour
{
[SerializeField] private new GameObject camera;
[SerializeField] private float parallaxScale;
[SerializeField] private float PixelsPerUnit;
private float startPos, length;
private void Start()
{
startPos = transform.position.x;
length = GetComponent<SpriteRenderer>().bounds.size.x;
}
private void Update()
{
float temp = camera.transform.position.x * (1 - parallaxScale);
float dist = camera.transform.position.x * parallaxScale;
transform.position = Clamp(new Vector2(startPos + dist, transform.position.y), PixelsPerUnit);
if (temp > startPos + length / 2)
startPos += length;
else if (temp < startPos - length / 2)
startPos -= length;
}
private Vector2 Clamp(Vector2 location, float ppu)
{
return new Vector2(Mathf.CeilToInt(location.x * ppu), Mathf.CeilToInt(location.y * ppu)) / ppu;
}
}
|