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.