Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Parallax : MonoBehaviour
- {
- private float length, startpos, camYStart;
- [SerializeField]
- [Tooltip("Offset to cameras y position.")]
- private float yOffset;
- public GameObject cam;
- public float parallaxEffect;
- [Tooltip(" 1 --> furthest away 0 --> very close")]
- public float heightMult;
- // Start is called before the first frame update
- void Start()
- {
- startpos = transform.position.x;
- length = GetComponent<SpriteRenderer>().bounds.size.x;
- camYStart = cam.transform.position.y;
- }
- // Update is called once per frame
- void FixedUpdate()
- {
- float temporary = (cam.transform.position.x * (1 - parallaxEffect));
- float distance = (cam.transform.position.x * parallaxEffect);
- //Set Position
- transform.position = new Vector3(startpos + distance, transform.position.y, transform.position.z);
- //Stay in bounds
- if (temporary > startpos + length) startpos += length;
- else if (temporary < startpos - length) startpos -= length;
- }
- private void LateUpdate()
- {
- //Set Y (Vertical) Position
- float yDiff = camYStart + ((camYStart - cam.transform.position.y) * -1 * heightMult);
- transform.position = new Vector3(transform.position.x, yDiff - yOffset, transform.position.z);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement