Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Aug 05, 2013 at 05:08 AM by Peter G for the following reason:

Duplicate Question

avatar image
0
Question by knuckles209cp · Aug 05, 2013 at 05:08 AM · longfull

Full/semi auto

I want to have an option to make my gun full or semi auto( NOT in-game ) Sorry about the long code : var beingHeld : boolean = false; var outsideBox : GameObject; @HideInInspector var countToThrow : int = -1; @HideInInspector var playerTransform : Transform; @HideInInspector 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; 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 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 muzzelFlash : GameObject; var gunbobAmountX : float = 0.5; var gunbobAmountY : float = 0.5; var currentGunbobX : float; var currentGunbobY : float; var gunModelObjects : GameObject[]; var reloading : boolean = false; var reloadAnimation : Animation; var reloadAnimationString : String; var reloadSound : AudioSource; var clipSize : int = 25; var currentClip : int = 25; var maxExtraAmmo : int = 100; var currentExtraAmmo : int = 100; var ammoType : int = 0; var bulletHudTexture : Texture; var ammoCountRect : Rect = Rect(25,25,50,25); var ammoStartX : int = 100; var ammoY : int = 25; var ammoSize : Vector2 = Vector2(10,25); var ammoSpacing : int = 4; var ammoDecorationHudRect : Rect = Rect(25,50,50,25); var ammoDecorationTexture : Texture; var sprintAnimation : Animation; var walkAnimation : Animation; var sprintString : String; var idleString : String; var Arms : GameObject; var IdleAnimation : Animation; function Awake () { countToThrow = -1; playerTransform = GameObject.FindWithTag("Player").transform; playerMovementScript = GameObject.FindWithTag("Player").GetComponent(PlayerMovementScript); cameraObject = GameObject.FindWithTag("MainCamera"); } function LateUpdate () { if (currentClip > clipSize) currentClip = clipSize; if (currentExtraAmmo > maxExtraAmmo) currentExtraAmmo = maxExtraAmmo; if (currentClip < 0) currentClip = 0; if (currentExtraAmmo < 0) currentExtraAmmo = 0; if (beingHeld) { if (!reloading && Input.GetButtonDown("Reload") && currentClip < clipSize && currentExtraAmmo > 0) { reloading = true; reloadAnimation.Play(reloadAnimationString); reloadSound.Play(); } if (!reloading && Input.GetButtonDown("Fire1") && currentClip == 0 && currentExtraAmmo > 0) { reloading = true; reloadAnimation.Play(reloadAnimationString); reloadSound.Play(); } if (reloading && !reloadAnimation.IsPlaying(reloadAnimationString)) { if (currentExtraAmmo >= clipSize - currentClip) { currentExtraAmmo -= clipSize - currentClip; currentClip = clipSize; } if (currentExtraAmmo < clipSize - currentClip) { currentClip += currentExtraAmmo; currentExtraAmmo = 0; } reloading = false; } for(var modelObject : GameObject in gunModelObjects) { modelObject.layer = 8; } rigidbody.useGravity = false; outsideBox.GetComponent(Collider).enabled = false; currentGunbobX = Mathf.Sin(cameraObject.GetComponent(MouseLookScript).headbobStepCounter) * gunbobAmountX * racioHipHold; currentGunbobY = Mathf.Cos(cameraObject.GetComponent(MouseLookScript).headbobStepCounter * 2) * gunbobAmountY * racioHipHold; var holdMuzzelFlash : GameObject; var holdSound : GameObject; if (Input.GetButton("Fire1") && currentClip > 0 && !reloading) { if (waitTilNextFire <= 0) { currentClip -= 1; if (bullet) Instantiate(bullet,bulletSpawn.transform.position, bulletSpawn.transform.rotation); if (bulletSound) holdSound = Instantiate(bulletSound, bulletSpawn.transform.position, bulletSpawn.transform.rotation); if (muzzelFlash) holdMuzzelFlash = Instantiate(muzzelFlash, 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; waitTilNextFire = 1; } } waitTilNextFire -= Time.deltaTime * fireSpeed; if (holdSound) holdSound.transform.parent = transform; if (holdMuzzelFlash) holdMuzzelFlash.transform.parent = transform; currentRecoilZPos = Mathf.SmoothDamp( currentRecoilZPos, 0, currentRecoilZPosV, recoilRecoverTime); cameraObject.GetComponent(MouseLookScript).currentTargetCameraAngle = zoomAngle; if (Input.GetButton("Fire2") && !reloading){ cameraObject.GetComponent(MouseLookScript).currentAimRacio = aimRacio; racioHipHold = Mathf.SmoothDamp(racioHipHold, 0, racioHipHoldV, hipToAimSpeed);} if (Input.GetButton("Fire2") == false || reloading){ 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 + currentGunbobX, holdHeight * racioHipHold + 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) { for(var modelObject : GameObject in gunModelObjects) { modelObject.layer = 0; } 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; } } } function OnGUI () { if (beingHeld) { for (var i : int = 1; i <= currentClip; i++) { GUI.DrawTexture(Rect(ammoStartX + ((i - 1) * (ammoSize.x + ammoSpacing)), ammoY, ammoSize.x, ammoSize.y), bulletHudTexture); } GUI.Label(ammoCountRect, currentExtraAmmo.ToString()); if (ammoDecorationTexture) GUI.DrawTexture( ammoDecorationHudRect, ammoDecorationTexture); } } function Update () { if(Input.GetButtonDown("Sprint")) sprintAnimation.Play(sprintString); else if(Input.GetButtonUp("Sprint")) walkAnimation.Play(idleString); if(beingHeld) Arms.renderer.enabled = true; else Arms.renderer.enabled = false; }

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Peter G · Aug 05, 2013 at 05:09 AM 0
Share

http://answers.unity3d.com/questions/31855/shooting-automatic-guns.html

http://answers.unity3d.com/questions/46334/making-an-automatic-weapon-from-fps-tutorial.html?sort=oldest

To make it toggle, just use the general idea of the previous ideas, and add an if statement that checks to see if we're in auto or semi-auto

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Look not responsive enough 0 Answers

How does code licensing for Unity works? What happens when I post for evaluation / showcase? 1 Answer

Instantiate as child 3 Answers

Scripting a Konami Code 1 Answer

App made with unity got rejected because of crash? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges