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
____________________________________________________ - Наука та технологія
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;
}
}
thanks
Went straight to the point thank you.
really easy and good way of making ADS, thanks
Glad you liked it!
nice tutorial very helpful
Great tutorial! Much appreciated. Do you think you can do a recoil tutorial? I like how yours looks on the rocket league video. 🔥💯
Can someone write the full code pls
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
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?
a bit confusing, but really helpful tutorial!
Fair enough, I think I'll do a more detailed tutorial on weapon management in a bit.
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
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
Can we have the code for the Weapons manager?
hey, in the inspector you have a variable default weapon position, is that weaponPosition and also what type is it, vector3 or transform. thanks
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
praise jesus thank you
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 😅.
Fair enough lol
litteraly making a Lego game with aiming down sights, will this work?
Yeah, like the graphic style effects whether it will work
@@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.
thx
hi my guy. ı like your tutorials. but when I use lerp, gun teleports to aiming position. can you help?
maybe change the interpolation time to something lower