i have a script that moves the camera a set amount when the player exceeds a certain distance from said camera in a given direction
i use an event to indicate when the camera moves to the next screen
however, even if the camera doesn’t move, the functions still are called constantly
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class CameraController : MonoBehaviour
{
GameObject player;
Vector2 playerRelative;
public float northBoundary, southBoundary, eastBoundary, westBoundary;
public Vector2 cameraMoveAmount;
public static event Action OnCameraMove;
void Start()
{
if(GameObject.FindGameObjectWithTag("player")) player = GameObject.FindGameObjectWithTag("player");
}
void Update()
{
if(!player)
{
if (GameObject.FindGameObjectWithTag("player")) player = GameObject.FindGameObjectWithTag("payer");
}
if(player)
{
playerRelative = - transform.position + player.transform.position;
if(playerRelative.x < westBoundary)
{
Debug.Log(playerRelative);
transform.position -= new Vector3(cameraMoveAmount.x, 0, 0);
//ScreenFlip();
}
if (playerRelative.x > eastBoundary)
{
Debug.Log(playerRelative);
transform.position += new Vector3(cameraMoveAmount.x, 0, 0);
//ScreenFlip();
}
if (playerRelative.y < southBoundary)
{
Debug.Log(playerRelative);
transform.position -= new Vector3(0, cameraMoveAmount.y, 0);
//ScreenFlip();
}
if (playerRelative.y > northBoundary)
{
Debug.Log(playerRelative);
transform.position += new Vector3(0, cameraMoveAmount.y, 0);
//ScreenFlip();
}
}
}
void ScreenFlip()
{
//OnCameraMove.Invoke();
Debug.Log("screen flip invoked");
}
}
as u can probably see, even tho the parameters within the if loops are not met, the enclosed functions still get called every cycle.
i am a very very very amateur programmer, and also a bit of a dumbass, so im sorry if this question is worded poorly or has an obvious and simple answer
thank u