So I am following this tutorial series.
On the 19th video he suggests we set the script on the gun to lateupdate() as it stutters when we apply headbob. I cannot do this because I have Final IK running to attach the arms to the gun and it runs on LateUpdate() and so the arms stutter when I put the gunscript on late update.
There was no stutter before adding two lines and so I felt perhaps smoothing the motion with a Mathf.SmoothDamp might work but it didn’t.
Here are the two scripts
Mouse Rotator
The lines between 50 and 60 are what started the stutter
using UnityEngine;
using System.Collections;
public class MouseRotator : MonoBehaviour {
public float SensitivityX = 5.0f;
public float SensitivityY = 5.0f;
public float looksmoothdamp = 0.1f;
public float minX = -90, maxX = 90;
public bool EffectX, EffectY;
public float RotationX;
public float RotationY;
float CurrentRotationX;
float CurrentRotationY;
float xRotationVel;
float yRotationVel;
public GameObject root;
public float currentAimRatio = 1;
public float HeadbobSpeed = 1;
public float HeadbobStepCounter;
public float HeadbobAmountX = 1;
public float HeadbobAmountY = 1;
Vector3 parentLastPos;
Vector3 OriginalPos;
public float eyeheightratio = 0.9f;
public bool BOBHEAD = false;
float currentXPOS;
public float currentXPOSVel;
float currentYPOS;
public float currentYPOSVel;
public float bobsmooth = 0.1f;
void Awake(){
OriginalPos = transform.localPosition;
parentLastPos = transform.root.position;
}
// Update is called once per frame
void Update () {
if (BOBHEAD) {
if (root.GetComponent<CharacterMotor> ().grounded) {
Debug.Log("grounded");
HeadbobStepCounter += Vector3.Distance (parentLastPos, root.transform.position) * HeadbobSpeed;
}
Vector3 v3t = transform.localPosition;
v3t.z = Mathf.Sin (HeadbobStepCounter) * HeadbobAmountX * currentAimRatio + OriginalPos.z;
v3t.x = (Mathf.Cos (HeadbobStepCounter * 2) * HeadbobAmountY * -1 * currentAimRatio) + OriginalPos.x;
transform.localPosition = v3t;
parentLastPos = root.transform.position;
}
if(EffectX == true){
RotationX -= Input.GetAxis("Mouse Y") * SensitivityX * currentAimRatio;
RotationX = Mathf.Clamp (RotationX, minX, maxX);
CurrentRotationX = Mathf.SmoothDamp (CurrentRotationX, RotationX, ref xRotationVel, looksmoothdamp);
transform.rotation = Quaternion.Euler (CurrentRotationX, root.transform.eulerAngles.y, 0);
}
if(EffectY == true){
RotationY += Input.GetAxis("Mouse X") * SensitivityY * currentAimRatio;
CurrentRotationY = Mathf.SmoothDamp (CurrentRotationY, RotationY, ref yRotationVel, looksmoothdamp);
transform.rotation = Quaternion.Euler (0, CurrentRotationY, 0);
}
}
}
and the gunscript
using UnityEngine;
using System.Collections;
public class Gunscript : Photon.MonoBehaviour {
public PlayerContentManager PCM;
float targetXRotation;
float targetYRotation;
float targetXRotationVel;
float targetYRotationVel;
public float rotateSpeed = 0.3f;
public float holdHeight = -0.28f;
public float holdSide = 0.13f;
public float ratiohip = 1f;
public float aimspeed = 0.1f;
float ratiohipVel;
public float aimSensRatio = 0.4f;
public float RoF = 15.0f;
float waitFire = 0.0f;
public GameObject Muzzle;
public GameObject Bullet;
public float ShootAngle = 10.0f;
public float ShootAngleAiming = 5.0f;
public float recoilForce = 0.05f;
public float recoveryTime = 0.2f;
float currentRecoilZPOS;
float currentRecoilZPOSVel;
public GameObject BulletSound;
public GameObject MuzzleFlash;
// Update is called once per frame
void Update () {
if (Input.GetButton ("Fire1")) {
if(waitFire <=0){
if(Bullet){
Instantiate(Bullet, Muzzle.transform.position, Muzzle.transform.rotation);
targetXRotation += (Random.value - 0.5f) * Mathf.Lerp(ShootAngleAiming, ShootAngle, ratiohip);
targetYRotation += (Random.value - 0.5f) * Mathf.Lerp(ShootAngleAiming, ShootAngle, ratiohip);
currentRecoilZPOS -= recoilForce;
}
if(MuzzleFlash){
GameObject MuzzleFlashObject = (GameObject) Instantiate(MuzzleFlash, Muzzle.transform.position, Muzzle.transform.rotation);
MuzzleFlashObject.transform.parent = transform;
}
if(BulletSound){
GameObject SoundObject = (GameObject) Instantiate(BulletSound, Muzzle.transform.position, Muzzle.transform.rotation);
SoundObject.transform.parent = transform;
}
waitFire = 1;
}
}
waitFire -= Time.deltaTime * RoF;
currentRecoilZPOS = Mathf.SmoothDamp (currentRecoilZPOS, 0, ref currentRecoilZPOSVel, recoveryTime);
if (Input.GetButton ("Fire2")) {
PCM.xRotator.currentAimRatio = aimSensRatio;
PCM.yRotator.currentAimRatio = aimSensRatio;
ratiohip = Mathf.SmoothDamp (ratiohip, 0, ref ratiohipVel, aimspeed);
}
if (!Input.GetButton ("Fire2")) {
PCM.xRotator.currentAimRatio = 1.0f;
PCM.yRotator.currentAimRatio = 1.0f;
ratiohip = Mathf.SmoothDamp (ratiohip, 1, ref ratiohipVel, aimspeed);
}
transform.position = PCM.PlayerCamera.transform.position + (Quaternion.Euler (0,targetYRotation,0) * new Vector3(holdSide * ratiohip,holdHeight * ratiohip,0) + Quaternion.Euler (targetXRotation,targetYRotation,0) * new Vector3(0,0,currentRecoilZPOS));
targetXRotation = Mathf.SmoothDamp (targetXRotation, PCM.xRotator.RotationX,ref targetXRotationVel, rotateSpeed);
targetYRotation = Mathf.SmoothDamp (targetYRotation, PCM.yRotator.RotationY,ref targetYRotationVel, rotateSpeed);
transform.rotation = Quaternion.Euler (targetXRotation, targetYRotation, 0);
}
}