- Home /
I'm trying to make recoil on my gun but the bullet spawn won't move with it
The gun script is
#pragma strict
function Start () {
}
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 holdHeight : float = -0.5;
var holdSide : float = 0.5;
var racioHipHold : float = 1;
var hipToAimSpeed : float = 0.1;
@HideInInspector
var racioHipHoldV : float;
var aimRacio : float = 0.4;
var zoomAngle : float = 30;
var fireSpeed : float = 15;
@HideInInspector
var waitTillNextFire : float = 0;
var bullet : GameObject;
var bulletSpawn : GameObject;
var shootAngleRandomizationAiming : float = 5;
var shootAngleRandomizationNotAiming : float = 15;
var recoilAmount : float = 0.5;
var recoilRecoverTime :float = 0.2;
@HideInInspector
var currentRecoilZPos : float;
@HideInInspector
var currentRecoilZPosV : float;
function Update ()
{
if (Input.GetButton("Fire1"))
{
if (waitTillNextFire <= 0)
{
if(bullet)
Instantiate(bullet, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
targetXRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, racioHipHold);
targetYRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, racioHipHold);
currentRecoilZPos += recoilAmount;
waitTillNextFire = 1;
}
}
waitTillNextFire -= Time.deltaTime * fireSpeed;
currentRecoilZPos = Mathf.SmoothDamp( currentRecoilZPos, 0, currentRecoilZPosV, recoilRecoverTime);
cameraObject.GetComponent(Mouselookscript).currentTargetCameraAngle = zoomAngle;
if(Input.GetButton("Fire2"))
{
cameraObject.GetComponent(Mouselookscript).currentAimRacio = aimRacio;
racioHipHold = Mathf.SmoothDamp(racioHipHold, 0, racioHipHoldV, hipToAimSpeed);}
if(Input.GetButton("Fire2") == false)
{
cameraObject.GetComponent(Mouselookscript).currentAimRacio = 1;
racioHipHold = Mathf.SmoothDamp(racioHipHold, 1, racioHipHoldV, hipToAimSpeed);}
transform.position = cameraObject.transform.position + (Quaternion.Euler(0,targetYRotation,0) * Vector3(holdSide * racioHipHold, holdHeight * racioHipHold, 0) + Quaternion.Euler(targetXRotation, targetYRotation, 0) * Vector3(0,0, currentRecoilZPos));
targetXRotation = Mathf.SmoothDamp( targetXRotation, cameraObject.GetComponent(Mouselookscript).xRotation, targetXRotation, rotateSpeed);
targetYRotation = Mathf.SmoothDamp( targetYRotation, cameraObject.GetComponent(Mouselookscript).yRotation, targetYRotation, rotateSpeed);
transform.rotation = Quaternion.Euler(targetXRotation, targetYRotation, 0);
}
Are you sure the bulletspawn Gameobject is parented properly?
You could also try to instantiate the bullet using the gun object not the bulletspawn one.
Answer by ExTheSea · Jun 09, 2013 at 09:55 AM
I just tried the package and it should be better when you increase the RotateSpeed variable to something like 0.5 . If you increase it even more the gun start spinning around the player.
Answer by TheRichardGamer · Jun 05, 2013 at 10:04 AM
You could make a recoil animation, I'm using one and it works just fine :)
In his case an animation is not really appropiate because the op is looking for a random spread.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Animation & Script Help 2 Answers
Smooth loop animations? 4 Answers
RayCast Shooting not working 0 Answers