[Add-on] Classic Lissajous Weapon Sway ("8", "U", "A" Curves)

YukiYuki Posts: 46Member
edited September 28 in Scripts & Mods
This UFPS vp_FP Weapon script extention adds the classic weapon bobbing / swaying using simple Lissajous curves for 3 different styles of movements : "8" curve ("Infinite" shape), "U" curve (45° Lissajous curve), and "A" curve (135° Lissajous curve) which is the "U" curve inverted.

Still, these scripts have to be improved. The curves will have to be dependent of the Player's velocity. They also should be deactivated when the Player jumps and when States are used, such as Run (adding lissajous values to 0 in the States text files should make it) -- unless Rotation Springs aren't used.

Simply copy and paste the following lines into the vp_FP Weapon script and the vp_FP Weapon Editor script to have access to the Lissajous values

__________________________________________________

/!\ FIRST : In vp_FP Weapon script, inside protected override void FixedUpdate() ; - You have to type the following otherwise the lissajous Updates won't work.

Under UpdateBob();
		UpdateLissajous8 ();

UpdateLissajousU ();

UpdateLissajousA ();
__________________________________________________

Lissajous "8" Sway / vp_FP Weapon

Under // weapon bob script :
	// classic lissajous 8 sway
[Range (0f, 0.0010f)]
[SerializeField]
public float xLissajous8Sway = 0.0002f;
[Range (0f, 0.0010f)]
[SerializeField]
public float yLissajous8Sway = 0.0001f;
[Range (0f, 10f)]
[SerializeField]
public float lissajous8Speed = 5f;
After UpdateBob() script :
protected virtual void UpdateLissajous8 ()
{
// lissajous 8 curve for a Goldeneye-like weapon sway effect
// activates when player velocity is over or under zero on horizontal and vertical axis
float x = 0, y = 0;
y += yLissajous8Sway * Mathf.Sin ((lissajous8Speed * 2) * Time.time) ;
x += xLissajous8Sway * Mathf.Sin (lissajous8Speed * Time.time) ;
if ((Player.Velocity.Get().z > 0)
| (Player.Velocity.Get().z < 0)
| (Player.Velocity.Get().x > 0)
| (Player.Velocity.Get().x < 0))
m_PositionSpring.AddForce (new Vector2 (x, y));
}
Lissajous "U" Sway / vp_FP Weapon

Under // classic lissajous 8 sway script :
	// classic lissajous U sway
[Range (0f, 0.0010f)]
[SerializeField]
public float xLissajousUSway = 0.0002f;
[Range (0f, 0.0010f)]
[SerializeField]
public float yLissajousUSway = 0.0003f;
[Range (0f, 5f)]
[SerializeField]
public float lissajousUSpeed = 3f;
After UpdateLissajous8() script :
protected virtual void UpdateLissajousU ()
{
// lissajous U curve for a Doom-like weapon sway effect
// activates when player velocity is over or under zero on horizontal and vertical axis
float x = 0, y = 0;
y += yLissajousUSway * Mathf.Sin ((lissajousUSpeed * 2) * Time.time) ;
x += xLissajousUSway * Mathf.Sin (lissajousUSpeed * Time.time + Mathf.PI / 4) ;
if ((Player.Velocity.Get().z > 0)
| (Player.Velocity.Get().z < 0)
| (Player.Velocity.Get().x > 0)
| (Player.Velocity.Get().x < 0))
m_PositionSpring.AddForce (new Vector2 (x, y));
}
Lissajous "A" Sway (Inverted "U") / vp_FP Weapon

Under // classic lissajous U sway script :
    // classic lissajous A sway
    [Range (0f, 0.0010f)]
    [SerializeField]
    public float xLissajousASway = 0.00025f;
    [Range (0f, 0.0010f)]
    [SerializeField]
    public float yLissajousASway = 0.0001f;
    [Range (0f, 15f)]
    [SerializeField]
    public float lissajousASpeed = 10f;
