Friday, March 31, 2017

Programming Weapon Sway

Weapon sway in games can easily be taken for granted by players. However, when it's not implemented the game looks plain and undone with a stationary weapon in the first person view. So that begs the question, how do first person shooters implement that cool weapon sway?! The truth is it's quite simple and this post will provide a brief code snippet on how to do it. Keep in mind that there are 1000 ways to skin a cat and the technique I'm presenting is one of those ways..but it is fairly popular.

Let's talk about Lissajous curves. There are plenty of websites that go into great detail about all the history, equations, and cool stuff you can do with Lissajous curves. I'll explain it simply and in terms of weapon sway. Think in your mind of your favorite FPS. As you walk the weapon typically sways horizontally and vertically in nice harmonic pattern. This complex motion in the x and y dimension is a Lissajous pattern.

Now, to program sway via Lissajous curves you can simply combine sin and/or cosine waves. There are different forms of the equations that use sin and/or cosine waves but for our example we will use 2 sin waves. Basically the goal is to oscillate vertically and horizontally harmoniously. From there you can customize your waves to get the effect you want.

This code will get you that traditional figure eight sway (this is a Unity C# snippet). Enjoy!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SimpleWeaponSway : MonoBehaviour 
{

    [SerializeField]
    Transform weapon;

    [Range(01f)]
    [SerializeField]
    float verticalSwayAmount = 0.5f;

    [Range(01f)]
    [SerializeField]
    float horiztonalSwayAmount = 1f;

    [Range(015f)]
    [SerializeField]
    float swaySpeed = 4f;

    // Update is called once per frame
    void Update () 
    {
        float x = 0, y = 0;
        y += verticalSwayAmount * Mathf.Sin((swaySpeed * 2) * Time.time);
        x += horiztonalSwayAmount * Mathf.Sin(swaySpeed * Time.time);        
        weapon.position = new Vector3(xyweapon.position.z);
    }
}

5 comments:

  1. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. softair san marino

    ReplyDelete
  2. Thanks For sharing this Superb article.I use this Article to show my assignment in college.it is useful For me Great Work. softair san marino

    ReplyDelete
  3. At the point when strike is submitted with a destructive weapon without goal to slaughter, it is named irritated ambush. This is a third-degree lawful offense.m4 elettrico

    ReplyDelete
  4. When I put this on my weapon it disappears? What's going on?

    ReplyDelete
  5. As the latest standard for Cascading Style Sheets (CSS), CSS3 makes it easier for programmers to create web applications that look good on both computers and mobile devices. Also, the developers can combine HTML5, CSS3, and JavaScript to build a variety of mobile web apps. Despite being backward-compatible with earlier versions of CSS, CSS3 includes several new modules.

    ReplyDelete