- Home /
Changing camera position to shoot through scope on gun
Right now I have 2 Cameras: the main camera displays the gun at its normal state and a second camera is attached to the gun (the gun is a child of the main camera) and when toggled it looks through the scope of the gun and increases the field of view.
Heres a visual for a better understanding:
Now if I were to just toggle the second camera on and turn the main camera off, this would work splendid, but it's not very ideal. You should only have 1 camera per scene.
So I want to Lerp the position of the camera to look through the scope and manually decrease the fieldofview. So I have written the following script:
public Camera ZoomedCamera;
private Transform MainCameraTransform;
private Transform ZoomedTransform;
private bool zoomed = false;
void Start () {
MainCameraTransform = Camera.main.transform;
ZoomedTransform = ZoomedCamera.transform;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.LeftShift))
{
zoomed = !zoomed;
}
if (zoomed) {
MainCameraTransform.position = Vector3.Lerp (MainCameraTransform.position, ZoomedTransform.position, 5f * Time.deltaTime);
MainCameraTransform.Rotate(ZoomedCamera.transform.rotation.eulerAngles);
}
}
The problem with this is that it doesn't work: when I hit the zoom button, the camera speeds through the scene at the speed of light and it's hard to tell exactly what is going on.
Could anyone give me some insight as to what Im doing wrong? I think it is something to do with the parent-child relationship, but even when I've tried using static values, I cannot seem to replicate the correct solution.
Answer by Pharan · Oct 10, 2015 at 06:35 AM
(1) All the Lerp samples in the docs are misleading. Look up linear interpolation anywhere else on the internet. This is a slightly better description: https://msdn.microsoft.com/en-us/library/microsoft.xna.framework.mathhelper.lerp.aspx?f=255&MSPPError=-2147217396
The third parameter of Lerp is not time. It's a value between 0 and 1 describing the weight of the second parameter.
0 means the resulting value will be equal to the value of your first argument, and 1 means it will be your second argument.
(2) transform.Rotate means it'll rotate it by a certain amount. And the way you set it up, it'll rotate infinitely, and really fast if the value it's getting is even moderate. If you want to set the rotation, use transform.rotation = yourNewRotation.
In this case, do read the docs. In fact, read all the stuff under Transform. It'll pay off in the long run. And read all of Vector3's docs too while you're at it.
(3) The other problem seems to be that there's no logic that returns your zoom level back to normal.
Thanks, the reason I did rotate is because It wasnt letting me set the rotation, but Il try again :P Good advice, and I never set the zoom so that part isnt there yet ;)
Your answer
Follow this Question
Related Questions
Clamping a 2D camera while zooming. 0 Answers
Help with Lerp 0 Answers
Camera zoom smoothing 1 Answer
Simple camera zoom function 1 Answer