Search Unity

Need help with weapon recoil for an FPS using C#

Discussion in 'Scripting' started by Miner717, Feb 20, 2015.

  1. Miner717

    Miner717

    Joined:
    Feb 17, 2015
    Posts:
    2
    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:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Gun_Shooting : MonoBehaviour {
    6.  
    7.     float range = 100.0f;
    8.     float cooldown = 0.1f;
    9.     float cooldownRemaining = 0;
    10.     public GameObject shrapnelPrefab;
    11.     float recoil = 10.0f;
    12.     float damage = 5f;
    13.    
    14.  
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.    
    19.     }
    20.    
    21.     // Update is called once per frame
    22.     void Update () {
    23.  
    24.         cooldownRemaining -= Time.deltaTime;
    25.  
    26.         if( Input.GetMouseButton (0) && cooldownRemaining <= 0 ) {
    27.             cooldownRemaining = cooldown;
    28.             Ray ray = new Ray( Camera.main.transform.position, Camera.main.transform.forward );
    29.             RaycastHit hitInfo;
    30.             Camera.main.transform.Rotate( recoil * Time.deltaTime, 0, 0 ) ;
    31.  
    32.  
    33.  
    34.             if( Physics.Raycast(ray, out hitInfo, range ) ) {
    35.                 Vector3 hitPoint = hitInfo.point;
    36.                 Vector3 normal = hitInfo.normal;
    37.                 GameObject go = hitInfo.collider.gameObject;
    38.                 //Debug.Log( "Hit Object: " + go.name );
    39.                 //Debug.Log( "Hit Point: " + hitPoint );
    40.  
    41.                 HasHealth health = go.GetComponent<HasHealth>();
    42.  
    43.                 if( health != null ) {
    44.                     health.RecieveDamage( damage );
    45.                 }
    46.  
    47.                 if(shrapnelPrefab != null) {
    48.      
    49.                     Instantiate(shrapnelPrefab, hitPoint, Quaternion.FromToRotation(Vector3.up, normal));
    50.                 }
    51.             }
    52.         }
    53.     }
    54. }
    As you can see under the if statement, I am using the code
    Code (csharp):
    1. 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.
     
  2. Fyko-chan

    Fyko-chan

    Joined:
    Sep 29, 2012
    Posts:
    76
    Look up Rigidbody.AddTorque
     
  3. Adnan601

    Adnan601

    Joined:
    Nov 17, 2014
    Posts:
    5
  4. Miner717

    Miner717

    Joined:
    Feb 17, 2015
    Posts:
    2
    Bump. Still can't get it working. Any more help?
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    the code above happens in a single frame. If you want something to happen "over time" you're probably looking at a coroutine or in this case I'd probably go with an Animator with a simple rotate up and back animation and a trigger call.