Unity: How to move the Camera by a certain amount?
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!
Answer by lgarczyn · Dec 28, 2019 at 07:57 PM
Either use transform.Translate(0, -10, 0)
or transform.position += new Vector3(0, -10,0 );
The reason you can't directly modify the 'x' of the position vector is a bit complicated, but basically when you get the transform.position member variable, you're actually calling a function, and getting a local variable. When you modify that variable, you're only modifying the local version, and unity warns you that it's not doing anything.
Your answer
Follow this Question
Related Questions
Camera yMin postion should be restricted to player postion 1 Answer
How to move the camera in VR mode? 2 Answers
Let the Camera Face the middle of the Map while still following the Player? 1 Answer
Camera Follow Script destroys Raycast on VR-Hands with Late Update 1 Answer
How to use the results of a dice roll? 2 Answers