I am working implementing a recoil model for a FPS gun.
I have looked at
http://forum.unity3d.com/viewtopic.php?t=20059&highlight=recoil
-But not found this approach to work. I cant seem to configure the ConfigurableJoint so ithas the tension I want.
Therefore I have started implementing a small script that manipulates the localPosition and localRotation parts of the right hand of my character.
I use the verlet method to update the position, and this works fine for translation.
BUT!!!
My approach breaks down when I try to do the same with the quaternion based rotations.
Any suggestions on how to do the same with quaternions?
using UnityEngine;
using System.Collections;
public class Sway : MonoBehaviour {
private Vector3 pOrig;
private Vector3 pNow;
private Vector3 pLast;
private float inversemass= 0.001f;
private float LinearStiffness = 1000000.0f;
private float LinearDamping = 0.35f;
void Awake()
{
print("Awake...");
pOrig = transform.localPosition;
pLast = new Vector3(0,0,0);
}
void LateUpdate ()
{
Vector3 LinearAcceleration;
Vector3 tmpV;
LinearAcceleration = pNow*(LinearStiffness*inversemass);
tmpV = pNow;
pNow = (2.0f - LinearDamping)*pNow-(1.0f-LinearDamping)*pLast - LinearAcceleration * Time.deltaTime * Time.deltaTime;
pLast = tmpV;
transform.localPosition = pOrig + pNow;
}
void kick()
{
print("Kick");
pLast = transform.localRotation * new Vector3(0.02f,0.0f,0.0f);
}
void Update()
{
if(Input.GetKeyDown("k"))
{
kick();
}
}
}