Advertisement
LeeMace

Cam Follow 2D Side Scroll w Stop Bound

Feb 5th, 2023
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.67 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class FollowPlayer : MonoBehaviour
  6. {
  7.     private Transform player;
  8.     private Vector3 tempPos;
  9.  
  10.     [SerializeField] private float minX, maxX;
  11.  
  12.     private void Start()
  13.     {
  14.         player = GameObject.FindWithTag("Player").transform;
  15.     }
  16.  
  17.     private void LateUpdate()
  18.     {
  19.         if (!player)
  20.             return;
  21.  
  22.         tempPos = transform.position;
  23.         tempPos.x = player.position.x;
  24.         if (tempPos.x < minX)
  25.             tempPos.x = minX;
  26.  
  27.         if (tempPos.x > maxX)
  28.             tempPos.x = maxX;
  29.         transform.position = tempPos;
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement