forked from nhathuy7996/ReUseScript_Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BGLoop.cs
32 lines (28 loc) · 977 Bytes
/
BGLoop.cs
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BGLoop : MonoBehaviour
{
Camera _mainCam;
SpriteRenderer _spriteRenderer;
[SerializeField]
float _widthImage;
// Start is called before the first frame update
void Start()
{
_mainCam = Camera.main;
_spriteRenderer = this.GetComponent<SpriteRenderer>();
Texture image = _spriteRenderer.sprite.texture;
_widthImage = image.width / _spriteRenderer.sprite.pixelsPerUnit;
}
// Update is called once per frame
void Update()
{
if (Mathf.Abs(_mainCam.transform.position.x - this.transform.position.x) > _widthImage)
{
float offset = Mathf.Abs(_mainCam.transform.position.x - this.transform.position.x) - _widthImage ;
this.transform.position = new Vector2(_mainCam.transform.position.x +offset , this.transform.position.y);
Debug.Log(offset);
}
}
}