Search Unity

How to create weapon sway & weapon movement?

Discussion in 'Scripting' started by vincentellis, Sep 24, 2015.

  1. vincentellis

    vincentellis

    Joined:
    Oct 21, 2013
    Posts:
    100
    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?
     
  2. ande04b

    ande04b

    Joined:
    Aug 29, 2012
    Posts:
    119
    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.
     
  3. vincentellis

    vincentellis

    Joined:
    Oct 21, 2013
    Posts:
    100
    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.
     
  4. vincentellis

    vincentellis

    Joined:
    Oct 21, 2013
    Posts:
    100
    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.
     
  5. vincentellis

    vincentellis

    Joined:
    Oct 21, 2013
    Posts:
    100
    Bumping. :)
     
  6. ZDTTUTA

    ZDTTUTA

    Joined:
    Mar 20, 2021
    Posts:
    7
    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++;
    }
    }
     
  7. drivedev6

    drivedev6

    Joined:
    Apr 12, 2021
    Posts:
    1
    Does it work with unity 2019.4?