Hello I have a bit of problem.
I’ve managed to create a recoil for my gun.
But when i start the game , the gun automaticly snaps itself to the gunHolder gameobject (coordinates 0,0,0 )
When i shoot the gun animates the recoil.
I’ve tried replacing the transform.localPosition with transform.position and Vector3.zero with transform.position didn’t worked the gun still snaped itself to the 0,0,0 coordinates
Does anybody knows how to fix this ?
Here is my code feel free to use it.
[Header("Atributes")]
public float fireRate = 0.25f;
[Header("Recoil")]
public Vector2 kickMinMax = new Vector2(.05f, .2f);
public Vector2 recoilAngleMinMax = new Vector2(3, 5);
public float recoilMoveSettleTime = .1f;
public float recoilRotationSettleTime = .1f;
Vector3 recoilSmoothDampVelocity;
float recoilRotSmoothDampVelocity;
float recoilAngle;
float nextFire;
private void Update()
{
if (Input.GetMouseButton(0) && Time.time > nextFire)
{
Shoot();
}
//RECOIL//
transform.localPosition = Vector3.SmoothDamp(transform.localPosition, Vector3.zero, ref recoilSmoothDampVelocity, recoilMoveSettleTime);
recoilAngle = Mathf.SmoothDamp(recoilAngle, 0, ref recoilRotSmoothDampVelocity, recoilRotationSettleTime);
transform.localEulerAngles = Vector3.right * -recoilAngle;
}
void Shoot()
{
nextFire = Time.time + fireRate;
//RECOIL//
transform.localPosition -= Vector3.forward * Random.Range(kickMinMax.x, kickMinMax.y);
recoilAngle += Random.Range(recoilAngleMinMax.x, recoilAngleMinMax.y);
recoilAngle = Mathf.Clamp(recoilAngle, 0, 30);
}
}