Search code examples
unity-game-enginecamerazoominglerp

Unity - Looking through the scope of a 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:

enter image description here

enter image description here

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:

[RequireComponent(typeof(Camera))]
public class Zoom : MonoBehaviour {

    private Transform CameraTransform = null;
    public Transform ZoomedTransform;

    private bool zoomed = false;

    void Start () {
        CameraTransform = Camera.main.transform;
    }

    // Update is called once per frame
    void Update () {
        if (Input.GetKey (KeyCode.LeftShift)) 
        {
            CameraTransform.position = Vector3.Lerp (
                CameraTransform.position, 
                CameraTransform.position + ZoomedTransform.position, 
                5f * Time.deltaTime
            );
            CameraTransform.Rotate(ZoomedTransform.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 I'm 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.

Hierarchy:

enter image description here


Solution

  • (This answer operates under the assumption that ZoomedTransform is a relative transformation, and not the absolute position of the camera as suspected by 31eee384's answer.)

    I think there are a couple issues with your code. I'll tackle them individually so they're easier to understand, but they both relate to the following line:

    CameraTransform.position = Vector3.Lerp (CameraTransform.position, CameraTransform.position + ZoomedTransform.position, 5f * Time.deltaTime);
    

    First, let's look at how you're using Vector3.Lerp(). For the third argument of Vector3.Lerp(), you're supplying 5f * Time.deltaTime. What exactly does this value work out to? Well, the standard framerate is about 60 FPS, so Time.deltaTime = ~1/60. Hence, 5f * Time.deltaTime = 5/60 = ~0.0833.

    What is Vector3.Lerp() expecting for the third argument, though? According to the documentation, that third argument should be between 0 and 1, and determines whether the returned Vector3 should be closer to the first or second given Vector3. So yes, 5f * Time.deltaTime falls within this range, but no interpolation will occur - because it will always be around ~0.0833, rather than progressing from 0 to 1 (or 1 to 0). Each frame, you're basically always getting back cameraPos + zoomTransform * 0.0833.

    The other notable problem is how you're updating the value of CameraTransform.position every frame, but then using that new (increased) value as an argument for Vector3.Lerp() the next frame. (This is a bit like doing int i = i + 1; in a loop.) This is the reason why your camera is flying across the map so fast. Here is what is happening each frame, using the hypothetical result of your Vector3.Lerp() that I calculated earlier (pseudocode):

    // Frame 1
    cameraPosFrame_1 = cameraPosFrame_0 + zoomTransform * 0.0833;
    // Frame 2
    cameraPosFrame_2 = cameraPosFrame_1 + zoomTransform * 0.0833;
    // Frame 3
    cameraPosFrame_3 = cameraPosFrame_2 + zoomTransform * 0.0833;
    // etc...
    

    Every frame, zoomTransform * 0.0833 gets added to the camera's position. Which ends up being a really, really fast, and non-stop increase in value - so your camera flies across the map.

    One way to address these problems is to have variables that stores your camera's initial local position, zoom progress, and speed of zoom. This way, we never lose the original position of the camera, and we can both keep track of how far the zoom has progressed and when to stop it.

    [RequireComponent(typeof(Camera))]
    public class Zoom : MonoBehaviour {
    
        private Transform CameraTransform = null;
        public Transform ZoomedTransform;
        private Vector3 startLocalPos;
        private float zoomProgress = 0;
        private float zoomLength = 2; // Number of seconds zoom will take
    
        private bool zoomed = false;
    
        void Start () {
            CameraTransform = Camera.main.transform;
            startLocalPos = CameraTransform.localPosition;
        }
    
        // Update is called once per frame
        void Update () {
            if (Input.GetKey (KeyCode.LeftShift)) 
            {
                zoomProgress += Time.deltaTime;
    
                CameraTransform.localPosition = Vector3.Lerp (startLocalPos, startLocalPos + ZoomedTransform.position, zoomProgress / zoomLength);
                CameraTransform.Rotate(ZoomedTransform.rotation.eulerAngles);
            }
        }
    }
    

    Hope this helps! Let me know if you have any questions. This answer does ramble a little, so I hope you don't have any trouble getting the important points from it.