I am trying to make my first game in unity, being a FPS. I have all of the controls and am working on animations, but I cannot get recoil working. My idea is that I want the camera to slowly/quickly (depending on the gun) move upwards to act as semi-realistic recoil. This is the script I am currently using for shooting the gun:
using UnityEngine;
using System.Collections;
public class Gun_Shooting : MonoBehaviour {
float range = 100.0f;
float cooldown = 0.1f;
float cooldownRemaining = 0;
public GameObject shrapnelPrefab;
float recoil = 10.0f;
float damage = 5f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
cooldownRemaining -= Time.deltaTime;
if( Input.GetMouseButton (0) && cooldownRemaining <= 0 ) {
cooldownRemaining = cooldown;
Ray ray = new Ray( Camera.main.transform.position, Camera.main.transform.forward );
RaycastHit hitInfo;
Camera.main.transform.Rotate( recoil * Time.deltaTime, 0, 0 ) ;
if( Physics.Raycast(ray, out hitInfo, range ) ) {
Vector3 hitPoint = hitInfo.point;
Vector3 normal = hitInfo.normal;
GameObject go = hitInfo.collider.gameObject;
//Debug.Log( "Hit Object: " + go.name );
//Debug.Log( "Hit Point: " + hitPoint );
HasHealth health = go.GetComponent<HasHealth>();
if( health != null ) {
health.RecieveDamage( damage );
}
if(shrapnelPrefab != null) {
Instantiate(shrapnelPrefab, hitPoint, Quaternion.FromToRotation(Vector3.up, normal));
}
}
}
}
}
As you can see under the if statement, I am using the code Camera.main.transform.Rotate( recoil * Time.deltaTime, 0, 0 ) ;
to try and rotate the camera, but all this code is doing is jolting the screen slightly, not making the camera slowly travel up. I am new to both C# and unity, and so I would greatly appreciate the help.