How To Aim Down Sight - Unity FPS

Поділитися
Вставка
  • Опубліковано 3 лип 2024
  • I've been doing a lot of fiddling with this little FPS in Unity and I got to a point where I needed to figure out how to aim down sight - so again I consulted the implementation of the Unity team in their FPS Microgame (learn.unity.com/project/fps-t.... And learnt about creating an entire weapons systems - And whilst I went ahead and implemented the whole thing, that might not be for everyone so I extracted the bit about aiming down sight and I thought I'd share it with you guys.
    This video is a bit more high level and explains things at a more conceptual level - I figure if you're searching for how to aim down sight in a Unity FPS game, then you have already gotten relatively far into the process of actually writing the game. So the amount of code shown is minimal, cause really to aim down sight in a Unity game does not require that much code.
    The idea is pretty simple, we track the position of the weapon in its default state and in the state where the player would be aiming down sight, and if the player is holding the input for aiming down sight then we lerp to the aiming position, and if the player is not holding the aim button we lerp back to the default position. It's really as simple as that.
    Check out my new website: www.tvtig.com
    Reach out to me on twitter if you're stuck or need some help with the code: tvtig
    ___________________________________________________
    Subscribe: / @tvtig
    ____________________________________________________
  • Наука та технологія

КОМЕНТАРІ • 30

  • @coyfish955
    @coyfish955 Рік тому +13

    For everyone wondering what the code looks like, this is how I got it to work:
    using UnityEngine;
    public class GunAim : MonoBehaviour
    {
    public Transform activeWeapon;
    public Transform defaultPosition;
    public Transform adsPosition;
    public Vector3 weaponPosition; // set to 0 0 0 in inspector
    public float aimSpeed = 0.25f; // time to enter ADS
    public float _defaultFOV = 80f; // FOV in degrees
    public float zoomRatio = 0.5f; // 1/zoom times
    public Camera fpsCam; // player camera
    void Update()
    {
    // ADS camera and gun movement
    if(Input.GetButton("Fire2"))
    {
    weaponPosition = Vector3.Lerp(weaponPosition, adsPosition.localPosition, aimSpeed * Time.deltaTime);
    activeWeapon.localPosition = weaponPosition;
    SetFieldOfView(Mathf.Lerp(fpsCam.fieldOfView, zoomRatio * _defaultFOV, aimSpeed * Time.deltaTime));
    }
    else
    {
    weaponPosition = Vector3.Lerp(weaponPosition, defaultPosition.localPosition, aimSpeed * Time.deltaTime);
    activeWeapon.localPosition = weaponPosition;
    SetFieldOfView(Mathf.Lerp(fpsCam.fieldOfView, _defaultFOV, aimSpeed * Time.deltaTime));
    }
    }

    void SetFieldOfView(float fov)
    {
    fpsCam.fieldOfView = fov;
    }
    }

  • @arunchintamani5271
    @arunchintamani5271 Рік тому +4

    Went straight to the point thank you.

  • @Banaaani
    @Banaaani 2 роки тому

    really easy and good way of making ADS, thanks

    • @Tvtig
      @Tvtig  2 роки тому

      Glad you liked it!

  • @t3st3d
    @t3st3d Рік тому

    nice tutorial very helpful

  • @jswiff
    @jswiff Рік тому +1

    Great tutorial! Much appreciated. Do you think you can do a recoil tutorial? I like how yours looks on the rocket league video. 🔥💯

  • @unoproor
    @unoproor Рік тому +3

    Can someone write the full code pls

  • @versenova5531
    @versenova5531 2 роки тому +2

    Hey! I couldnt figure out how to get this to work. Everything is fine, but i cant get the position in teh ads script to actually work. Ive watched lots of times and dont know what to do still

  • @youssefgaaloul
    @youssefgaaloul 2 роки тому +2

    I like it. I didnt like using animations, cuz sometimes, the exit time and other vars just become complex as soon as you have to do it for every weapon and save animations seperatly. Now I can just use the two positions and change them in the script. I guess that saves storage space if your game is in a bigger scale, like BRs?

  • @marksseasalt5785
    @marksseasalt5785 2 роки тому +4

    a bit confusing, but really helpful tutorial!

    • @Tvtig
      @Tvtig  2 роки тому +1

      Fair enough, I think I'll do a more detailed tutorial on weapon management in a bit.

  • @TheGoldenDragonDev
    @TheGoldenDragonDev Рік тому

    Ok so for me, its working fine until the character starts moving vertically, like falling or going up, then the gun moves up or down(down when going up as it needs to catch up with player and up when going down for the same reason). How do i fix this

  • @donivynnothx1332
    @donivynnothx1332 5 місяців тому

    for some reason when I aim, the gun doesn't lerp to the right spot and just goes off the camera or clips into the camera. How do I fix this

  • @yankomirov4290
    @yankomirov4290 2 роки тому +1

    Can we have the code for the Weapons manager?

  • @skyjetblue5434
    @skyjetblue5434 2 роки тому +2

    hey, in the inspector you have a variable default weapon position, is that weaponPosition and also what type is it, vector3 or transform. thanks

    • @skyjetblue5434
      @skyjetblue5434 2 роки тому +1

      Can you please show me how you defined the variables: weaponPosition, weaponDefaultPosition, weaponAimingPosition. becuase im confused because if i set them to public Transform then I get an error, and if I set them to Public Vector3 then I get an error with the local position

  • @EricYoungVFX
    @EricYoungVFX 4 місяці тому

    praise jesus thank you

  • @iiropeltonen
    @iiropeltonen Рік тому +1

    A detail. If you were to aim with a pistol so that it's pressed against your cheek you would not only have horrible recoil but also a broken cheek bone 😅.

    • @Tvtig
      @Tvtig  Рік тому +1

      Fair enough lol

  • @ECSOrder66
    @ECSOrder66 2 роки тому +6

    litteraly making a Lego game with aiming down sights, will this work?

    • @sync3471
      @sync3471 2 роки тому

      Yeah, like the graphic style effects whether it will work

    • @ECSOrder66
      @ECSOrder66 2 роки тому

      @@sync3471 Yeah. I just tested it out, the aiming down sights didn't really work, but I'm pleased with a different result I got. Check my channel, it's the very first video I made. It could be better, but keep in mind, I only started 5 months ago, and that was my 3 month progress.

  • @moreontogameing6685
    @moreontogameing6685 2 роки тому

    thx

  • @jhin28
    @jhin28 Рік тому

    hi my guy. ı like your tutorials. but when I use lerp, gun teleports to aiming position. can you help?

    • @agtech6692
      @agtech6692 Рік тому

      maybe change the interpolation time to something lower