I’m making a split-screen game, and need to move projectiles in the same direction the camera is facing, but the script “transform.Translate(Camera.main.transform.forward * [variable number] * Time.deltaTime);” only works for the main camera. What can I use instead of Camera.main to get the direction of a second camera?
Camera.main is just a shortcut to the object (of type Camera) whose GameObject has the MainCamera tag. You can create a public Camera reference to get any arbitrary camera object. For example:
public Camera player1cam;
public Camera player2cam;
Now, look at the script you put that into in the inspector. You will see two “slots”, into which you can drag your two cameras. You can now reference them easily:
transform.Translate(player1cam.transform.forward * number * Time.deltaTime);
More likely, assuming these are two players with split-screen views and controls, you will probably want to have a single public Camera object on your player control script, and link each instance of that script with its own respective camera.
Well, you could make a variable that holds the other camera reference. Whenever you initialize or instantiate the projectile, you call a function on its script that could setup that reference.
private GameObject otherCamera;
void Update()
{
transform.Translate(otherCamera.transform.forward * [variable number] * Time.deltaTime);
}
//Call this right after you instantiate the projectile
public void InitializeProjectile(GameObject newCamera)
{
otherCamera = newCamera;
}