I´m not surte how to approach line 16.
The idea is that it search the active camera form the list, finds in what position is on the list, creates a int from that number and changes the number of ID.
public int ID;
public int IDActiveCamera;
public GameObject[] idcameras;
public GameObject ActiveCamera;
public void FindActiveCamera()
{
List<GameObject> ActiveCamera= new List<GameObject>();
foreach (GameObject camera in idcameras)
{
if (camara.activeInHierarchy)
{
ActiveCamera.Add(camera);
}
}
----> IDActiveCamera = idcameras.FindIndex(CamaraActiva.Count);
this.ActiveCamera = ActiveCamera[IDActiveCamera];
ID = IDActiveCamera;
}
Is it generating an error? idcameras is an Array and FindIndex is a List method.
More importantly (I think) you need to decide what makes the camera the active one because we can’t really tell here. It shouldn’t be necessary to generate a List of GameObjects for this. Maybe but I am skeptical of the methodology used here.
As @tleylan hints, you have errors, such as camara
within the for() loop that iterates with camera
, and if you’re not seeing that error, then you have another field somewhere with one or the other, and more importantly, whatever you think should be happening is almost certainly NOT what is happening based on the code above.
Not only that but you have ActiveCamera
as both a single GameObject and a list of them. I don’t even know what is going on here…
Slow down and get it right… and while you’re at it…
Camera stuff is pretty tricky… I hear all the Kool Kids are using Cinemachine from the Unity Package Manager.
There’s even a dedicated Camera / Cinemachine area: see left panel.
If you insist on making your own camera controller, do not fiddle with camera rotation.
The simplest way to do it is to think in terms of two Vector3 points in space:
- where the camera is LOCATED
- what the camera is LOOKING at
private Vector3 WhereMyCameraIsLocated;
private Vector3 WhatMyCameraIsLookingAt;
void LateUpdate()
{
cam.transform.position = WhereMyCameraIsLocated;
cam.transform.LookAt( WhatMyCameraIsLookingAt);
}
Then you just need to update the above two points based on your GameObjects, no need to fiddle with rotations. As long as you move those positions smoothly, the camera will be nice and smooth as well, both positionally and rotationally.