- Home /
Storing camera angles for a FPS
I am making a First Person Shooter as sort of an introduction to programming. I have separate scripts. one handles movement and camera rotation, one handles spawning the bullets and applying a force to them, and the last handles specifics of shooting a bullet.
However, I would like to make it so whenever a bullet is instantiated, the camera rotates simulating knock-back in a way; but I'm not quite sure how to do that. Here is my code:
void Update () {
Quaternion x = Quaternion.Euler (Input.GetAxis ("Mouse X"));
Quaternion y = Quaternion.Euler (Input.GetAxis ("Mouse Y" + 3));
if(Input.GetButton ("Fire1")){
Camera c = Camera.main;
verticalRotation = Input.GetAxis ("Mouse Y");
c.transform.localRotation.Quaternion.Euler(x,y,0);
}
}
In this, I'm trying to store the camera rotation in Quaternion angles, and add 3 to the camera's vertical rotation nudging it up a bit. Is this a viable way of doing such a thing and I'm just missing something, or should I go a completely different route?
Answer by 334499p · Jul 20, 2015 at 09:23 PM
This method gets a little bit jittery so what I do is parent the main camera to an empty GameObject. Use that gameObject as the "camera" for your character camera script and then use the actual camera to handle all extraneous camera motions.
Oh. That's a nice idea to try out. Another idea I came across was to attach an animation to the camera, making an animation to just shake it. Is there a tutorial I could glimpse at to help me do this with my camera?
Animating this wouldn't allow for randomness of the recoil effect, because of that there aren't tutorials out there for that approach. But if you want to use that approach then you need to still parent the cam to an empty gameobject since basic animations override scripts. So you need to create the animation and change its mode to legacy then use the following script on the camera:
Animation anim;
Void Start(){
anim = GetComponent<Animation>();
}
Void Recoil(){
if(anim.isPlaying()){
anim.CrossFade("gunRecoilAnimation");
}else{
anim.Play();
}
}
$$anonymous$$ake sure the animation on the camera is also set to one shot and dont play it on start.