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.
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.
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++;
}
}