Reputation: 105
Hello! I am trying to make a dungeon game, sort of like The Binding of Isaac. I am trying to make a script that will move the camera down my a specific amount when the player goes into another room. Right now, I am focusing on the part of the script that will move the camera down when the player enters a room from the top.
After looking at many other StackOverflow and questions on the Unity site, the line of code I made that moves the camera down enter a room from the top is Camera.main.transform.position = new Vector3(0, -10, 0);
. After this line executes, the camera no longer renders anything except for the set background color. I am also concerned about this line of code because I think that it will not move the camera down by 10 points, but will set it to the position 0, -10, 0
, but I'm not sure.
I've also tried getting the individual points from the camera ( Camera.main.transform.position.x += 10;
for example), but Visual Studio and Unity gives me the error: Unable to get the return values of position.x because they are not variables
. I have been trying to figure this out for a very long time and can't for some reason. Thanks!
Upvotes: 0
Views: 4306
Reputation: 70
I am also concerned about this line of code because I think that it will not move the camera down by 10 points, but will set it to the position 0, -10, 0, but I'm not sure.
Yes, that will indeed set the position to 0, -10, 0, because you are setting the position to a new Vector3 with no reference to the old position.
You should instead use Camera.main.transform.Translate(0, -10, 0)
, which will move the Camera down 10 points on the y-axis relative to its current position.
EDIT: The camera will render just the background colour because (I assume) there are no objects in the camera's field of view once its position has been set to (0, -10, 0), therefore only the background is visible.
Upvotes: 1