- Home /
How to lock my camera on the Y axis?
I'm making a 3D top-down shooter game, and I was thinking about locking my camera (Which follows the player around) on the Y axis, to prevent the camera from going up when the player jumps for example...
I've been looking around the forum and google and I struggle to find a tutorial or just a thread about how to do it. I know there's actually plenty of threads about this but most of them are about 2D or just unanswered.
If you could find me a tutorial, or suggest a script, that would be awesome. Thanks!
P.S I'd rather write code in C#, thank you :P
Answer by AurimasBlazulionis · Jan 04, 2017 at 10:45 AM
Basically you have to set the Y value to whatever you want, you can read it to a variable in Awake method. Now, in LateUpdate (not Update, since we need to run this script after the follow script) function do something like transform.position = new Vector3(transform.position.x, yPosition, transform.position.z)
, where yPosition is the height you want to lock the camera position, you can try to read it from the follow script.
This way it should work, but if it does not, then you have to change script execution order to make the script run "below the default time".
Answer by Creeper_Math · Jan 04, 2017 at 08:40 PM
Although logic says it works, @SamElTerrible , You CANNOT set specific positions, as @TheDiamondPlay says is coming up.. You can only directly set the transform position through full Vector3 Values... Here's the script like what @SamElTerrible had except with this idea in mind
void Update() {
Vector3 setPosition = transform.position;
setPosition.x = player.transform.position.x + offset.x;
setPosition.z = player.transform.position.z + offset.z;
transform.position = setPosition;
}
This is because transform.position.x, y, or z are only {Get;}, while just transform.position is a {Get; Set;} Meaning you can change it through a Vector3
Answer by SamElTerrible · Jan 04, 2017 at 10:53 AM
If you're using a script on the camera to follow the player around, you could do the following:
I assume what you currently have in your camera is something like this.
void Update(){
this.transform.position = player.transform.position + new Vector3(offsetX, offsetY, offsetZ);
}
What you could do instead is:
void Update(){
this.transform.position.x = player.transform.position.x + offsetX
this.transform.position.z = player.transform.position.z + offsetZ
}
This way, the camera will follow the player only on the X and Z axes, and you can set the Y position of your camera as a separate value which is not connected to the player.
Won't work. You can not edit the individual values of the position in C#.
$$anonymous$$aybe you can share your piece of code that doesn't seem to be working?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do i Achieve mesh Extrusion? 2 Answers
Can I make a collider ignore only one other collider? 0 Answers
Echolocation in 2D top down game 0 Answers