After UpdateLissajousU() script :
 protected virtual void UpdateLissajousA ()
{
// lissajous A curve (Inverted U) for a Perfect Dark-like weapon sway effect
        // activates when player velocity is superior or inferior to zero on x and z axis
// CAUTION : X and Y axis are inverted (to be fixed)
        float xA = 0, yA = 0;
        yA += yLissajousASway * Mathf.Sin ((lissajousASpeed / 2) * Time.time);
        xA += xLissajousASway * Mathf.Sin (lissajousASpeed * Time.time + ((Mathf.PI + Mathf.PI + Mathf.PI) / 4));
        if ((FPPlayer.Velocity.Get ().z > 0)
            | (FPPlayer.Velocity.Get ().z < 0)
            | (FPPlayer.Velocity.Get ().x > 0)
            | (FPPlayer.Velocity.Get ().x < 0))
            m_PositionSpring.AddForce (new Vector3 (yA, xA, 0));
    }
__________________________________________________

All Lissajous in Unity Inspector / vp_FP Weapon Editor

Type at line 34 :
public static bool m_WeaponLissajousFoldout;
Type at line 96 :
DoLissajousFoldout();
After DoBobFoldout() script :


    /// <summary>
    /// 
    /// </summary>
    public virtual void DoLissajousFoldout()
    {
        
        m_WeaponLissajousFoldout = EditorGUILayout.Foldout (m_WeaponLissajousFoldout, "Lissajous");

        if (m_WeaponLissajousFoldout) {                

            GUILayout.Label ("Classic 8 Sway", vp_EditorGUIUtility.NoteStyle);
        GUILayout.Space(2);
            GUILayout.Label ("Goldeneye 64 Style \n Average values : x : 0.0002 / y : 0.0001 / Speed : 5", vp_EditorGUIUtility.NoteStyle);

            m_Component.xLissajous8Sway = EditorGUILayout.FloatField ("x Sway", m_Component.xLissajous8Sway);
            m_Component.yLissajous8Sway = EditorGUILayout.FloatField ("y Sway", m_Component.yLissajous8Sway);
            m_Component.lissajous8Speed = EditorGUILayout.FloatField ("Speed", m_Component.lissajous8Speed);

        vp_EditorGUIUtility.Separator ();
    
            GUILayout.Label ("Very Classic U Sway", vp_EditorGUIUtility.NoteStyle);
        GUILayout.Space (2);
            GUILayout.Label ("Doom / Duke Nukem 3D Style \n Average values : x : 0.0002 / y : 0.0003 / Speed : 3", vp_EditorGUIUtility.NoteStyle);

            m_Component.xLissajousUSway = EditorGUILayout.FloatField ("x Sway", m_Component.xLissajousUSway);
            m_Component.yLissajousUSway = EditorGUILayout.FloatField ("y Sway", m_Component.yLissajousUSway);
            m_Component.lissajousUSpeed = EditorGUILayout.FloatField ("Speed", m_Component.lissajousUSpeed);

        vp_EditorGUIUtility.Separator ();

            GUILayout.Label ("Classic A Sway (Inverted U)", vp_EditorGUIUtility.NoteStyle);
        GUILayout.Space(2);
            GUILayout.Label ("Perfect Dark / TimeSplitters Style \n Average values : x : 0.00025 / y : 0.0001 / Speed : 10", vp_EditorGUIUtility.NoteStyle);
     // x and y are visually inverted back from the inverted result in the vp_FP Weapon script
            m_Component.yLissajousASway = EditorGUILayout.FloatField ("x Sway", m_Component.yLissajousASway);
            m_Component.xLissajousASway = EditorGUILayout.FloatField ("y Sway", m_Component.xLissajousASway);
            m_Component.lissajousASpeed = EditorGUILayout.FloatField ("Speed", m_Component.lissajousASpeed);

        }
    }
