Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class Camerafollow : MonoBehaviour
- {
- public Transform target;
- public Transform leftBounds;
- public Transform rightBounds;
- public float smoothDampTime = 0.15f;
- private Vector3 smoothDampVelocity = Vector3.zero;
- private float camWidth;
- private float camHeight;
- private float levelMinX;
- private float levelMaxX;
- // Start is called before the first frame update
- void Start()
- {
- camHeight = Camera.main.orthographicSize * 2;
- camWidth = camHeight * Camera.main.aspect;
- float leftBoundswidth = leftBounds.GetComponentInChildren<SpriteRenderer>().bounds.size.x / 2;
- float rightBoundsWidth = rightBounds.GetComponentInChildren<SpriteRenderer>().bounds.size.x / 2;
- levelMinX = leftBounds.position.x + leftBoundswidth + (camWidth / 2);
- levelMaxX = rightBounds.position.x - rightBoundsWidth - (camWidth / 2);
- }
- // Update is called once per frame
- void Update()
- {
- if (target)
- float targetX = Mathf.Max(levelMinX, Mathf.Min(levelMaxX, target.position.x));
- float x = Mathf.SmoothDamp(transform.position.x, targetX, ref smoothDampVelocity, smoothDampTime);
- transform.position = new Vector3(x, transform.position.y, transform.position.z);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement