Did you fix your bug differently?

I’m curious to know if you fixed your bug differently to how I fixed mine.

Hi Rick,

I did it a little differently, I added a WeaponZoom script to each to each weapon and then added a bool isZoomable.

If the isZoomable is false, it returns it to the standard zoom. My thought behind this is that each weapon prefab can then be identical in the way they are constructed. Also opens up the feature that you could get a scope pickup that will add a weapon to be zoomable.

See my code below, (also small difference in my code is that I only wanted it to zoom when the “Z” key is down.)

Cheers
James

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;

public class WeaponZoom : MonoBehaviour
{
[SerializeField] Camera mainCamera;
[SerializeField] FirstPersonController fpsController;
[SerializeField] int normalZoom = 44;
[SerializeField] int zoom = 15;
[SerializeField] float zoomOutSensitivity = 2f;
[SerializeField] float zoomInSensitivity = 1f;
[SerializeField] bool isZoomable = false;

}

private void Update()
{
    if (Input.GetKey(KeyCode.Z) && isZoomable)
    {
        mainCamera.fieldOfView = zoom;
        fpsController.m_MouseLook.XSensitivity = zoomInSensitivity;
        fpsController.m_MouseLook.YSensitivity = zoomInSensitivity;
    }
    else
    {
        mainCamera.fieldOfView = normalZoom;
        fpsController.m_MouseLook.XSensitivity = zoomOutSensitivity;
        fpsController.m_MouseLook.YSensitivity = zoomOutSensitivity;
    }
}

}

2 Likes

I took a different approach also. My method disables weapon swapping if you are zoomed.
The weapon zoom class now has:

[SerializeField] WeaponSwitcher switcher;
bool ZoomedInSwitch = false;

My zoom in/out methods let the weapons switcher know when we are zoomed out:

 private void ZoomOut()
    {
        ZoomedInSwitch = false;
        switcher.SetIsZoomed(false);
        ToggleWeaponVisibility(true);
        fpsCamera.fieldOfView = zoomOutFOV;
        fpsController.mouseLook.XSensitivity = zoomOutSensitivity;
        fpsController.mouseLook.YSensitivity = zoomOutSensitivity;
    }

The weapon switcher class has a setter for this now:

    public void SetIsZoomed(bool zoomed)
    {
        isZoomed = zoomed;
    }

Finally, the weapon switcher class checks to see if it is zoomed in before allowing swapping:

 void Update()
    {
        if (isZoomed == false)
        {
            int previousWeapon = currentWeapon;
            ProcessKeyInput();
            ProcessScrollWheel();

            if (previousWeapon != currentWeapon)
            {
                SetActiveWeapon();
            }
        }
    }

I’m also toggling the visibility of the weapon when zoomed in because the barrel bothered me.

4 Likes

Yeah I disabled weapon switching if zoomed in, I was thinking about approaching it the way you tackled the problem but I have a Animation putting the iron sights up with an animation event. So that solution didnt end up working for me ^^.

3 Likes

This is a great solution!

Instead of adding weapon zoom script to individual pistol, shotgun, carbine I added it to weapons GameObject, which in turn makes it independent of which weapon is active.

1 Like

My version is doing the most after seeing Rick’s example but here it is.
I created a variable that in WeaponSwitcher of type WeaponZoom
I searched for the script in the Start function

[SerializeField] int currentWeapon = 0;
bool isDead = false;
WeaponZoom isZoomed;

void Start()
{
    isZoomed = FindObjectOfType<WeaponZoom>();
    SetWeaponActive();
}

Then I called the ZoomOut() function from WeaponZoom whenever the player uses the scroll wheel or Alpha1,2,3 Keys. I Also set the WeaponZoom zoomIsToggled variable to false to fix another bug I created by doing it this way. The bug would force the player to have to click twice next time they wanted to zoom on the weapon with the WeaponZoom script.

private void ProccessKeyInput()
{
    if (Input.GetKeyDown(KeyCode.Alpha1))
    {
        isZoomed.GetComponent<WeaponZoom>().ZoomOut();
        isZoomed.zoomedInToggle = false;
        currentWeapon = 0;
    }if (Input.GetKeyDown(KeyCode.Alpha2))
    {
        isZoomed.GetComponent<WeaponZoom>().ZoomOut();
        isZoomed.zoomedInToggle = false;
        currentWeapon = 1;
    }if (Input.GetKeyDown(KeyCode.Alpha3))
    {
        isZoomed.GetComponent<WeaponZoom>().ZoomOut();
        isZoomed.zoomedInToggle = false;
        currentWeapon = 2;
    }
    
}
public void IsDead()
{
    isDead = true;
}

private void ProcessScrollWheel()
{
    if (isDead)
    {
        print("Your dead can't change weapon");
    }
    else
    {     
     if (Input.GetAxis("Mouse ScrollWheel") < 0)
      {
            isZoomed.GetComponent<WeaponZoom>().ZoomOut();
            isZoomed.zoomedInToggle = false;

            if (currentWeapon >= transform.childCount - 1)
        {
            currentWeapon = 0;
        }
        else
        {
                currentWeapon++;
        }
    }  
    if (Input.GetAxis("Mouse ScrollWheel") > 0)
    {
            isZoomed.GetComponent<WeaponZoom>().ZoomOut();
            isZoomed.zoomedInToggle = false;

            if (currentWeapon <= 0)
        {
              
                currentWeapon = transform.childCount - 1;
        }
        else
        {
                currentWeapon--;
        }
     } 
   }
}

Thank you for sharing your solutions!
Like some of the folks above, I also put the WeaponZoom script on each weapon, which allows for different FOVs.
I only allow weapon switching when fully zoomed out, which I find more realistic.
Instead of Zoom animation, I am using linear interpolation, which has a slightly different speed for each weapon.

1 Like

I raised an event when the weapon was switched and added the ZoomOut() method as a listener

I also went with “don’t allow switching while zoomed” first. This could actually be a difficulty setting for the game.
I also like the idea of every weapon having possibly the ability to be zoomed and this depending on having a scope attached which could be an upgrade you can get.

Optionally having different zoom levels sounds like an interesting extra feature, as well… :slight_smile:

One thing I noted is that when you jump out of the zoomed carbine, next time you switch to it it takes two attempts for zooming. What’s missing is setitng the “isZoomed” flag to false, as well…

hi instructor
i have gone with a different approach for the main zoom functionality
I wanted to smooth out the zoom that’s why i have use a coroutine with mathf.lerp but when I try to zoom out in onDisable it says that we couldn’t start the coroutine when the object is inactive
I just wanted to know is there any other way we can make that coroutine work in onDisable
here’s my weaponZoom script

if i use this code it dosen’t work

private void OnDisable()
    {
        if (zoomedIntoggle)
        {
            zoomedIntoggle = false;
            ToggleZoom();
        }
    }

here’s the actual code that I made it to work

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;
public class weaponZoom : MonoBehaviour
{
    [Header("ZOOMFOV SETTINGS")]
    [SerializeField] Camera fpsCamera;
    [SerializeField] float zoomInFov;
    [SerializeField] float zoomOutFov;
    [SerializeField][Range(0f,.5f)] float SmootingFactor;
    [SerializeField] [Range(0f, .01f)] float timeSmoothingGap;
    
    
    [Header("ZOOM MOUSE SENSITIVITY SETTINGS")]
    [SerializeField] RigidbodyFirstPersonController fpsController;
    [SerializeField] Vector2 ZoomedInSensitivity;
    [SerializeField] Vector2 ZoomedOutSensitivity;
    //s Start is called before the first frame update
    bool zoomedIntoggle = false;

    private void OnDisable()
    {
        if (zoomedIntoggle)
        {
            zoomedIntoggle = false;
            fpsController.mouseLook.XSensitivity = ZoomedOutSensitivity.x;
            fpsController.mouseLook.YSensitivity = ZoomedOutSensitivity.y;
            fpsCamera.fieldOfView = zoomOutFov;
        }
    }


    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
            ToggleZoom();
        }

    }

    private void ToggleZoom()
    {
        if (!zoomedIntoggle)
        {
            zoomedIntoggle = true;
            fpsController.mouseLook.XSensitivity = ZoomedInSensitivity.x;
            fpsController.mouseLook.YSensitivity = ZoomedInSensitivity.y;
            StartCoroutine(ChangeZoom(zoomOutFov, zoomInFov));

        }
        else
        {
            zoomedIntoggle = false;
            fpsController.mouseLook.XSensitivity = ZoomedOutSensitivity.x;
            fpsController.mouseLook.YSensitivity = ZoomedOutSensitivity.y;
            StartCoroutine(ChangeZoom(zoomInFov, zoomOutFov));

        }
    }

    IEnumerator ChangeZoom(float a, float b)
    {
        float t = 0;
        while (t < 1)
        {
            fpsCamera.fieldOfView = Mathf.Lerp(a, b, t);
            t += SmootingFactor;
            yield return new WaitForSeconds(timeSmoothingGap);
        }
    }
}



thanks for your response

HI Rick

I honestly didn’t think of any solutions because that style of coding was advanced for me, so it will take a bit more practise so I can have ideas of how to resolve them.

Also I haven’t done any of your other sections yet in that course because I’m atm focusing on making mechanics for my own fps shooter game which is why I’ve started on zombie runner first rather than the other sections, so after I complete zombie runner I’m going to look at the other sections so I can learn the advanced stuff better

Privacy & Terms