- Home /
Recoil with the FPS controller
Hello! I'm trying to create a recoil system to my current FPS game.
The recoil system should rotate the camera, and then slowy change back to normal again, however the mouselook script locks the camera. To work around this, I put it in a bool to check if it could rotate. (mouselook.cs)
public void LookRotation(Transform character, Transform camera)
{
if (active)
{
float yRot = CrossPlatformInputManager.GetAxis("Mouse X") * XSensitivity;
float xRot = CrossPlatformInputManager.GetAxis("Mouse Y") * YSensitivity;
m_CharacterTargetRot *= Quaternion.Euler(0f, yRot, 0f);
m_CameraTargetRot *= Quaternion.Euler(-xRot, 0f, 0f);
if (clampVerticalRotation)
m_CameraTargetRot = ClampRotationAroundXAxis(m_CameraTargetRot);
if (smooth)
{
character.localRotation = Quaternion.Slerp(character.localRotation, m_CharacterTargetRot,
smoothTime * Time.deltaTime);
camera.localRotation = Quaternion.Slerp(camera.localRotation, m_CameraTargetRot,
smoothTime * Time.deltaTime);
}
else
{
character.localRotation = m_CharacterTargetRot;
camera.localRotation = m_CameraTargetRot;
}
UpdateCursorLock();
}
}
However, this gave a very laggy look, and didn't work as i wanted. heres the recoil script
public class Recoil : MonoBehaviour {
[HideInInspector]
public float recoil, max;
[HideInInspector]
public GameObject recoil_obj;
private float rot, recoverSpeed;
public void Update()
{
RecoilControl();
}
public void AddRecoil()
{
rot += recoil / 10;
MouseLook.active = false;
Invoke("UnlockMouse", 0.2f);
}
void RecoilControl()
{
if(rot > 0)
{
rot -= Time.deltaTime * recoverSpeed;
}
if (rot < 0)
rot = 0;
Vector3 c_rot = recoil_obj.transform.localEulerAngles;
recoil_obj.transform.localEulerAngles = new Vector3(c_rot.x + rot, c_rot.y,c_rot.z);
}
void UnlockMouse()
{
MouseLook.active = true;
}
}
Answer by FortisVenaliter · Apr 13, 2017 at 04:07 PM
I would recommend making your camera a child of the mouselook object. Then you could easily apply the recoil locally to the camera itself, which would combine it's rotation with the parent's Mouselook.
This worked! I feel stupid for not thinking about it myself
Your answer
Follow this Question
Related Questions
CS:GO-like recoil 1 Answer
FPS Gun Recoil (Accuracy) 1 Answer
Camera Recoil,Please help me. 2 Answers
Using recoil on a Gun? 1 Answer
Realistic Scope Effect? 1 Answer