- Home /
Question by
Hein06 · Oct 07, 2019 at 10:32 PM ·
c#camerabugcamera movement
How do I stop my camera from zooming into the outer boundary? It follows the player but zooms in and out to far from the game view.
private Transform PlayerMovement;
public Transform target;
public float speed;
// Start is called before the first frame update
void Update()
{
PlayerMovement = GameObject.FindGameObjectWithTag("Player").transform;
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
float scroll = Input.GetAxis("Mouse ScrollWheel");
transform.LookAt(target);
transform.Translate(y=0, 0, scroll * speed, Space.Self);
}
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
float scroll = Input.GetAxis("Mouse ScrollWheel");
transform.LookAt(target);
transform.Translate(0, 0, scroll * speed, Space.Self);
}
}
// Update is called once per frame
void LateUpdate()
{
Vector3 TemporaryPosition = transform.position;
//make camera follow every movement such as jump, attack, and walk
TemporaryPosition.x = PlayerMovement.position.x;
TemporaryPosition.z = PlayerMovement.position.z;
transform.position = TemporaryPosition;
}
Comment
Answer by psychosaywhat · Oct 09, 2019 at 07:48 AM
I just recreated for a 3d space with a standard assets character and changed it to the Y axis to go up and down... but how about this - just put a limit check on it -
private Transform PlayerMove;
public Transform target;
public float speed;
public float Limit = 25;
// Start is called before the first frame update
void Update()
{
PlayerMove = GameObject.FindGameObjectWithTag("Player").transform;
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
float scroll = Input.GetAxis("Mouse ScrollWheel");
transform.LookAt(target);
if (transform.position.y > -Limit)
{
transform.Translate(0, scroll * speed, 0);
}
}
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
float scroll = Input.GetAxis("Mouse ScrollWheel");
transform.LookAt(target);
if (transform.position.y < Limit)
{
transform.Translate(0, scroll * speed, 0);
}
}
}
I tried this the camera zoom just stopped working altogether.
Answer by cdr9042 · Oct 09, 2019 at 08:37 AM
have you tried using Cinemachine? I haven't used it in 3D, but they have the feature to bound the camera from looking outside the boundary
https://docs.unity3d.com/Packages/com.unity.cinemachine@2.2/manual/CinemachineConfiner.html
Your answer