__________________________________________________

Showcase Video

Comments

  • TryThisTryThis Posts: 887Member
    You've handed over a lot of interesting and useful work and it is really appreciated. Thank you!
  • JustinJustin Posts: 307Member, Administrator, Moderator
    Thanks for posting such a detailed addon! The springs in version 2 behave similar to how they do in version 1 but it would be nice to be able to select different types. I've bookmarked this thread!

    [ UFPS Docs ] . . . [ F.A.Q. ] . . . [ Tutorials ] . . . [ Knowledgebase ]
    [ Games Showcase ] . . . [ Get it now! ]

    [home] [twitter] [youtube]
  • YukiYuki Posts: 46Member
    edited September 20
    I don't really know how should I code the selection script as I'm very new to coding (this add-on is my very first achievement). I tried to make something with "Activated" checkboxes for each, but I messed it up and my script was not working anymore for some reason. I thought it would have been nice to use the UFPS prefab system with text files, but it was even harder for me and I gave up this idea. The Activated checkboxes are what I want and I need to figure out how to disable the UpdateLissajousX() in vp_FP Weapon if a checkbox is set on false in vp_FP Weapon Editor.

    I'll make some update next week. Hope you enjoy it for now and feel free to improve this add-on (about the Player's Velocity dependancy, it's very important).
  • YukiYuki Posts: 46Member
    edited September 21
    Could you tell me if this new code links the sway speed to the player's velocity ?
    protected virtual void UpdateLissajous8 ()
    {
    // lissajous 8 curve for a Goldeneye-like weapon sway effect
    // activates when player velocity is over or under zero on horizontal and vertical axis
    float x = 0, y = 0;
    y += yLissajous8Sway * Mathf.Sin ((lissajous8Speed * 2) * Time.time) ;
    x += xLissajous8Sway * Mathf.Sin (lissajous8Speed * Time.time) ;
    velocitySway8 = Player.Velocity.Get() / lissajous8Speed;
    clampVelocitySway8 = Mathf.Clamp (velocitySway8, 0, 5);
    if ((Player.Velocity.Get().z > 0)
    | (Player.Velocity.Get().z < 0)
    | (Player.Velocity.Get().x > 0)
    | (Player.Velocity.Get().x < 0))
    m_PositionSpring.AddForce (new Vector2 ((x / clampVelocitySway8), (y / calmpVelocitySway8));
    }
    Not sure what the 0 and 5 should be in the Mathf.Clamp parenthesis (Player's min and max velocity ?)
  • JustinJustin Posts: 307Member, Administrator, Moderator
    Within vp_FPWeapon.UpdateSwaing the sway velocity is retrieved with:

    m_SwayVel = Controller.velocity * PositionInputVelocityScale;
    m_SwayVel = Vector3.Min(m_SwayVel, Vector3.one * PositionMaxInputVelocity);
    m_SwayVel = Vector3.Max(m_SwayVel, Vector3.one * -PositionMaxInputVelocity);

    Controller.velocity will return the player's velocity.

    [ UFPS Docs ] . . . [ F.A.Q. ] . . . [ Tutorials ] . . . [ Knowledgebase ]
    [ Games Showcase ] . . . [ Get it now! ]

    [home] [twitter] [youtube]
  • TryThisTryThis Posts: 887Member
    edited September 21
    Yes, they are minimum and maximum values. They should be floats. (xxx, 0, 5.0f)
  • YukiYuki Posts: 46Member
    Okay, I'll try to make something with that next week.
  • YukiYuki Posts: 46Member
    edited September 27
    About the scripts not working, I found out I forgot to set the Update functions in the FixedUpdate() ! I'm so stupid...

    I updated the installation process.

    Otherwise, it seems like I can't use m_SwayVel in my scripts as it is a Vector3 thing while my values are floats.
  • YukiYuki Posts: 46Member
    Showcase video added !
Sign In or Register to comment.