I think I’m just having a hard time understanding the 3d space and how objects and camera relate to it. I’ve went through a number of starting tutorials and yet I still find myself stumbling around trying to get the camera focused on my objects and the point light to show light where I want the light.
I’ve learned the hard way that if the camera can’t see it it is out of the game view. I was trying to get both views to match because that is how all the tutorials I watched did it but I’m having some trouble getting things right.
Anyone else have trouble with these starting out? If so, maybe you can offer me some advice on what you did to overcome it.
Have you tried Transform.LookAt? This will work for both the camera and the light
Assuming your objects move only on the X/Y plane):
// For 2D sidescrollers
protected float smoothing = 0.1f;
public void FocusCameraOnTarget2D (Camera cam, GameObject target)
{
Vector3 newCameraPosition = cam.transform.position;
newCameraPosition.x = target.transform.position.x;
newCameraPosition.y = target.transform.position.y;
// We skip the z-position because we want to keep it in the same position
// We use a lerp to avoid "jitter" in camera movements
cam.transform.position = Vector3.Lerp (cam.transform.position, newCameraPosition, smoothing);
}
For 3D space this can be a little tricker. Suppose you want a “security camera” that stays in one place and follows the player. Something like this might work:
public void PointToTarget (Camera cam, GameObject target)
{
Vector3 relativeAngle = (target.transform.position - cam.transform.position).normalized;
cam.transform.rotation = Quaternion.LookRotation (relativeAngle);
}
Anything else (follow camera, rotational cameras, track cams) are more complex and beyond the scope of this. Google is your friend.