FPS 1.12 and recoil

problem is i cant control recoil or fire rate
here’s my code:

var beingHeld : boolean;
var outSideBox : GameObject;
@HideInInspector
var countToThrow : int = -1;
var playerTransform : Transform;
var playerMovementScript : PlayerMovementScript;
@HideInInspector
var CameraObject : GameObject;
@HideInInspector
var TargetXRotation : float;
@HideInInspector
var TargetYRotation : float;
@HideInInspector
var TargetXRotationV : float;
@HideInInspector
var TargetYRotationV : float;
var RotateSpeed : float = 0.3;
var HoldHight : float = -0.3;
var HoldSide : float = 0.5;
var RacioHipHold : float = 1;
var HipToAimSpeed : float = 0.2;
@HideInInspector
var RacioHipHoldV : float;
var AimRacio : float = 0.6;
var ZoomAngle : float = 30;
var FireSpeed : float = 15;
var WaitTilNextFire : float = 0;
var Bullet : GameObject;
var BulletSpawn : GameObject;
var ShootAngleRandomizationAiming : float = 5;
var ShootAngleRandomizationHip : float = 15;
var CurrentRecoilAmount : float = 0.5;
var RecoilRecoverTime : float = 0.2;
@HideInInspector
var CurrentRecoilZPosition : float;
@HideInInspector
var CurrentRecoilZPositionV : float;
var BulletSound : GameObject;


function LateUpdate()
{
if (beingHeld)
{
rigidbody.useGravity = false;
outSideBox.GetComponent(Collider).enabled = false;

var HoldSound : GameObject;

if (Input.GetButton("Fire1"))
{
  if (WaitTilNextFire <= 0);
  if (Bullet);
  Instantiate(Bullet,BulletSpawn.transform.position,BulletSpawn.transform.rotation);
  WaitTilNextFire = 1;
  WaitTilNextFire -= Time.deltaTime * FireSpeed;
TargetXRotation += (Random.value - 0.5) * Mathf.Lerp(ShootAngleRandomizationAiming,ShootAngleRandomizationHip,RacioHipHold);
TargetYRotation += (Random.value - 0.5) * Mathf.Lerp(ShootAngleRandomizationAiming,ShootAngleRandomizationHip,RacioHipHold);
CurrentRecoilZPosition -= CurrentRecoilAmount;
if (BulletSound);
HoldSound = Instantiate(BulletSound,BulletSpawn.transform.position,BulletSpawn.transform.rotation);
}
if (HoldSound)
{
HoldSound.transform.parent = transform;
}

CurrentRecoilZPosition = Mathf.SmoothDamp(CurrentRecoilZPosition, 0,CurrentRecoilZPositionV,RecoilRecoverTime);
CameraObject.GetComponent(MouseLookScript).CurrentTargetCameraAngle = ZoomAngle;

if (Input.GetButton("Fire2"))
{
RacioHipHold = Mathf.SmoothDamp(RacioHipHold,0,RacioHipHoldV,HipToAimSpeed);
CameraObject.GetComponent(MouseLookScript).CurrentAimRacio = AimRacio;
}

if (Input.GetButton("Fire2") == false)
{
RacioHipHold = Mathf.SmoothDamp(RacioHipHold,1,RacioHipHoldV,HipToAimSpeed);
CameraObject.GetComponent(MouseLookScript).CurrentAimRacio = 1;
}

transform.position = CameraObject.transform.position + (Quaternion.Euler(0,TargetYRotation,0) * Vector3(HoldSide * RacioHipHold, HoldHight * RacioHipHold, 0) + Quaternion.Euler(TargetXRotation,TargetYRotation,0) * Vector3(0,0,CurrentRecoilZPosition));

TargetXRotation = Mathf.SmoothDamp( TargetXRotation, CameraObject.GetComponent(MouseLookScript).XRotation, TargetXRotationV, RotateSpeed);
TargetYRotation = Mathf.SmoothDamp( TargetYRotation, CameraObject.GetComponent(MouseLookScript).YRotation, TargetYRotationV, RotateSpeed);
transform.rotation = Quaternion.Euler(TargetXRotation, TargetYRotation, 0);
}

if (!beingHeld)
{
rigidbody.useGravity = true;
outSideBox.GetComponent(Collider).enabled = true;

countToThrow -= 1;
if (countToThrow == 0)
rigidbody.AddRelativeForce(0,playerMovementScript.throwGunUpForce,playerMovementScript.throwGunForwardForce);
if (Vector3.Distance(transform.position,playerTransform.position) < playerMovementScript.distToPickUpGun  Input.GetButtonDown("Use Key") playerMovementScript.waitFrameForSwitchGuns <= 0)
{
playerMovementScript.currentGun.GetComponent(GunScript).beingHeld = false;
playerMovementScript.currentGun.GetComponent(GunScript).countToThrow = 2;
playerMovementScript.currentGun = gameObject;
beingHeld = true;
TargetYRotation = CameraObject.GetComponent(MouseLookScript).YRotation - 100;
playerMovementScript.waitFrameForSelection = 2;
}
}
}
function Awake ()
{
countToThrow = -1;
playerTransform = GameObject.FindWithTag("Player").transform;
playerMovementScript = GameObject.FindWithTag("Player").GetComponent(PlayerMovementScript);
CameraObject = GameObject.FindWithTag("MainCamera");
}

Try this:

var beingHeld : boolean;
var outSideBox : GameObject;
@HideInInspector
var countToThrow : int = -1;
var playerTransform : Transform;
var playerMovementScript : PlayerMovementScript;
@HideInInspector
var CameraObject : GameObject;
@HideInInspector
var TargetXRotation : float;
@HideInInspector
var TargetYRotation : float;
@HideInInspector
var TargetXRotationV : float;
@HideInInspector
var TargetYRotationV : float;
var RotateSpeed : float = 0.3;
var HoldHight : float = -0.3;
var HoldSide : float = 0.5;
var RacioHipHold : float = 1;
var HipToAimSpeed : float = 0.2;
@HideInInspector
var RacioHipHoldV : float;
var AimRacio : float = 0.6;
var ZoomAngle : float = 30;
var FireSpeed : float = 15;
var WaitTilNextFire : float = 0;
var Bullet : GameObject;
var BulletSpawn : GameObject;
var ShootAngleRandomizationAiming : float = 5;
var ShootAngleRandomizationHip : float = 15;
var CurrentRecoilAmount : float = 0.5;
var RecoilRecoverTime : float = 0.2;
@HideInInspector
var CurrentRecoilZPosition : float;
@HideInInspector
var CurrentRecoilZPositionV : float;
var BulletSound : GameObject;


function LateUpdate()
{
if (beingHeld)
{
rigidbody.useGravity = false;
outSideBox.GetComponent(Collider).enabled = false;

var HoldSound : GameObject;

if (Input.GetButton("Fire1"))
{
  if (WaitTilNextFire <= 0)
  {
  if (Bullet);
  Instantiate(Bullet,BulletSpawn.transform.position,BulletSpawn.transform.rotation);
  WaitTilNextFire = 1;
  WaitTilNextFire -= Time.deltaTime * FireSpeed;
TargetXRotation += (Random.value - 0.5) * Mathf.Lerp(ShootAngleRandomizationAiming,ShootAngleRandomizationHip,RacioHipHold);
TargetYRotation += (Random.value - 0.5) * Mathf.Lerp(ShootAngleRandomizationAiming,ShootAngleRandomizationHip,RacioHipHold);
CurrentRecoilZPosition -= CurrentRecoilAmount;
if (BulletSound);
HoldSound = Instantiate(BulletSound,BulletSpawn.transform.position,BulletSpawn.transform.rotation);
}
}
if (HoldSound)
{
HoldSound.transform.parent = transform;
}

CurrentRecoilZPosition = Mathf.SmoothDamp(CurrentRecoilZPosition, 0,CurrentRecoilZPositionV,RecoilRecoverTime);
CameraObject.GetComponent(MouseLookScript).CurrentTargetCameraAngle = ZoomAngle;

if (Input.GetButton("Fire2"))
{
RacioHipHold = Mathf.SmoothDamp(RacioHipHold,0,RacioHipHoldV,HipToAimSpeed);
CameraObject.GetComponent(MouseLookScript).CurrentAimRacio = AimRacio;
}

if (Input.GetButton("Fire2") == false)
{
RacioHipHold = Mathf.SmoothDamp(RacioHipHold,1,RacioHipHoldV,HipToAimSpeed);
CameraObject.GetComponent(MouseLookScript).CurrentAimRacio = 1;
}

transform.position = CameraObject.transform.position + (Quaternion.Euler(0,TargetYRotation,0) * Vector3(HoldSide * RacioHipHold, HoldHight * RacioHipHold, 0) + Quaternion.Euler(TargetXRotation,TargetYRotation,0) * Vector3(0,0,CurrentRecoilZPosition));

TargetXRotation = Mathf.SmoothDamp( TargetXRotation, CameraObject.GetComponent(MouseLookScript).XRotation, TargetXRotationV, RotateSpeed);
TargetYRotation = Mathf.SmoothDamp( TargetYRotation, CameraObject.GetComponent(MouseLookScript).YRotation, TargetYRotationV, RotateSpeed);
transform.rotation = Quaternion.Euler(TargetXRotation, TargetYRotation, 0);
}

if (!beingHeld)
{
rigidbody.useGravity = true;
outSideBox.GetComponent(Collider).enabled = true;

countToThrow -= 1;
if (countToThrow == 0)
rigidbody.AddRelativeForce(0,playerMovementScript.throwGunUpForce,playerMovementScript.throwGunForwardForce);
if (Vector3.Distance(transform.position,playerTransform.position) < playerMovementScript.distToPickUpGun  Input.GetButtonDown("Use Key") playerMovementScript.waitFrameForSwitchGuns <= 0)
{
playerMovementScript.currentGun.GetComponent(GunScript).beingHeld = false;
playerMovementScript.currentGun.GetComponent(GunScript).countToThrow = 2;
playerMovementScript.currentGun = gameObject;
beingHeld = true;
TargetYRotation = CameraObject.GetComponent(MouseLookScript).YRotation - 100;
playerMovementScript.waitFrameForSelection = 2;
}
}
}
function Awake ()
{
countToThrow = -1;
playerTransform = GameObject.FindWithTag("Player").transform;
playerMovementScript = GameObject.FindWithTag("Player").GetComponent(PlayerMovementScript);
CameraObject = GameObject.FindWithTag("MainCamera");
}

well now it only shoots one bullet :confused:

what should wait til next fire equal?

i’ve noticed after i’ve shot the wait til next fire gets set to : 0.6748539

or around there it seems to be randomized.

never mind i just put the WaitTilNextFire -= Time.deltaTime * FireSpeed

outside the brackets that you put thanks for your help :wink: