Question by
jamiegreeny · Jun 26, 2020 at 03:01 PM ·
boundscamera follow
Camera Follow Problem
For some reason, whenever I assign bounds to my camera or player using Mathf Clamp, they just fly between the two y values. Can anybody help?
Here is my code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class topdowncamera : MonoBehaviour { private Vector2 velocity;
public float smoothTimeY;
public float smoothTimeX;
public GameObject player;
public bool bounds;
public Vector3 mincamerapos;
public Vector3 maxcamerapos;
private void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
}
private void FixedUpdate()
{
float posX = Mathf.SmoothDamp(transform.position.x, player.transform.position.x, ref velocity.x, smoothTimeX);
float posY = Mathf.SmoothDamp(transform.position.y, player.transform.position.y, ref velocity.y, smoothTimeY);
transform.position = new Vector3(posX, posY, transform.position.z);
if (bounds)
{
transform.position = new Vector3(Mathf.Clamp(transform.position.x, mincamerapos.x, maxcamerapos.x),
Mathf.Clamp(transform.position.y, mincamerapos.y, maxcamerapos.y),
Mathf.Clamp(transform.position.z, mincamerapos.z, maxcamerapos.z));
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Keep Top Down 2D camera in bounds of background sprite 3 Answers
Unity 2D camera Bounds for player 0 Answers
Encapsulated bounds center error 1 Answer
How to set Camera Limits in Unity that would stay accurate for different screen ratios? 2 Answers
Camera rig - tanks tutorial - local and world space? 0 Answers