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:
![alt text][1]
![alt text][2]
[1]: /storage/temp/55851-screen-shot-2015-10-09-at-120129-pm.jpg
[2]: /storage/temp/55852-screen-shot-2015-10-09-at-120155-pm.jpg
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.
↧