Quantcast
Channel: Questions in topic: "write-my-code"
Viewing all articles
Browse latest Browse all 92

how do you switch guns from single shot to full auto?

$
0
0
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; } } }

Viewing all articles
Browse latest Browse all 92

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>