below is my weapon sway script
works not as i expected. the problem is it overshoots when passing the “zero” of an axis.
how do i prevent this hickup in the rotation?
script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class gunsway : MonoBehaviour
{
public Transform root;
Vector3 rotationLast;
Vector3 rotationDelta;
public float rotationAmount = 4f;
public float speed = 3f;
public float x;
public float y;
void Start ()
{
rotationLast = root.transform.eulerAngles;
}
void LateUpdate ()
{
rotationDelta = (root.transform.eulerAngles - rotationLast) / Time.deltaTime /200f;
rotationDelta.y = Mathf.Round(rotationDelta.y);
rotationDelta.x = Mathf.Round (rotationDelta.x);
rotationDelta.x = Mathf.Clamp (rotationDelta.x, -4f, 4f) ;
rotationDelta.y = Mathf.Clamp (rotationDelta.y, -4f, 4f);
Vector3 wantedrotation = new Vector3 (rotationDelta.x , rotationDelta.y,0f);
wantedrotation *= rotationAmount;
x = rotationDelta.x;
y = rotationDelta.y;
transform.localRotation = Quaternion.Slerp(transform.localRotation,Quaternion.Euler(wantedrotation),Time.deltaTime * speed );
if (rotationDelta.magnitude == 0) {
rotationLast = -rotationLast;
}
rotationLast = root.transform.eulerAngles;
//transform.localEulerAngles = rotationDelta;
}
public Vector3 angularvelocity
{
get
{
return rotationDelta;
}
}
}