Unity Weapon Sway Tutorial in less than 3 min

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

КОМЕНТАРІ • 278

  • @unigolde
    @unigolde 3 роки тому +98

    mfw plai uploads a 3 minute video after months :dissolve:

    • @plaidev
      @plaidev  3 роки тому +13

      sigma type stuff

    • @syedsadiq8631
      @syedsadiq8631 3 роки тому

      @@plaidev plai can you plz make the swipe camera to see tutorial for mobile by watching 5 videos I create mobile game. But not camera tutorial 😊

    • @unigolde
      @unigolde 3 роки тому +1

      @@plaidev i think you meant ligma....

    • @Tybek1
      @Tybek1 3 роки тому +1

      @@plaidev I can't accept the discord invitation

    • @TheForceElementz
      @TheForceElementz 3 роки тому +2

      @@plaidev can u make a headbob tutorial

  • @Danboodles
    @Danboodles 3 роки тому +8

    The legend is back, awesome!

  • @djalaljay
    @djalaljay 3 роки тому +17

    I don't know how this tutorial could be more perfect. Thank you so much for making my game better

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

    Man, there is no fluff its just straight to the point. love it

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

    worked great thank you! I also added the players Y velocity to the mouse Y velocity for added feedback when jumping.

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

      Can you elaborate on this, ie show your code?

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

      @@baxterclagmoar9333 basically just replaced the mouse x and y variables with: float x = Input.GetAxisRaw("Mouse X") * multiplier + rb.velocity.x/10;
      float y = Input.GetAxisRaw("Mouse Y") * multiplier + -rb.velocity.y;

  • @potatoo2137
    @potatoo2137 2 роки тому +31

    Great tutorial. Quick tip:
    if your guns rotation is not set to zero on a Y axis you need to use this modified code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class GunSway : MonoBehaviour
    {
    public float smooth;
    public float swayMultiplayer;
    private void Update()
    {
    float mouseX = Input.GetAxisRaw("Mouse X") * swayMultiplayer -90f;
    float mouseY = Input.GetAxisRaw("Mouse Y") * swayMultiplayer;
    Quaternion rotationX = Quaternion.AngleAxis( mouseY, Vector3.left);
    Quaternion rotationY = Quaternion.AngleAxis(mouseX, Vector3.up);
    Quaternion targetRotation = rotationX * rotationY;
    transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, smooth * Time.deltaTime);
    }
    }
    otherwise just use this code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class GunSway : MonoBehaviour
    {
    public float smooth;
    public float swayMultiplayer;
    private void Update()
    {
    float mouseX = Input.GetAxisRaw("Mouse X") * swayMultiplayer ;
    float mouseY = Input.GetAxisRaw("Mouse Y") * swayMultiplayer;
    Quaternion rotationX = Quaternion.AngleAxis( mouseY, Vector3.left);
    Quaternion rotationY = Quaternion.AngleAxis(mouseX, Vector3.up);
    Quaternion targetRotation = rotationX * rotationY;
    transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, smooth * Time.deltaTime);
    }
    }
    ur welcome

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

      does the -90f depend on how much your gun is rotated??

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

      @@RileyyyTTV yes. For me, my gun is rotated -270 deegres on the y axis, so just change the 90 to how much your gun is rotated

    • @mr-mayo7168
      @mr-mayo7168 2 роки тому +1

      Thanks bro

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

      Thanks that solved my problem 🙏🙏

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

      LifeSaver. Thx

  • @hareyold3460
    @hareyold3460 2 роки тому +25

    heres the script if you want to paste it:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class WeaponSway : MonoBehaviour
    {

    [Header("Sway Settings")]
    [SerializeField] private float smooth;
    [SerializeField] private float swayMultiplier;
    // Start is called before the first frame update
    void Start()
    {

    }
    // Update is called once per frame
    void Update()
    {
    float mouseX = Input.GetAxisRaw("Mouse X") * swayMultiplier;
    float mouseY = Input.GetAxisRaw("Mouse Y") * swayMultiplier;
    Quaternion rotationX = Quaternion.AngleAxis(-mouseY, Vector3.right);
    Quaternion rotationY = Quaternion.AngleAxis(mouseX, Vector3.up);
    Quaternion targetRotation = rotationX * rotationY;
    transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, smooth * Time.deltaTime);
    }
    }

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

      thx. too lazy to do it myself

  • @cecrit
    @cecrit 2 роки тому +3

    Perfect tutorial. Showing how it looks in the end, straight to the implementation, and everything within a few minutes.

  • @andypomsztein291
    @andypomsztein291 3 роки тому +10

    Woah, nice. Used this in my game, and it looks fantastic. Thx mate :)

  • @playerplus5917
    @playerplus5917 3 роки тому +3

    Thank you for coming back. Can’t wait to see more tutorials :D

  • @Solidrigs
    @Solidrigs 3 роки тому +2

    man the way you explain things is purely straight to the point I have never yawned a time, you deserve more 0s behind your subs

  • @Legionope
    @Legionope 9 місяців тому

    Amazing tutorial. Btw, for people using this on flashlight in their project, use negative value on the swayMultiplication.
    The player's flashlight will then tilt toward the target faster than the camera turns, giving a really cool effect.

  • @stylie473joker5
    @stylie473joker5 3 роки тому +1

    Maaan i just discovered your channel yesterday and saw that you haven't posted in 5 months and today you post haha damn the timing welcome back

  • @desawwww
    @desawwww 3 роки тому +2

    Just when I needed it the most ;) Great that you are back

  • @swordy70
    @swordy70 3 роки тому +1

    Oh boy, that's what i was looking at and don't know what was it name, finally find it

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

    Thanks so much! Simple, straight forward. Worked like a charm!

  • @thatsjustfunny1
    @thatsjustfunny1 3 роки тому +2

    Hey brother, just stumbled across your channel and your tutorials are awesome! Please keep it up, unbelievable that you only have 2K subscribers!
    Also I really appreciate that you've been referencing the unity documentation in your videos so I can learn more about the issues.

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

    Currently working on a Descent-like game.
    Used your method to make the camera rotate a bit slower than the cockpit model. Works greats!

  • @ezioteddie1223
    @ezioteddie1223 3 роки тому

    You uploaded this whilst I was coding your slope tutorial lol. Welcome back king

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

    "I was on vacation"
    bruh
    good to have you back tho

  • @DR-kw4ff
    @DR-kw4ff Рік тому

    thx alot men i was struggling a bit with adding sway, and my code was looking very close to yours, but after watching your video i've realised that i had made some mistakes that caused my issues:D

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

    Love the script, simple and modular is all I need. Excellent tutorial.

  • @antn908
    @antn908 3 роки тому +2

    Hey dude! Really like your videos, they're informative and to the point.

  • @syedsadiq8631
    @syedsadiq8631 3 роки тому +1

    Wooooooooo u back after long time

  • @owo600
    @owo600 3 роки тому +1

    "Hey you! Youre finnaly awake! Now go upload some Tutorials!"

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

    Bro you do not know how happy I a I finally found a tutorial for this I switched from ue5 to unity but in ue it's something called "spring arm" so I tried to code it my self well I still suck at coding but then I found your vid. amazing
    I know this is an old vid but still so damn helpful

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

    Another good way is to aim a raycast from the gun at the hit position of a raycast from camera. Just change the rotation of the gun or gun root. This allows the gun to move closer to the enemy when facing walls too.

  • @PeterBouSaada
    @PeterBouSaada 3 роки тому

    THE KING IS BACK!!! hope u had a good vacation

  • @stopmotionman9137
    @stopmotionman9137 8 місяців тому

    Heres the script i used to get controller and mouse input and weapon sway. Thanks for the amazing tutorial :)
    Script:
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.InputSystem;
    public class WeaponSway : MonoBehaviour
    {
    [Header("Sway Settings")]
    [SerializeField] private float smooth;
    [SerializeField] private float swayMultiplier;
    private Vector2 inputVector;
    private void Update()
    {
    // Get input from mouse or controller
    inputVector = GetInput();
    // Calculate target rotation
    float mouseX = inputVector.x * swayMultiplier;
    float mouseY = inputVector.y * swayMultiplier;
    Quaternion rotationX = Quaternion.AngleAxis(-mouseY, Vector3.right);
    Quaternion rotationY = Quaternion.AngleAxis(mouseX, Vector3.up);
    Quaternion targetRotation = rotationX * rotationY;
    // Rotate
    transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, smooth * Time.deltaTime);
    }
    private Vector2 GetInput()
    {
    // Get input from mouse or controller
    Vector2 mouseInput = Vector2.zero;
    Vector2 controllerInput = Vector2.zero;
    // Check if mouse input is available
    if (Mouse.current != null)
    mouseInput = Mouse.current.delta.ReadValue();
    // Check if controller input is available
    if (Gamepad.current != null)
    controllerInput = Gamepad.current.rightStick.ReadValue();
    // Use mouse input if available, otherwise use controller input
    if (Mathf.Abs(mouseInput.x) > Mathf.Abs(controllerInput.x))
    return mouseInput.normalized;
    else
    return controllerInput.normalized;
    }
    }

  • @LetuscontinueBlending
    @LetuscontinueBlending 3 роки тому +1

    yay big bro wallrun tutorial guy is back :D

  • @MAS9Nine
    @MAS9Nine 7 місяців тому

    I just have to thank you so much. This added a lot of feeling to my game. Thx

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

    You literally made my game look way better in just 3 mins ;) Thank you

  • @nespero576
    @nespero576 3 роки тому

    So, you're best unity UA-camr, congratulations!

  • @ardlyn4134
    @ardlyn4134 3 роки тому

    welcome back plai good to see you again

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

    Simple and works, can't ask for more. TY sir

  • @alaap3334
    @alaap3334 3 роки тому +1

    you're a really good teacher
    I want new lessons, and thank you.

  • @chonkgames
    @chonkgames 3 роки тому

    Great to see you back!

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

    Awesome Video! Can't Wait for more

  • @pranavsharma4268
    @pranavsharma4268 3 роки тому

    The best tutorial maker king is back boiiiiiiii
    He also explains things very easily and briefly

  • @thirstyemmo
    @thirstyemmo 3 роки тому +4

    short but straight forward perfect for beginners like me keep up the great work

  • @hellobrother5849
    @hellobrother5849 3 роки тому

    My laptop is broken rn so cant even test this thing out
    But bro u explain things so efficiently and in an understandable way hats off!

  • @synkgd619
    @synkgd619 3 роки тому

    Great video, cant wait for the next vid.

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

    This worked so well and this was so simple! Thank you so much!

  • @REIO-mn6cx
    @REIO-mn6cx 3 роки тому +1

    EPIC! Finnaly I got weapon sway in my FPS game!

  • @LowLevelLemmy
    @LowLevelLemmy 3 роки тому

    no one makes tuts like these. So satisfying. Make another video already

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

      no way a lemmy comment with zero likes

  • @Khaled_khalil
    @Khaled_khalil 3 роки тому

    happy To be subscribed to this Channel :D
    keep Going with the Good Stuff Bro

  • @jeffjohnbonesjones4043
    @jeffjohnbonesjones4043 3 роки тому

    Dude don't stop making vids ur amazing at this

  • @mrworldwide9172
    @mrworldwide9172 3 роки тому +1

    More videos please
    Your videos are awesome
    Maybe on vehicle physics or weapon shooting with recoil system

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

    This is a great tutorial, Totally made my game look good, thanx mate really appreciate your work, keep it up

  • @Jeast.
    @Jeast. Рік тому

    Thanks for this man. Earned a sub.

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

    Exactly what I was after! A concise tutorial with code and example at the end in a few minutes....
    No wonder you have only 6k subs. UA-cam doesn't like it when you don't talk bollocks for 20 minutes.

  • @xzippyzachx
    @xzippyzachx 3 роки тому +1

    Hippity hoppity your code is now my property :p

  • @Paragorax
    @Paragorax 3 роки тому

    Love these. Keep them up. Im dying to know about view bobbing as well

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

    Thank you very much! I was looking for a tutorial like that and this video helped me a lot!

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

    Lol its the shortest and best tutor on weapon sway i found

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

    This was quick, but It always points towards the player
    [Header("Sway Settings")]
    [SerializeField] private float smooth;
    [SerializeField] private float swayMultiplier;
    // Start is called before the first frame update
    void Start()
    {

    }
    // Update is called once per frame
    void Update()
    {
    float mouseX = Input.GetAxisRaw("Mouse X") * swayMultiplier;
    float mouseY = Input.GetAxisRaw("Mouse Y") * swayMultiplier;
    Quaternion rotationX = Quaternion.AngleAxis(-mouseY, Vector3.right);
    Quaternion rotationY = Quaternion.AngleAxis(mouseX, Vector3.up);
    Quaternion targetRotation = rotationX * rotationY;
    transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, smooth * Time.deltaTime);
    }

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

    You just earned yourself a new subscriber :)

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

    Thank you this helped me so much. straight to the point.

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

    Fantastic video, works with no bs!

  • @bennydaboy6984
    @bennydaboy6984 3 роки тому +1

    he's back les go

  • @gamedevdemonold
    @gamedevdemonold 3 роки тому +1

    Love the vid...Very helpful

  • @MMKnight_1
    @MMKnight_1 3 роки тому

    The legend is back!

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

    Hey i found a very good fix if your gun is going too right or too left. Its because of the scale of your screen. For example, my screen is 2560x1440. Which means i have to use this:
    float mouseX = Input.GetAxisRaw("Mouse X") * swayMultiplier / (2560.0f / 1440.0f);
    float mouseY = Input.GetAxisRaw("Mouse Y") * swayMultiplier;
    With that was X and Y is using same power for smoothness and sway. (If you are going to release this game you know that many people will have different monitor sizes so i don't know how can you change it automaticly but i believe this will work for all since all of them make something like 1.7777.....)

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

    For the FPS movement tutorials i have an idea for it, can you make the camera like offset and bob a little when you hit the ground
    so like landing feedback

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

    Another excellent tutorial. Thank you so much! 😊

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

    im subbing cuz my wepon looked like a image and now it has life

  • @berserqq
    @berserqq 3 роки тому

    the best tutorials i've ever seen

  • @jaqindlemalingle3591
    @jaqindlemalingle3591 3 роки тому

    YES OMGGGGGG TY, PLAI UR THE BEST UPLOAD MORE

  • @yaxpatel
    @yaxpatel 3 роки тому

    welcome back :)

  • @josemanuel5205
    @josemanuel5205 3 роки тому

    Thanks for the tutorial, please make more like this

  • @mhze
    @mhze 3 роки тому

    Fast and usefall just how I like it !
    Thank you!

  • @silvhook
    @silvhook 3 роки тому

    welcome back, plai

  • @feetless6969
    @feetless6969 3 роки тому +1

    Good to have you back legend

  • @idkwhatmynameisyetlol7331
    @idkwhatmynameisyetlol7331 3 роки тому

    lesss goooooooo the legend is baaaakkkkkk

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

    This works amazing thank you so much !

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

    Thankyou So much! this was very helpful

  • @rekhajain9044
    @rekhajain9044 3 роки тому

    Nice i thought he stop making those cool videos but i am gonna say the thumbnail was nice

  • @Alan-jy5wr
    @Alan-jy5wr 3 роки тому

    Welcome Back !!!!

  • @KatDiedAgain
    @KatDiedAgain 3 роки тому +3

    This is SO much easier than my janky setup, thank you!
    Does anyone know of a way to move the anchor point of this rotation forward a certain distance? I need my weapon to rotate around a point a few meters in front of the player instead of right where the container object is located, and I can't move the container object.

  • @kagedevshorts4886
    @kagedevshorts4886 3 роки тому +1

    Now we want a weapon tutorial! Like If you want the same thing

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

    your way to underrated wtf

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

    would there be a way to add this same movement to just a camera? Kind of like a delay or sway to the camera while looking around or moving the mouse quickly?

  • @bca1546
    @bca1546 3 роки тому

    New Brackey, Bro please start making videos again cause all your videos are needed content i always see ur videos because they are easy to understand and worth it

  • @marksseasalt5785
    @marksseasalt5785 3 роки тому

    HE'S BACK

  • @mihneasimionescu2022
    @mihneasimionescu2022 3 роки тому

    Thank you very much it helped me a lot! Thank you!

  • @Vangsguard
    @Vangsguard 3 роки тому

    LEEESSSHH GOOOO NEW VIDEO!!!

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

    Thanks very much for this tutorial

  • @anj_10
    @anj_10 3 роки тому +1

    Your the best you are a life saver

  • @aser1249
    @aser1249 3 роки тому

    bro is back

  • @sonalvadnere5722
    @sonalvadnere5722 3 роки тому

    Yay you back!!!

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

    Awesome Video!

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

    Thank you so much for the tutorial, but if I rotate the player a continuously, the sway looks glitchy. Could you please help?

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

    Looks good but the only problem is that the effect is weaker at higher frame rates and stronger at lower frame rates which isn't ideal.

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

    fast, simple, no any trash

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

    Nearly 4k 👀
    :D

  • @zerohidz
    @zerohidz 3 роки тому

    You have to keep making videos dude!

  • @xacer
    @xacer 3 роки тому +1

    The videos just keep getting better 😎

    • @Tybek1
      @Tybek1 3 роки тому

      Bro what happened with me on discord server?

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

    Awesome job!

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

    Thanks Alot This Helped!

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

    If anyone is having the error where your weapon sways in the WRONG direction (i.e. your weapon sways toward the direction you're turning, not away as it should):
    I discovered that to fix this, I also had to make the mouseX value negative in line 18. This fixed the problem for me.