Find and activate nearest camera

Hello,
I’m trying to figure out a C# script that starts on the player and maybe casts a raycast bubble to check the area for all the camera’s and get the distance from each camera to find the closest or (Shortest distance number between the player and camera’s) and activate that camera. Once’s another camera is closer then switch to that camera and switch the last one off. Having the cameras stationary. I could use some advice on how to go about creating this script.

I was also going to have the camera track the players movement so I was thinking of starting with

public Transform player;

void Start()
{
if (!player)
{
player = GameObject.FindWithTag(“Player”).transform;
}
}

void Update()
{
transform.LookAt(new Vector3(player.position.x, player.position.y, player.position.z));

}

(This tracks the player and works well)

Then I was thinking of going into a raycast to check the area for all the camera’s and get the distance from each camera to find the closest or (Shortest distance number between the player and camera’s) and activate that camera. Once’s another camera is closer then switch to that camera and switch the last one off.

I was thinking of using tags for the camera objects to separate them from other object.

So I think I should do something like this next. Attached to the character and using the capsule collider to raycast a sphere around him 10 meters.

RaycastHit hit;

Vector3 physicsCentre = this.transform.position + this.GetComponent().center;
float distanceToObstacle = 0;

if (Physics.SphereCast(physicsCentre, this.GetComponent().height / 2, transform.forward, out hit, 10))
{
distanceToObstacle = hit.distance;
}


I’m however a bit mind boggled here on how I should continue. I need to tell distance between objects with tags “Camera” and then activate and deactivate them by nearest distance.

Maybe this for distance?

public GameObject FindClosestCamera()
{

GameObject[ ] gos;
gos = GameObject.FindGameObjectsWithTag(“Camera”);
GameObject closest = null;
float distance = Mathf.Infinity;
Vector3 position = transform.position;
foreach (GameObject go in gos)
{
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
return closest;
}

Check the first forum post for code formatting hints please.

Not sure what kind of camera effect you’re looking to get, maybe something like Silent Hill?

There’s a cool article here:

1 Like

Don’t run FindGameObjectsWithTag frequently if you can avoid it. If the number of cameras in the scene is fixed you can run FindGameObjectsWithTag once and cache the results, then on periodic intervals you could iterate through the cached array and check for distance.

You could also consider just having a single camera and repositioning that camera to different points on the map, instead of enabling/disabling cameras.

1 Like

@Joe-Censored That’s actually a better idea then what I’m trying to do, didn’t think of that. Should be easier too. Thanks

@Kurt-Dekker That link helped a lot. Figured out what I’ll be doing now. Thanks for the input everyone.

1 Like