EndlessRocket/Assets/Free 2D Cartoon Parallax Background/Demo/Script/ParallaxBackground_0.cs
2023-07-19 00:28:23 +05:30

53 lines
2.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ParallaxBackground_0 : MonoBehaviour
{
[Header("Layer Setting")]
public float[] Layer_Speed = new float[7];
public GameObject[] Layer_Objects = new GameObject[7];
private Transform _camera;
// private float[] startPos = new float[7];
private float boundSizeX;
private float sizeX;
private GameObject Layer_0;
public float SwitchPos = 40;
public float SpeedMultiplier = -1;
float startX;
void Start()
{
_camera = Camera.main.transform;
sizeX = Layer_Objects[0].transform.localScale.x;
boundSizeX = Layer_Objects[0].GetComponent<SpriteRenderer>().sprite.bounds.size.x;
startX = lastCamX = _camera.position.x;
// for (int i=0;i<5;i++){
// startPos[i] = _camera.position.x;
// }
}
float lastCamX;
void Update(){
float camDif = _camera.position.x - lastCamX;
// Debug.Log(camDif);
lastCamX = _camera.position.x;
for (int i=0;i<5;i++){
// float temp = (_camera.position.x * (1-Layer_Speed[i]) );
// float distance = _camera.position.x * Layer_Speed[i] ;
Layer_Objects[i].transform.position += new Vector3(Layer_Speed[i] * camDif * SpeedMultiplier,0);
if(Layer_Objects[i].transform.localPosition.x >= SwitchPos || Layer_Objects[i].transform.localPosition.x <= -SwitchPos){
Layer_Objects[i].transform.localPosition = new Vector3(0,0,10);
}
// Layer_Objects[i].transform.position = new Vector2 (startPos[i] + distance, _camera.position.y);
// if (temp > startPos[i] + boundSizeX*sizeX){
// startPos[i] += boundSizeX*sizeX;
// }else if(temp < startPos[i] - boundSizeX*sizeX){
// startPos[i] -= boundSizeX*sizeX;
// }
}
}
}