[2D - Top Down] Camera track / Border control
Hey, I'm having a bit of an issue with my camera script. The aim is so that it will gently track the player up until it hits the background boundary. I can get it to track the player statically and stop at the walls, I can slowly track the player, but I can't do both.
Here's what I'm using now:
using UnityEngine;
using System.Collections;
public class Border : MonoBehaviour
{
private float rightBound;
private float leftBound;
private float topBound;
private float bottomBound;
private Vector3 pos;
private Transform target;
private SpriteRenderer spriteBounds;
public float dampTime = 0.15f;
private Vector3 velocity = Vector3.zero;
// Use this for initialization
void Start()
{
float vertExtent = Camera.main.orthographicSize;
float horzExtent = vertExtent * Screen.width / Screen.height;
spriteBounds = GameObject.Find("BG").GetComponentInChildren<SpriteRenderer>();
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);
}
void FixedUpdate()
{
Vector3 point = Camera.main.WorldToViewportPoint(target.position);
Vector3 delta = target.position - Camera.main.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, point.z));
Vector3 destination = transform.position + delta;
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;
Debug.Log(pos);
transform.position = Vector3.SmoothDamp(pos, destination, ref velocity, dampTime);
}
}
If anyone could shed some light as to why this is happening I'd be very grateful. Thanks!
Answer by cjdev · Sep 07, 2015 at 09:49 PM
You're Clamping the position before the SmoothDamp. Try doing it to the transform's position after the SmoothDamp.
Just gave it a try and I'm getting the same issue, I'm clamping the target positions so it's ignoring the current transform. I think my general theory on how this was supposed to work is wrong, I'm certain there's a better way of doing it, I can't seem to find one though.
You could try clamping the destination vector ins$$anonymous$$d of the current position.
Sorry just came back home! Tried clamping the destination and works perfectly, albeit with a bit of jitteriness but I can work on that. Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220612032754im_/https://answers.unity.com/themes/thub/images/avi.jpg)