kiltec

ParallaxLayer.cs

Feb 26th, 2022 (edited)
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.82 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class ParallaxLayer : MonoBehaviour
  4. {
  5.     [SerializeField] [Tooltip("Leave empty to automatically grab the camera.")] private Transform _followTransform;
  6.     [Space]
  7.     [SerializeField] private Vector3 movementScale = new Vector3(2f, 1f, 1f);
  8.     [SerializeField] private Vector3 offsetPosition = new Vector3(0f, 0f, 0f);
  9.     [SerializeField] [Tooltip("Manually set the offset or check this box to work it out automatically.")] private bool autoOffset;
  10.     [Space]
  11.     [SerializeField] private bool axisXDisabled = false;
  12.     [SerializeField] private bool axisYDisabled = true;
  13.     [SerializeField] private bool axisZDisabled = true;
  14.     [Space]
  15.     [SerializeField] private bool parallaxDisabled;
  16.  
  17.     private Vector3 _originalPosition;
  18.     private Vector3 _moveToPosition;
  19.  
  20.     private float _xPosition;
  21.     private float _yPosition;
  22.     private float _zPosition;
  23.  
  24.     private void Start()
  25.     {
  26.         if (!_followTransform) { _followTransform = Camera.main.transform; }
  27.         if (autoOffset) { offsetPosition = transform.position - _followTransform.position; }
  28.         _originalPosition = transform.position;
  29.     }
  30.  
  31.     private void LateUpdate()
  32.     {
  33.         if (parallaxDisabled) { return; }
  34.  
  35.         if (axisXDisabled) { _xPosition = _originalPosition.x; }
  36.         else { _xPosition = _followTransform.position.x + offsetPosition.x; }
  37.  
  38.         if (axisYDisabled) { _yPosition = _originalPosition.y; }
  39.         else { _yPosition = _followTransform.position.y + offsetPosition.y; }
  40.  
  41.         if (axisZDisabled) { _zPosition = _originalPosition.z; }
  42.         else { _zPosition = _followTransform.position.z + offsetPosition.z; }
  43.  
  44.         _moveToPosition = Vector3.Scale(new Vector3(_xPosition, _yPosition, _zPosition), movementScale);
  45.         transform.position = _moveToPosition;
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment