Recoil script help

Hey guys how you doing :slight_smile:
I was wondering if you could help me out well basically i have created a simple recoil script which makes the camera go up and making it look like recoil. Now the only problem is that it goes up when i press or hold the F key but it wont come back down how can i solve this here is my script :

var playerCamera : GameObject;



function Update()

{

if(Input.GetKey(KeyCode.F)){

Recoil();

}

}



function Recoil()

{

playerCamera.transform.localEulerAngles -= Vector3.right;

}

Thank you in advance MCHALO THANK YOU :slight_smile:

Hey dude i have found a different way but its getting there. Basically at the moment it can go up and down but the problem is when i start the game i have already picked the set the original values of the position it starts of in. And if i shoot it will go up then the cooler kicks in and then it comes back down. Now if when it is going down how do i check if it has passed it original position on the X. lets say x was 1 and when its going down how do i check if its less then 1 so -1 if it set it back to 1. Now i have tried

 if(mainCameraPos.transform.localPosition <1){

then do something but that gives me a new problem

here is my script

var mainCameraObject: GameObject;

var timerStart : float;

var Cooler : float ;

var Speed: float;

var OriginalPos : Vector3;

var inputTimer : float;



function Start (){



 OriginalPos = transform.localPosition;



}



function Update()

{



if( timerStart > 0){



  timerStart -= Time.deltaTime;

  mainCameraObject.transform.localEulerAngles += OriginalPos ;

  



}





if( timerStart < 0){



   timerStart = 0;



}



if(Input.GetKey(KeyCode.F)){



  Recoil();

  

  timerStart = Cooler;

  



}



}







function Recoil()



{



 mainCameraObject.transform.localEulerAngles -= Vector3.right * Speed;



}

Well, youโ€™re doing this in a too-simple way. As it is, it only has functionality for moving the camera back- none for moving forward again. Try putting this stuff into a Coroutine-

function Recoil()
{
    recoiling = true;
    var currentTime : float = 0;
    var originalRotation : Quaternion = transform.localRotation;
    while (currentTime < recoilTime)
    {
        transform.localRotation = Quaternion.Slerp(originalRotation, recoilRotation, currentTime / recoilTime);
        currentTime += Time.deltaTime;
        yield;
    }
    currentTime = 0;
    while (currentTime < resetTime)
    {
        transform.localRotation = Quaternion.Slerp(recoilRotation, originalRotation, currentTime / resetTime);
        currentTime += Time.deltaTime;
        yield;
    }
    recoiling = false;
}

Where recoilTime and resetTime are floats which designate how long each stage should take, and recoilRotation is a Quaternion which tells it how far back to lean during the recoil action.

To activate it, use-

if(!recoiling && Input.GetKey(KeyCode.F))
{
    Recoil();
}

Check This

var recoilMod : Transform;
 var weapon : GameObject;
 var maxRecoil_x : float = -20;
 var recoilSpeed : float = 10;
 var recoil : float = 0.0;
  
 function Update()
 {
     if(Input.GetMouseButton(0))
     {
         //every time you fire a bullet, add to the recoil.. of course you can probably set a max recoil etc..
         recoil+= 0.25;
     }else
     {
     recoil = 0;
     }
  
     recoiling();
 }
  
 function recoiling()
 {
     if(recoil > 0)
     {
         var maxRecoil = Quaternion.Euler (maxRecoil_x, 0, 0);
         // Dampen towards the target rotation
         recoilMod.rotation = Quaternion.Slerp(recoilMod.rotation, maxRecoil, Time.deltaTime * recoilSpeed);
         weapon.transform.localEulerAngles.x = recoilMod.localEulerAngles.x;
         recoil -= Time.deltaTime;
     }
     else
     {
         recoil = 0;
         var minRecoil = Quaternion.Euler (0, 0, 0);
         // Dampen towards the target rotation
         recoilMod.rotation = Quaternion.Slerp(recoilMod.rotation, minRecoil,Time.deltaTime * recoilSpeed / 2);
         weapon.transform.localEulerAngles.x = recoilMod.localEulerAngles.x;
     }
 }