- Home /
Question by
GamerDude27 · Sep 09, 2018 at 07:15 PM ·
gun scriptweapon systeminertiasway
Problems With Weapon Sway
Hey, I've got this code for my current weapon sway system:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponSway : MonoBehaviour
{
// Variables
Vector3 vLastFacing;
float flMaxViewModelLag = 1.5f;
//-----------------------------------------------------------------------------
// Purpose: Use this for initialization
//-----------------------------------------------------------------------------
void Start ()
{
}
//-----------------------------------------------------------------------------
// Update is called once per frame
// Purpose: Calculates the sway the weapon has when looking around
//-----------------------------------------------------------------------------
Vector3 origin;
Quaternion angles;
Quaternion original_angles;
void Update ()
{
Vector3 vOriginalOrigin = origin;
Quaternion vOriginalAngles = angles;
// Calculate our drift
Vector3 forward = new Vector3();
VectorExtensionMethods.AngleVectors(angles, forward, Vector3.zero, Vector3.zero);
if (Time.deltaTime != 0.0f)
{
Vector3 vDifference = new Vector3();
VectorExtensionMethods.VectorSubtract(forward, vLastFacing, vDifference);
float flSpeed = 5.0f;
// If we start to lag too far behind, we'll increase the "catch up" speed.
// Solves the problem with fast cl_yawspeed, m_yaw or joysticks rotating quickly.
// The old code would slam m_vecLastFacing with origin causing the viewmodel to pop to a new position
float flDiff = vDifference.magnitude;
if ((flDiff > flMaxViewModelLag) && (flMaxViewModelLag > 0.0f))
{
float flScale = flDiff / flMaxViewModelLag;
flSpeed *= flScale;
}
// FIXME: Needs to be predictable?
VectorExtensionMethods.VectorMA(vLastFacing, flSpeed * Time.deltaTime, vDifference, vLastFacing);
// Make sure it doesn't grow out of control!!!
Vector3.Normalize(vLastFacing);
VectorExtensionMethods.VectorMA(origin, 5.0f, vDifference * -1.0f, origin);
Debug.Assert(vLastFacing.IsValid());
}
Vector3 right = new Vector3();
Vector3 up = new Vector3();
VectorExtensionMethods.AngleVectors(original_angles, forward, right, up);
// Mouse X/Y
float factorX = -Input.GetAxis("Mouse X");
float factorY = -Input.GetAxis("Mouse Y");
if (factorX > 180.0f)
{
factorX -= 360.0f;
}
else if (factorY < -180.0f)
{
factorY += 360.0f;
}
// If viewmodel lag equals 0 return to original origin/angles
if (flMaxViewModelLag == 0.0f)
{
origin = vOriginalOrigin;
angles = vOriginalAngles;
}
//FIXME: These are the old settings that caused too many exposed polys on some models
// X
VectorExtensionMethods.VectorMA(origin, -factorX * 0.035f, forward, origin);
VectorExtensionMethods.VectorMA(origin, -factorX * 0.03f, right, origin);
VectorExtensionMethods.VectorMA(origin, -factorX * 0.02f, up, origin);
// Y
VectorExtensionMethods.VectorMA(origin, -factorY * 0.035f, forward, origin);
VectorExtensionMethods.VectorMA(origin, -factorY * 0.03f, right, origin);
VectorExtensionMethods.VectorMA(origin, -factorY * 0.02f, up, origin);
}
}
Now what I'm unsure about is how to make it actually work in-game. My previous system had this to get it working:
Vector final = new Vector (def.x + factorX, def.y + factorY, def.z);
transform.localPosition = Vector.Lerp (transform.localPosition, final, Time.deltaTime = _smooth);
But I don't know how to do this for my current system.
Comment