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 /
avatar image
0
Question by kayloh · Jun 03, 2013 at 06:07 PM · shooting

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;
 
     }
 
 }
 }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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"

Comment
Add comment · Share
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
1

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

Comment
Add comment · Show 2 · Share
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 kayloh · Jun 03, 2013 at 10:54 PM 0
Share

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

avatar image Mahtrok · Jun 04, 2013 at 08:31 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 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 avatar image

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


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