@@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;
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
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() {
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.
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.
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
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
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.
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.
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() {
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.....)
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
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.
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?
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
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.
mfw plai uploads a 3 minute video after months :dissolve:
sigma type stuff
@@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 😊
@@plaidev i think you meant ligma....
@@plaidev I can't accept the discord invitation
@@plaidev can u make a headbob tutorial
The legend is back, awesome!
I don't know how this tutorial could be more perfect. Thank you so much for making my game better
Man, there is no fluff its just straight to the point. love it
worked great thank you! I also added the players Y velocity to the mouse Y velocity for added feedback when jumping.
Can you elaborate on this, ie show your code?
@@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;
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
does the -90f depend on how much your gun is rotated??
@@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
Thanks bro
Thanks that solved my problem 🙏🙏
LifeSaver. Thx
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);
}
}
thx. too lazy to do it myself
Perfect tutorial. Showing how it looks in the end, straight to the implementation, and everything within a few minutes.
Woah, nice. Used this in my game, and it looks fantastic. Thx mate :)
Thank you for coming back. Can’t wait to see more tutorials :D
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
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.
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
Just when I needed it the most ;) Great that you are back
Oh boy, that's what i was looking at and don't know what was it name, finally find it
Thanks so much! Simple, straight forward. Worked like a charm!
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.
Currently working on a Descent-like game.
Used your method to make the camera rotate a bit slower than the cockpit model. Works greats!
You uploaded this whilst I was coding your slope tutorial lol. Welcome back king
"I was on vacation"
bruh
good to have you back tho
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
Love the script, simple and modular is all I need. Excellent tutorial.
Hey dude! Really like your videos, they're informative and to the point.
Wooooooooo u back after long time
Yessir
@@plaidev when new videos comeing
"Hey you! Youre finnaly awake! Now go upload some Tutorials!"
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
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.
THE KING IS BACK!!! hope u had a good vacation
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;
}
}
yay big bro wallrun tutorial guy is back :D
I just have to thank you so much. This added a lot of feeling to my game. Thx
You literally made my game look way better in just 3 mins ;) Thank you
So, you're best unity UA-camr, congratulations!
welcome back plai good to see you again
Simple and works, can't ask for more. TY sir
you're a really good teacher
I want new lessons, and thank you.
Great to see you back!
Awesome Video! Can't Wait for more
The best tutorial maker king is back boiiiiiiii
He also explains things very easily and briefly
short but straight forward perfect for beginners like me keep up the great work
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!
Great video, cant wait for the next vid.
This worked so well and this was so simple! Thank you so much!
EPIC! Finnaly I got weapon sway in my FPS game!
Nice!
no one makes tuts like these. So satisfying. Make another video already
no way a lemmy comment with zero likes
happy To be subscribed to this Channel :D
keep Going with the Good Stuff Bro
Dude don't stop making vids ur amazing at this
More videos please
Your videos are awesome
Maybe on vehicle physics or weapon shooting with recoil system
This is a great tutorial, Totally made my game look good, thanx mate really appreciate your work, keep it up
Thanks for this man. Earned a sub.
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.
Hippity hoppity your code is now my property :p
(・o・)
Love these. Keep them up. Im dying to know about view bobbing as well
Thank you very much! I was looking for a tutorial like that and this video helped me a lot!
Lol its the shortest and best tutor on weapon sway i found
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);
}
You just earned yourself a new subscriber :)
Thank you this helped me so much. straight to the point.
Fantastic video, works with no bs!
he's back les go
Love the vid...Very helpful
The legend is back!
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.....)
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
Another excellent tutorial. Thank you so much! 😊
im subbing cuz my wepon looked like a image and now it has life
the best tutorials i've ever seen
YES OMGGGGGG TY, PLAI UR THE BEST UPLOAD MORE
welcome back :)
Thanks for the tutorial, please make more like this
Fast and usefall just how I like it !
Thank you!
welcome back, plai
Good to have you back legend
lesss goooooooo the legend is baaaakkkkkk
This works amazing thank you so much !
Thankyou So much! this was very helpful
Nice i thought he stop making those cool videos but i am gonna say the thumbnail was nice
Welcome Back !!!!
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.
Now we want a weapon tutorial! Like If you want the same thing
your way to underrated wtf
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?
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
HE'S BACK
Thank you very much it helped me a lot! Thank you!
LEEESSSHH GOOOO NEW VIDEO!!!
Thanks very much for this tutorial
Your the best you are a life saver
bro is back
Yay you back!!!
Awesome Video!
Thank you so much for the tutorial, but if I rotate the player a continuously, the sway looks glitchy. Could you please help?
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.
fast, simple, no any trash
Nearly 4k 👀
:D
You have to keep making videos dude!
The videos just keep getting better 😎
Bro what happened with me on discord server?
Awesome job!
Thanks Alot This Helped!
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.