Quantcast
Channel: Answers for "Keep Top Down 2D camera in bounds of background sprite"
Viewing all articles
Browse latest Browse all 6

Answer by Paikz

$
0
0
Solved it after the answers I've been given + some googling. Here's the script I came up with: private float rightBound; private float leftBound; private float topBound; private float bottomBound; private Vector3 pos; private Transform target; private SpriteRenderer spriteBounds; // Use this for initialization void Start () { float vertExtent = Camera.main.camera.orthographicSize; float horzExtent = vertExtent * Screen.width / Screen.height; spriteBounds = GameObject.Find("1 - Background").GetComponentInChildren(); target = GameObject.FindWithTag("Player").transform; leftBound = (float)(horzExtent - spriteBounds.sprite.bounds.size.x / 2.0f); rightBound = (float)(spriteBounds.sprite.bounds.size.x / 2.0f - horzExtent); bottomBound = (float)(vertExtent - spriteBounds.sprite.bounds.size.y / 2.0f); topBound = (float)(spriteBounds.sprite.bounds.size.y / 2.0f - vertExtent); } // Update is called once per frame void Update () { //Debug.Log(); var pos = new Vector3(target.position.x, target.position.y, transform.position.z); pos.x = Mathf.Clamp(pos.x, leftBound, rightBound); pos.y = Mathf.Clamp(pos.y, bottomBound, topBound); transform.position = pos; } void OnLevelWasLoaded() { Start(); } Helpfull link: http://answers.unity3d.com/questions/501893/calculating-2d-camera-bounds.html Thanks for the help guys (Joshpsawyer & robertbu)! I'm glad you took the time to help me out. :)

Viewing all articles
Browse latest Browse all 6

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>