Procedural Weapon Animations

I want to animate the weapon based on the state of the player, I have bool’s like , Is Walking and IsSprinting, I want to procedurally animate the weapon like UFPS, Or similar, … Any help would be appreciated

A tutorial like this is probably a good starting point for those kind of effects: https://www.youtube.com/watch?v=DR4fTllQnXg

Thanks for the answer though I already watched it and it didn’t work best, But I found another tutorial which suits my needs but doesn’t change the weapon pose while running so I wrote some code myself., and it works!, If anyone wants to see the code here it is (I needed it for my fps full body series) Fps series : https://www.youtube.com/playlist?list=PLGED7dmKbboVRQSlz2qdLbM0fgeqnmfsz

Code :

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

public class GunHolderAnimated : MonoBehaviour
{

    #region General

    // Settings
    public HeadBobSettings headBobSettings;
    public MainBobSettings mainBobSettings;

    // Start Pos, Rot.
    Vector3 startPos;
    Vector3 startRot;

    // Start is called before the first frame update
    void Start()
    {
        startPos = transform.localPosition;
        startRot = transform.localRotation.eulerAngles;
    }

    // Update is called once per frame
    void Update()
    {
        HeadBobCheck();
        SmoothPositionToTarget();
        SmoothRotToTarget();
    }

    #endregion

    #region Functions

    private void HeadBobCheck()
    {
        Vector2 moveInput = mainBobSettings.controller.Input.Player.Move.ReadValue<Vector2>();
        float inputMag = new Vector3(moveInput.x, 0f, moveInput.y).magnitude;

        if (inputMag > 0)
        {
            CreateHeadBobPositionEffect();
            MoveTilt();
        }
    }

    private Vector3 CreateHeadBobPositionEffect()
    {
        Vector3 pos = Vector3.zero;

        if (mainBobSettings.controller.IsGrounded() == true)
        {
            if (mainBobSettings.controller.IsWalking() == true)
            {
                pos.y += Mathf.Lerp(pos.y, Mathf.Sin(Time.time * headBobSettings.walkBobSpeed) *
                    headBobSettings.walkBobAmount * 1.4f, headBobSettings.smoothing * Time.deltaTime);
                pos.x += Mathf.Lerp(pos.x, Mathf.Cos(Time.time * headBobSettings.walkBobSpeed / 2f) *
                    headBobSettings.walkBobAmount * 1.6f, headBobSettings.smoothing * Time.deltaTime);
            }
            else if (mainBobSettings.controller.IsSprinting() == true)
            {
                // CHANGES
                //pos.y += Mathf.Lerp(pos.y, Mathf.Sin(Time.time * headBobSettings.sprintBobSpeed) *
                //    headBobSettings.sprintBobAmount * 1.4f, headBobSettings.smoothing * Time.deltaTime);
                //pos.x += Mathf.Lerp(pos.x, Mathf.Cos(Time.time * headBobSettings.sprintBobSpeed / 2f) *
                //    headBobSettings.sprintBobAmount * 1.6f, headBobSettings.smoothing * Time.deltaTime);
                pos.y += Mathf.Lerp(pos.y, Mathf.Sin(Time.time * headBobSettings.sprintBobSpeed) *
                    headBobSettings.sprintBobAmount * 1.4f, headBobSettings.smoothing * Time.deltaTime);
                pos.z += Mathf.Lerp(pos.x, Mathf.Cos(Time.time * headBobSettings.sprintBobSpeed / 2f) *
                    headBobSettings.sprintBobAmount * 1.6f, headBobSettings.smoothing * Time.deltaTime);
            }
            else if (mainBobSettings.controller.IsCrouching() == true)
            {
                pos.y += Mathf.Lerp(pos.y, Mathf.Sin(Time.time * headBobSettings.crouchBobSpeed) *
                    headBobSettings.crouchBobAmount * 1.4f, headBobSettings.smoothing * Time.deltaTime);
                pos.x += Mathf.Lerp(pos.x, Mathf.Cos(Time.time * headBobSettings.crouchBobSpeed / 2f) *
                    headBobSettings.crouchBobAmount * 1.6f, headBobSettings.smoothing * Time.deltaTime);
            }

            transform.localPosition += pos;
        }

        return pos;
    }

    private Vector3 MoveTilt()
    {
        Vector3 rot = Vector3.zero;

        if (mainBobSettings.controller.IsGrounded() == true)
        {
            Vector2 moveInput = mainBobSettings.controller.Input.Player.Move.ReadValue<Vector2>();

            rot.z = moveInput.x * headBobSettings.xTiltMulti * Time.deltaTime;

            transform.localRotation *= Quaternion.Euler(rot);
        }

        return rot;
    }

    private void SmoothPositionToTarget()
    {
        if (transform.localPosition == startPos) return;
        transform.localPosition = Vector3.Lerp(transform.localPosition, startPos + (mainBobSettings.controller.IsSprinting() ?
            headBobSettings.sprintPositionOffset : Vector3.zero), headBobSettings.smoothing * Time.deltaTime);
    }

    private void SmoothRotToTarget()
    {
        transform.localRotation = Quaternion.Slerp(transform.localRotation, Quaternion.Euler(mainBobSettings.controller.IsSprinting() ?
            headBobSettings.sprintRotation : startRot), headBobSettings.smoothing * Time.deltaTime);
    }

    #endregion

}

#region Settings

[Serializable]
public class HeadBobSettings
{
    [Header("Bobbing Settings")]
    public float walkBobAmount = 0.0035f;
    public float walkBobSpeed = 14f;
    public float sprintBobAmount = 0.015f;
    public float sprintBobSpeed = 20f;
    public float crouchBobAmount = 0.004f;
    public float crouchBobSpeed = 12f;
    public float smoothing = 8f;
    public float xTiltMulti = 30f;

    public Vector3 sprintRotation = new Vector3(10, -10, 0);
    public Vector3 sprintPositionOffset = Vector3.zero;
}

[Serializable]
public class MainBobSettings
{
    [Header("Controller")]
    public PlayerController controller;
}

#endregion
1 Like