Two Small Mechanics to Physically Handle Weapons - Unity 3D Tutorial

Поділитися
Вставка
  • Опубліковано 4 жов 2024

КОМЕНТАРІ • 32

  • @Adot000
    @Adot000 Місяць тому

    Dude you're the GOAT, I used your tutorial "Weapon Sway And Bobbing Without Animations" to create Weapon Sway and Bobbing with Unity's Input System! And now attempting to add these mechanics. Thank you for your great explanation and tutorials, it really helped me understand and debug any issues I came across. Would love to see you do more tutorials like these!

  • @neozoid7009
    @neozoid7009 Рік тому +6

    please a tutorial on Procedural recoil . I love your tutorials they are soo clean and easy to implement in general

  • @a3dadventure79
    @a3dadventure79 8 місяців тому +1

    appreciate your tutorials, especially the depth and care you go into with presenting them. keep up the good work

  • @elvizer_2388
    @elvizer_2388 2 роки тому +5

    This is awesome! Great tutorial for aspiring game devs.

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

    Not sure if anyone else has run into this, but for those having a "stuttering" issue with their weapon (for the ClipPrevention script), change the "Update" function to "LateUpdate" and it smooths everything out.
    Might be just a situation I'm running into with my game, but I figured I'd share in case someone else runs into this same issue.

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

    very enlightening stuff! i'll definetly check out your devlogs, your game is looking great!

  • @jerematic10
    @jerematic10 8 місяців тому +1

    Another way to avoid camera clipping with an FPS weapon is to use a dedicated Weapon Camera as an overlay Camera, then stack it into the Main Camera. This avoids any coding to achieve the same result and also allows for an empty game object to hold all Cameras and contain a script to handle procedural recoil.
    Personally I have a Main Camera as the base, a Weapon Camera overly and a UI Camera overlay so I can control the views for each independently with layer masks.

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

    This is probably a great tutorial!

  • @GilbertGames
    @GilbertGames 9 місяців тому +2

    Whoa I saw my vid in your search

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

    thank you!

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

    Not sure if this is a good idea but what i had in mind was, you still shoot from the main cameras middle point, when you hit something, you store the hit position, and you make the gun fire a bullet towards that hit position

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

      That’s how many games will fudge the visuals of firing. Technically the real “bullet” that actually deals damage will originate from the camera and a separate fake bullet will originate from the barrel of the gun for visual effect. Garbaj had a good video on how some games pull off this trick: ua-cam.com/video/EpkvNUoxlxM/v-deo.html

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

    bro is so right!!!

  • @Xiao-t8x
    @Xiao-t8x 21 день тому

    thanks for the great tutorial ❤❤ full code below (rotation + position)

    • @Xiao-t8x
      @Xiao-t8x 21 день тому

      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      public class ClipPrevention : MonoBehaviour
      {
      public GameObject ClipProjector; // Reference to the object projecting the clip
      public float checkDistance = 5f; // Max distance for raycast checking
      public Vector3 newDirection = new Vector3(30, 0, 0); // New rotation direction
      public Vector3 positionOffset = new Vector3(0, 0, -0.5f); // Offset to adjust position when clipping
      private Vector3 initialPosition; // To store the gun's initial position
      private float lerpPos;
      private RaycastHit hit;
      // Start is called before the first frame update
      void Start()
      {
      // Store the initial local position of the gun so it doesn't get modified unintentionally
      initialPosition = transform.localPosition;
      }
      // Update is called once per frame
      void Update()
      {
      // Perform raycast to check for obstacles in front of the ClipProjector
      if (Physics.Raycast(ClipProjector.transform.position, ClipProjector.transform.forward, out hit, checkDistance))
      {
      lerpPos = 1 - (hit.distance / checkDistance);
      }
      else
      {
      lerpPos = 0;
      }
      // Clamp the lerp position between 0 and 1
      lerpPos = Mathf.Clamp01(lerpPos);
      // Rotate the object smoothly based on the calculated lerp position
      transform.localRotation =
      Quaternion.Lerp(
      Quaternion.Euler(Vector3.zero),
      Quaternion.Euler(newDirection),
      lerpPos
      );
      // Adjust the position smoothly based on the lerp position only if necessary
      // Moves the object backwards based on the hit distance, preventing clipping
      transform.localPosition = Vector3.Lerp(
      initialPosition, // Starting position is the gun's initial position
      initialPosition + positionOffset, // Offset when clipping is detected
      lerpPos // Use the lerpPos value for the transition
      );
      }
      }

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

    I think I’m going to try this but instead of turning the weapon I will switch the raycast to shoot from the main camera when it gets to the clipping range. No idea if this is a good idea or not but worth a try

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

    Drop and equip weapons please

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

      I've largely moved on from game dev, but tutorials for dropping and picking up weapons exist. When I was working on my project, I originally based it off of @davegamedevelopment 's system seen here: ua-cam.com/video/8kKLUsn7tcg/v-deo.html

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

    In my case, shooting from muzzle position mess up the aim and the shot doesn't land in the middle of the screen where the crosshair is, how did you fix it?

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

      It's actually a given that your shots will be slightly off using this method. You can partially mitigate it by tweaking how the weapon is held, or by creating your own custom solutions to hide this fact. Garbaj talks about how some games hide how their weapons work in his video here: ua-cam.com/video/EpkvNUoxlxM/v-deo.html
      Alternatively, you can fully embrace it. Right around the time I put this video out, I was trying to implement a system similar to Escape from Tarkov, but in a more Sci-Fi setting. As a result, I had the aiming reticle show the actual point of impact on the player's HUD, as opposed to being fixed in the center of the screen. You can see that in this dev log of the project I had at the time: ua-cam.com/video/Sfwc9xpgNOs/v-deo.html
      I never actually say how this works, and I actually abandoned that code some time ago, but If I recall correctly I would constantly have a raycast coming out of the weapon, and then convert the hit.point to the screen using Camera.WorldToScreenPoint in unity.

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

      @@buffatwo Thank you for your reply, will try something)

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

    Hey, how would you go about lerping the current position to the new position? Not talking about lerping back to the original position.

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

      for anyone wondering the answer was simple, just use Mathf.Lerp(lerpPos, (1 - (hit.distance / checkDistance)), SMOOTH * Time.deltaTime);

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

    Do you know what is the funny part
    On my game have system simuler to this on video that the weapon fire for weapon and after and wail this works perfectly I saying scrow this system it's so much hasle
    And make the weapon shots on camera center 😅
    After all the game I'm working it's more like old fps the have player to walk fast all the time I will implement crazy weapon ECT 😅 it's multiplayer game and the have slot system you don't able to Care all the weapon's like most game on the type of game I tried to make but this game play distion I have disaid for my game and not trying to make my game realistic.
    With the acception of slot system and few other mekanics I have plan to implement my game I will probably similar game field like game's as quake and unreal tournament 😅
    It will not be quake clone or arena clone whatever the called but it's fast past game with not loosing accuracy wean you move and collect weapon and item for the map and the player moved are very similar for those games I ever have double jump.
    But other than that I will have my own weapons I have the slot system ECT.
    This game is still in progress and haven't made weapon shooting wet it's still early in development.

  • @Pancake_simp15.
    @Pancake_simp15. 11 місяців тому +1

    5:59 can you leave a link to download this weapon model?

    • @buffatwo
      @buffatwo  11 місяців тому

      I actually made the weapon models and animations myself in blender. Honestly they aren’t that great, I’m a pretty inexperienced modeler/animator, so they were meant to be placeholders on this project. Also, I had a hard drive failure a while back so I’m not 100% sure if I still have them.

    • @Pancake_simp15.
      @Pancake_simp15. 11 місяців тому +1

      @@buffatwo well if you have the time can you make a tutorial on how to make a model similar to the kriss vector in this video(i think its a kriss vector)

    • @buffatwo
      @buffatwo  11 місяців тому

      @@Pancake_simp15. Probably not, I simply don't have time to do much 3D modeling nowadays because I'm focusing on other things.
      To give an overview of what I did, I photobashed together a Vector for the lower of the weapon and a MP5-SD for the upper. From there I popped it into blender to use as my reference image and made the general profile of the weapon. I went through about 2-3 revisions before I got something I was happy with. The magazine and charging handle are separate pieces since they need to move for the animation. Nothing really advanced, and plenty of other people have made tutorials for making similar models. Here is an example of a guy making an AK-47 which is significantly nicer than what I did: ua-cam.com/video/FiE-nCrTiqM/v-deo.html
      Once the model was made, I made a separate project for my FPS view model animations, following this tutorial and others like it: ua-cam.com/video/IV6XP-EDzw8/v-deo.htmlsi=LuV4YgXVBAVcWuwf
      From there it was all about moving the models and animations into unity, another topic which plenty of people covered.

    • @Pancake_simp15.
      @Pancake_simp15. 11 місяців тому +1

      @@buffatwo completely understandable! Have a nice day evening or night

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

    Could you maybe show the code for how to lerp to the position over the course of few frames? I've tried doing it myself but honestly i can't figure it out, and would apreacite if you could help me out on this.

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

      I actually had to check an old backup I had to find the line I used. That code and the code I showed in the video are slightly different, but if you take a look at the `else` statement in the clip prevention script, I think you would just need to change the line from `lerpPos = 0` to instead be `lerpPos -= 0.075f`.
      That way, instead of directly setting the value to zero you're just whittling away at the value over a few frames. Directly after that is the clamp between 0 and 1 so it should stay between those two values, though looking at the video now I realize I made a typo and it should've been `lerpPos = Mathf.Clamp01(lerpPos);`
      Hope that helps! This coming week I'm also doing a new tutorial on procedural weapon bobbing/sway which may also interest you.

    • @theFishy_
      @theFishy_ Рік тому +2

      @@buffatwo hey, thank you very much for the help and sorry for the burden of going through old backups hahah, the code you provided does indeed work for bringing the weapon back slowly. And I will surely check out the New tutorial, good luck with your game 😉