Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class Camerafollow : MonoBehaviour
  4. {
  5. public Transform target;
  6. public Transform leftBounds;
  7. public Transform rightBounds;
  8.  
  9. public float smoothDampTime = 0.15f;
  10.  
  11. private Vector3 smoothDampVelocity = Vector3.zero;
  12. private float camWidth;
  13. private float camHeight;
  14. private float levelMinX;
  15. private float levelMaxX;
  16.  
  17. // Start is called before the first frame update
  18. void Start()
  19. {
  20. camHeight = Camera.main.orthographicSize * 2;
  21. camWidth = camHeight * Camera.main.aspect;
  22.  
  23. float leftBoundswidth = leftBounds.GetComponentInChildren<SpriteRenderer>().bounds.size.x / 2;
  24. float rightBoundsWidth = rightBounds.GetComponentInChildren<SpriteRenderer>().bounds.size.x / 2;
  25.  
  26. levelMinX = leftBounds.position.x + leftBoundswidth + (camWidth / 2);
  27. levelMaxX = rightBounds.position.x - rightBoundsWidth - (camWidth / 2);
  28. }
  29.  
  30. // Update is called once per frame
  31. void Update()
  32. {
  33. if (target)
  34. float targetX = Mathf.Max(levelMinX, Mathf.Min(levelMaxX, target.position.x));
  35. float x = Mathf.SmoothDamp(transform.position.x, targetX, ref smoothDampVelocity, smoothDampTime);
  36.  
  37. transform.position = new Vector3(x, transform.position.y, transform.position.z);
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement