How To Lock Camera Y Position Unity2d
- Spaces
- Default
- Help Room
- META
- Moderators
-
- Topics
- Questions
- Users
- Badges
- Home /
1
How to lock my camera on the Y centrality?
I'm making a 3D acme-down shooter game, and I was thinking about locking my camera (Which follows the player around) on the Y axis, to prevent the photographic camera from going up when the histrion jumps for example...
I've been looking around the forum and google and I struggle to find a tutorial or merely a thread about how to practice it. I know there'due south actually enough of threads about this but most of them are well-nigh 2D or simply 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
3 Replies
· Add your respond
- Sort:
1
Reply by AurimasBlazulionis · Jan 04, 2017 at x:45 AM
Basically you take to set the Y value to whatever you want, you tin 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) role do something like transform.position = new Vector3(transform.position.x, yPosition, transform.position.z)
, where yPosition is the superlative you want to lock the camera position, you tin try to read it from the follow script.
This way it should work, simply if it does not, then you have to change script execution order to brand the script run "below the default time".
ane
Reply by Creeper_Math · Jan 04, 2017 at 08:40 PM
Although logic says information technology works, @SamElTerrible , Yous CANNOT set specific positions, as @TheDiamondPlay says is coming upwards.. Y'all tin can only directly set the transform position through full Vector3 Values... Here's the script like what @SamElTerrible had except with this thought in mind
void Update() { Vector3 setPosition = transform.position; setPosition.x = player.transform.position.x + starting time.x; setPosition.z = histrion.transform.position.z + showtime.z; transform.position = setPosition; }
This is because transform.position.x, y, or z are only {Get;}, while just transform.position is a {Get; Ready;} Meaning you can alter it through a Vector3
0
Respond 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 accept in your camera is something similar 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.ten = thespian.transform.position.x + offsetX this.transform.position.z = thespian.transform.position.z + offsetZ }
This way, the camera will follow the actor only on the X and Z axes, and you can set the Y position of your photographic camera as a divide value which is not continued to the player.
Your answer
Welcome to Unity Answers
The best identify to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, cheque out our FAQ for more than information.
Make certain to bank check out our Knowledge Base for normally asked Unity questions.
If y'all are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Follow this Question
Related Questions
Source: https://answers.unity.com/questions/1293372/how-to-lock-my-camera-on-the-y-axis.html
Posted by: wilbankssmill1985.blogspot.com
Won't work. Yous tin not edit the individual values of the position in C#.
$$anonymous$$aybe you lot can share your piece of code that doesn't seem to be working?