Advertisement
Guest User

Untitled

a guest
Jun 1st, 2021
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.45 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Parallax : MonoBehaviour
  6. {
  7.    
  8.     private float length, startpos, camYStart;
  9.     [SerializeField]
  10.     [Tooltip("Offset to cameras y position.")]
  11.     private float yOffset;
  12.     public GameObject cam;
  13.     public float parallaxEffect;
  14.     [Tooltip(" 1 --> furthest away      0 --> very close")]
  15.     public float heightMult;
  16.    
  17.  
  18.  
  19.     // Start is called before the first frame update
  20.     void Start()
  21.     {
  22.         startpos = transform.position.x;
  23.         length = GetComponent<SpriteRenderer>().bounds.size.x;
  24.         camYStart = cam.transform.position.y;
  25.     }
  26.  
  27.     // Update is called once per frame
  28.     void FixedUpdate()
  29.     {
  30.  
  31.         float temporary = (cam.transform.position.x * (1 - parallaxEffect));
  32.         float distance = (cam.transform.position.x * parallaxEffect);
  33.         //Set Position
  34.         transform.position = new Vector3(startpos + distance, transform.position.y, transform.position.z);
  35.         //Stay in bounds
  36.         if (temporary > startpos + length) startpos += length;
  37.         else if (temporary < startpos - length) startpos -= length;
  38.     }
  39.  
  40.     private void LateUpdate()
  41.     {
  42.         //Set Y (Vertical) Position
  43.         float yDiff = camYStart + ((camYStart - cam.transform.position.y) * -1 * heightMult);
  44.         transform.position = new Vector3(transform.position.x, yDiff - yOffset, transform.position.z);
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement