- Home /
how do you switch guns from single shot to full auto?
My friend and i are just making a basic FPS game (i know very cliche) and he's made a revolver and a Thompson using blender, I've made the revolver shoot with every click but when i switch weapons to the thompson it continues to be single shot.
how can i make it that the revolver stays single shot and the thompson to change to fully automatic whenever i switch guns?
here's my gunscript I've been using.... sorry if it seems messy i'm still an amateur :S
var beingHeld: boolean = false;
var outsideBox: GameObject;
var countToThrow: int = -1;
@HideInInspector
var playerTransform : Transform;
@HideInInspector
var playerMovementScript : playermovement;
@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 holdHeight :float = -0.5;
var holdSlide : float = 0.5;
var ratioHipHold : float = 1;
var hipToAimSpeed : float = 0.1;
@HideInInspector
var ratioHipHoldV : float;
var aimRatio : float = 0.4;
var zoomAngle : float = 30;
var fireSpeed : float = 15;
@HideInInspector
var waitTilNextFire : 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;
var bulletSound : GameObject;
var muzzleFlash : GameObject;
var gunbobAmountX : float = 0.5;
var gunbobAmountY : float = 0.5;
var currentGunbobX : float;
var currentGunbobY : float;
function Awake ()
{
countToThrow = -1;
playerTransform = GameObject.FindWithTag("Player").transform;
playerMovementScript = GameObject.FindWithTag("Player").GetComponent(playermovement);
cameraObject = GameObject.FindWithTag("MainCamera");
}
function LateUpdate ()
{
if (beingHeld)
{
rigidbody.useGravity = false;
outsideBox.GetComponent(Collider).enabled = false;
currentGunbobX = Mathf.Sin(cameraObject.GetComponent(mouselookscript).headbobStepCounter) * gunbobAmountX * ratioHipHold;
currentGunbobY = Mathf.Cos(cameraObject.GetComponent(mouselookscript).headbobStepCounter * 2) * gunbobAmountY * -1 * ratioHipHold;
var holdmuzzleFlash : GameObject;
var holdSound : GameObject;
if (Input.GetButtonDown("Fire1"))
{
if (waitTilNextFire <= 0)
{
if (bullet)
Instantiate(bullet,bulletspawn.transform.position, bulletspawn.transform.rotation);
if (bulletSound)
holdSound = Instantiate(bulletSound,bulletspawn.transform.position, bulletspawn.transform.rotation);
if (muzzleFlash)
holdmuzzleFlash = Instantiate(muzzleFlash,bulletspawn.transform.position, bulletspawn.transform.rotation);
targetXRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, ratioHipHold);
targetYRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, ratioHipHold);
currentRecoilZPos -= recoilAmount;
waitTilNextFire = 6;
}
}
waitTilNextFire -= Time.deltaTime * fireSpeed;
if (holdSound)
holdSound.transform.parent = transform;
if (holdmuzzleFlash)
holdmuzzleFlash.transform.parent = transform;
currentRecoilZPos = Mathf.SmoothDamp( currentRecoilZPos, 0, currentRecoilZPosV, recoilRecoverTime);
cameraObject.GetComponent(mouselookscript).currentTargetCameraAngle = zoomAngle;
if (Input.GetButton("Fire2")){
cameraObject.GetComponent(mouselookscript).currentAimRatio = aimRatio;
ratioHipHold = Mathf.SmoothDamp(ratioHipHold, 0, ratioHipHoldV, hipToAimSpeed);}
if (Input.GetButton("Fire2") == false){
cameraObject.GetComponent(mouselookscript).currentAimRatio = 1;
ratioHipHold = Mathf.SmoothDamp(ratioHipHold, 1, ratioHipHoldV, hipToAimSpeed);}
transform.position = cameraObject.transform.position + (Quaternion.Euler(0, targetYRotation,0) * Vector3(holdSlide * ratioHipHold + currentGunbobX, holdHeight * ratioHipHold + currentGunbobY, 0) + Quaternion.Euler(targetXRotation, targetYRotation,0) * Vector3(0,0,currentRecoilZPos));
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 - 180;
playerMovementScript.waitFrameForSwitchGuns = 2;
}
}
}
Answer by darthbator · Jun 03, 2013 at 06:24 PM
I didn't read through all your code but right off the bat I notice you are polling with GetButtonDown which only registers in the frame the button is down. You can change it to GetButton to return true as long as it's down. That will likely call the method to fast but you're going to want to poll the button using GetButton not GetButtonDown. Then you can have a timer to restrict how quickly the firing method of the gun will "cycle"
Answer by Mahtrok · Jun 03, 2013 at 06:47 PM
Detect which weapon is active
var magSize : int = 10; // Set the size of your magazine or the amount how many bullets should be spawned on one click
if(revolverIsUsed) {
// your single Shot routine
}
else if(thompsonIsUsed) {
// Spawn as many bullets as you have in your magazine on one click, but only 1 per second
for(var i : int = 0; i < magSize; i++) {
// Instantiate the Bullet, Play Sound, do whatever your singleshot is doing but inside the for loop
yield WaitForSeconds(1); // wait a small amount of time before spawning the next Bullet
}
}
Hope this helps, greetz
it didnt work :L i've tried to make a copy of the script and put it into my thompson with 1 peice of code altered... from "GetButtonDown" which makes it single shot to "Getbutton"... it works but whenever i try to switch weapons from a revolver to the thompson it just doesn't work... i get the error nullreferenceexception: object reference not set to an instance of an object... thanks anyway :P
NullReference always means that some Object you are trying to Access is not set or was destroyed. Try to find out what object is missing and why and try to avoid the Object is destroyed or that whatever tries to Access the object checks if it is null before.
Your answer
Follow this Question
Related Questions
Shoot. Just Shoot 5 Answers
Audio repeats 3 Answers
Bullet stops when new bullet instantiates pls help 3 Answers
Using Raycast shooting while using a ps4 controller? 0 Answers
Shoot projectile straight from player no matter the rotation. 1 Answer