Moving an Object Away from the Camera

In Unity 2D I am attempting to move the camera from one image to another using
if (Input.GetKeyDown(“space”))
transform.position = new Vector3(-15, -15, 0);

The camera starts off looking at the image, and the location transform is meant to have it change to looking at another. However, when I execute this it does not work. The investigator acknowledges that the position of the camera has changed to its new location; yet the camera is still looking at the image.

Furthermore, if I attach this function onto the image instead; the image will change its position and this will be acknowledged. But, the camera will still somehow be looking at it even though the camera has not moved either.

Regardless of what I do the camera always holds its position on the image, even though I am in play mode and everything is saying that it is working and is not looking at the image. I am assuming there is something simple that I need to toggle to stop this but I cannot find it around the interface at all.

Sounds like you have Image on UI under Canvas that has been set as Screen Space. This means that everything on Canvas will be drawn on top of everything else and camera position will have no effect at all.

I could think of three options for you:

  1. Add your Images to scene as sprites so it will be in game world and not UI.
  2. Move the position of the RectTransform of your Image on UI. Depeding on Canvas Scaler settings you might need to move a lot more than 15 units to change image.
  3. Set your Canvas Render mode to “world space” so the UI will be part of game world.