Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Recoil

Discussion in 'Scripting' started by The3DKnight, Jan 27, 2013.

  1. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Hi guys.
    I need some help with recoil . I've managed to script shooting but the problem is my when shooting rifle gun doesn't get back to original position , and i can't recoil when shooting automatically.I don't know how to describe problem best.So i have variable recoil and when i shoot it should increase but when not decrease but it doesn't. Can anybody help. And go easy on me i'm still new to scripting.

    Code (csharp):
    1. //Shooting
    2. var BulletPrefab:Transform;
    3. var force : float = 2000;
    4.  
    5. //Rifle
    6. var ammo : int = 100;
    7. var FireRate = 0.1;
    8. var Rifle = true;
    9. var CanFire = true;
    10.  
    11. //Recoil
    12. var recoilMod : Transform;
    13. var weapon : GameObject;
    14. var maxRecoil_x : float = -20;
    15. var recoilSpeed : float = 10;
    16. var recoil : float = 0.0;
    17.  
    18.  
    19.  
    20. function Update()
    21. {
    22.  
    23.  
    24. if (Rifle == false )
    25. {
    26.  
    27. if(Input.GetButtonDown("Fire1"))
    28.     {  
    29.     if (ammo >0)
    30.         {
    31.              
    32.             recoil+= 0.1;
    33.             Fire();
    34.         }
    35.     }
    36. }
    37. else {
    38. if(Input.GetButton("Fire1"))
    39. {
    40.  
    41.     if (ammo >0)
    42. {
    43.  
    44.      recoil += 0.1;
    45.     Fire();
    46. }
    47. }
    48.         }
    49.  
    50.     }
    51.  
    52.  
    53. function Fire ()
    54. {
    55. if(recoil > 0)
    56.  
    57.     {
    58.  
    59.         var maxRecoil = Quaternion.Euler (maxRecoil_x, 0, 0);
    60.  
    61.         recoilMod.rotation = Quaternion.Slerp(recoilMod.rotation, maxRecoil, Time.deltaTime * recoilSpeed);
    62.  
    63.         weapon.transform.localEulerAngles.x = recoilMod.localEulerAngles.x;
    64.  
    65.         recoil -= Time.deltaTime * 5;
    66.  
    67.     }
    68.  
    69.     else
    70.  
    71.     {
    72.  
    73.         recoil = 0;
    74.  
    75.         var minRecoil = Quaternion.Euler (0, 0, 0);
    76.  
    77.         recoilMod.rotation = Quaternion.Slerp(recoilMod.rotation, minRecoil,Time.deltaTime * recoilSpeed / 2);
    78.  
    79.         weapon.transform.localEulerAngles.x = recoilMod.localEulerAngles.x;
    80.  
    81.     }
    82.  
    83.  
    84. if(CanFire == true)
    85. {
    86.     var bullet = Instantiate(BulletPrefab, GameObject.Find("spawnPoint").transform.position, GameObject.Find("Obj").transform.rotation);
    87.     bullet.rigidbody.AddForce(bullet.transform.forward * force);
    88.     CanFire = false;
    89.         yield WaitForSeconds (FireRate);
    90.     CanFire  = true;
    91.    
    92.     ammo -= 1;
    93.    
    94.    
    95.    
    96. }
    97.  
    98. }
    99.  
    100.  
    101.  
    Thanx

    -3DK