Weapon sway with idle animation together?

Hey there!
I’ve been working on a small FPS game, and it has reached a point where it has an idle animation.
I wanted to make things a bit better, so I added a sway script.
The problem with the sway is that it repetitively sets the position of the gun through the update function (which is intentional of course), but it doesn’t leave place for the idle animation, so I’ll only have sway and won’t see the animation running.
The idle animation is not more than a simple up and down on position to simulate breathing.
I tried to set the update option in the animator to “Animate Physics”, but then everything is very laggy and jiggly.
This is the Sway script:

public float amount = 0.055f;
    public float maxAmount = 0.09f;
    float smooth = 3;
    Vector3 def;
  

    void Start ()
    {
        def = transform.localPosition;
      
    }

    float _smooth;
    void Update ()
    {
        _smooth = smooth;

        float factorX = -Input.GetAxis("Mouse X") * amount;
        float factorY = -Input.GetAxis("Mouse Y") * amount;

        if (factorX > maxAmount)
            factorX = maxAmount;

        if (factorX < -maxAmount)
            factorX = -maxAmount;

        if (factorY > maxAmount)
            factorY = maxAmount;

        if (factorY < -maxAmount)
            factorY = -maxAmount;

        Vector3 final = new Vector3(def.x + factorX, def.y + factorY, def.z);
        transform.localPosition = Vector3.Lerp(transform.localPosition, final, Time.deltaTime * _smooth);
    }

I also tried changing the method to FixedUpdate, which I remember is used for physics, but it seems like that makes no sway at all.
Any ideas on how to combine the animation and the sway without ruining absolutely everything?

One way, as long as the animations aren’t too extreme (sounds like they’re not), is to have the animations drive different transforms in a hierarchy, something like this hierarchy:

// this is a GameObject hierarchy, not code!

ThePlayer
  TheWeaponHoldingTransform
    TransformToDriveForIdleBreathing
      TransformToDriveForWeaponSway
        TheActualGunTransform

Then you can separately drive the two sub-transforms with your small sway and idle animations and I believe it will blend properly. I’ve not done this with animations before but that’s kinda the reference way to mix other rotations such as tank barrels that both rotate around 360 deg and also elevate regardless of their heading.

2 Likes

hey, did you find the solution because I am watching the same tutorial and having the same problems as you…
Thanks in advance