How to create weapon sway & weapon movement?

I’m trying to replicate this effect:

Right now, my weapon model is just attached to an Camera in Unity. It looks really really bad, like a static image in front of the camera. How can i add this sway effect on the weapon when the player moves the mouse? Also, how can i add a small “bobbing” effect to the weapon model, not the camera, like in the old Doom?

Have a look at the Vector3.slerp method, as well as maybe curves? I’m pretty sure you can use curves for what you need while simultaneously add an easy way to configure or tweak the weapon sway.

EDIT:
you can also have a look here:

This tutorial discusses gun weight and movement. It’s in UnityScript though, not sure if that’s what you’re using.

Lissajous curve? I find it very strong and annoying. Some people use Perlin, but how can you use something “random” like perlin to get a good curve?

I managed to get the old Quake sway, but it’s just lerping in a 8-like shape.

That video, btw, only helps you to create the “damp” effect on mouse movement. There’s no bobbing or sway effect added to the weapon model upon player movement or idleness.

Bumping. :slight_smile:

The below code I used for my game. It uses the Mathf.Sin() function to manipulate the weapons position.
I’m not sure if this is the best way of doing it, but its actually pretty close (85%) of what you’re looking for in that video. Hope this helps…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class sway : MonoBehaviour
{
float swayCounter = 0f;
float swayMagnitude = .1f;

// Start is called before the first frame update
void Start()
{

}
// Update is called once per frame
void Update()
{
float addAmount = Mathf.Sin(0.01f * swayCounter) * swayMagnitude * Time.deltaTime;
transform.localPosition += Vector3.up * addAmount;
swayCounter++;
}
}

Does it work with unity 2019.4?