Help With Recoil Script
Here is my script, the issue is that my Gun only moves to it's recoil Position for a half second. When recoil == false my gun is being moved to the players hand smoothly. GunLook() is being run in the update function.
Effectively, how do I check that my Gun has reached it's recoilPosition for a decent amount of time;
void GunLook() {
if(Recoil == false)
{
GunObj.position = Vector3.Lerp(GunObj.transform.position, GunPos.position, Mathf.SmoothStep(0f,1f,LerpSpeedR));
GunObj.localRotation = Quaternion.Lerp(GunObj.rotation, GunPos.rotation, Mathf.SmoothStep(0f,1f,LerpSpeedP));
}
else
{
GunObj.position = Vector3.Lerp(GunObj.transform.position, GunPos.position + GunPos.TransformDirection(Vector3.back) * RecoilAmount, Mathf.SmoothStep(0f,1f,LerpSpeedR));
GunObj.localRotation = Quaternion.Lerp(GunObj.rotation, GunPos.rotation, Mathf.SmoothStep(0f,1f,LerpSpeedP));
Recoil = false;
}
if(Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Physics.Raycast(GunObj.transform.position, GunObj.TransformDirection(Vector3.forward), out hit, 100f);
Recoil = true;
if(hit.rigidbody)
{
hit.rigidbody.AddForceAtPosition(GunObj.TransformDirection(Vector3.forward * force), hit.point);
}
}
}
}
Also of course, any tips on improving my code in general or for readability are very welcome :)
Your answer
Follow this Question
Related Questions
Problems making FPS arms and the gun work together 1 Answer
How can I animate the trigger and hammer of a gun? 1 Answer
How to make gun shoot in the crosshair & automatic fire (Javascript) 1 Answer
How to make an automatic fire mode for a gun (Javascript) 0 Answers
How to fix ( ArgumentException: Input Button fire 1 is not setup) 1 Answer