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?