- Home /
changing camera view
my camera is the first person controller and my project is in a old village. the first person acts like a man and the camera is his eye.
i need to know if it is possible to change the view of the camera like when the man is squatting down and looking straight buy pressing a key.
what i meant is i need to change the position of the camera on Y axis when i press a key.
i have no idea where to start this coding in javascript. i hope you guys can help me.
Answer by Aubrey Hesselgren · Aug 19, 2010 at 10:37 AM
I can hopefully give you a bit of a start, but I can't tell you everything! Not sure how your system is set up.
Start by making a new input in the input manager (Edit->Project Settings->Input. Increment the "size" value by 1. Rename this "Crouch". It's important to remember the right name, later in the script. Fill in the details using something like "Jump" as a reference for the Sensitivity/Gravity etc. but make the key "shift" or "c" or whatever, rather than space).
Now, right click in your project window somewhere to make a new script (I'm going to use C# for the sake of this example). Name it "CameraCrouchOffset".
Drag this component onto the camera in your hierarchy, so that the behaviour is attached to the camera. Make sure the camera is a child of your character controller, and that its initial position is 0,0,0 relative to its parent.
Above the "start" function, add:
public float StandingEyeHeight = 0.0f;
public float CrouchingEyeHeight = -0.75f;
These will now be tweakable in the component's window, so you can get the right heights later on.
In the Update function, add something like:
//The transform.up, below, gives the object's own "up" vector, rather than world's "y" axis. This is better practice. Vector3 StandingEyeOffset = transform.up * StandingEyeHeight; Vector3 CrouchingEyeOffset = transform.up * CrouchingEyeHeight;
//The value of Input.GetAxis("Crouch") is going to be between 0 and 1. //So when it's at zero, the camera's offset will be equal to StandingEyeOffset. //When it's 1, the offset will be the same as CroucingEyeOffset. //If it's anywhere inbetween, it will be a blend of the two target vectors. transform.localposition = Vector3.Lerp(StandingEyeOffset, CrouchingEyeOffset, Input.GetAxis("Crouch") );
After you've done that, and tested that it shifts your eye up and down, you might find that it jerks between heights too much. You could make your own value to "slide" between 0 and 1, or, as a quick fix, You could go back to the input manager and tweak your "Crouch" virtual button. Change the "sensitivity" and "gravity" values to make the eye position transition smoothly. They're probably both 1000 at the start, which makes the button press effectively "instant" between 0 and 1. Try setting them both to about 5 for it to have a quick, but still noticeable transition.
1 second / 5 u/s = 200ms duration for the transition.
After that, you can tweak the heights in run time! Remember to remember your desired heights, and possibly replace the defaults at the top of the script file with them, then on the component view, right click -> reset on your CameraCrouchOffset component to reset it to the default values in the script.
I don't know java script... or... just enough to convert it to c#! Sorry!
Answer by swhite · Sep 03, 2010 at 09:17 PM
Well,
First off remember that a camera is just an object so you can transform it's position like any other object in unity. So of course you could just move the transform of the camera as the man went to kneel.
You could also have different camera's set-up that have the view you'd like to switch to ready and could transition from one camera to the other dependent on what you're in the middle of.
(so you could easily switch from First Person to a 3rd person view)
There's also some scripts that could give you some help with different angles on different cameras (as well as transitions)
Your answer
![](https://koobas.hobune.stream/wayback/20220613071618im_/https://answers.unity.com/themes/thub/images/avi.jpg)