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 george3d2011 · Jul 12, 2013 at 10:44 AM · bulletshoot

C# ShootController()

hey guys. I have a problem with my ShootController(). Specifically when I play the game and press ("Fire1") for shooting my weapon shoots 3 bullets with one click. I cant imagine the problem So I decided to post it here.

Here is my script (I removed the parts which has nothing to do with Shooting):

 //Sound variables
 
 public GameObject bulletSound;
 
 public GameObject holdSound;
 
 public void Update()
     {
         ShootController();
     }
 
 public void ShootController()
     {
         if (Input.GetButton("Fire1"))
         {
             if (shoottime <= Time.time)
             {
                 shoottime = Time.time + CurrentWeapon.firerate;
                 CurrentRecoil1 += new Vector3(CurrentWeapon.RecoilRotation.x, Random.Range(-CurrentWeapon.RecoilRotation.y, CurrentWeapon.RecoilRotation.y));
                 CurrentRecoil3 += new Vector3(Random.Range(-CurrentWeapon.RecoilKickback.x, CurrentWeapon.RecoilKickback.x), Random.Range(-CurrentWeapon.RecoilKickback.y, CurrentWeapon.RecoilKickback.y), CurrentWeapon.RecoilKickback.z);
             }
             
             GameObject inst_bullet = Instantiate(CurrentWeapon.Bullet, CurrentWeapon.Spawnpoint.position, CurrentWeapon.Spawnpoint.rotation) as GameObject;
             inst_bullet.rigidbody.AddRelativeForce(Vector3.forward, ForceMode.Impulse);
             Physics.IgnoreCollision(inst_bullet.collider, CharCont);
             inst_bullet.GetComponent<BulletManager>().damage = CurrentWeapon.weaponDamage;
             if (bulletSound)
             {
                 holdSound = Instantiate(bulletSound, CurrentWeapon.Spawnpoint.transform.position, CurrentWeapon.Spawnpoint.transform.rotation) as GameObject;
             }
             if (holdSound)
             {
                 holdSound.transform.parent = transform;
             }
         }
     }
 public void RecoilController()
     {
         CurrentRecoil1 = Vector3.Lerp(CurrentRecoil1, Vector3.zero, 0.1f);
         CurrentRecoil2 = Vector3.Lerp(CurrentRecoil2, CurrentRecoil1, 0.1f);
         CurrentRecoil3 = Vector3.Lerp(CurrentRecoil3, Vector3.zero, 0.1f);
         CurrentRecoil4 = Vector3.Lerp(CurrentRecoil4, CurrentRecoil3, 0.1f);
          RecoilHolder.localEulerAngles = CurrentRecoil2;
         RecoilHolder.localPosition = CurrentRecoil4;
     }
 [System.Serializable]
 public class WeaponInfo
 {
     public string name = "Weapon";
     public float firerate = 0.1f;
     public float bulletSpeed = 1;
     public float weaponDamage = 10;
     public Transform WeaponTransform;
     public Transform Spawnpoint;
      public Vector3 RecoilRotation;
     public Vector3 RecoilKickback;
     public GameObject Bullet;
      public int CurrentScope;
     public List<WeaponScope> Scopes = new List<WeaponScope>();
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ReliCWeb · Jul 12, 2013 at 02:20 PM

Here's some pseudocode of how I did a machinegun in a recent prototype (m_fireRate = 0.1f):

 if (Input.GetButton("Fire1"))
 {
     if (Time.time - m_lastFire > m_fireRate)
     {
         GameObject inst_bullet = Instantiate(CurrentWeapon.Bullet, CurrentWeapon.Spawnpoint.position, CurrentWeapon.Spawnpoint.rotation) as GameObject;
         inst_bullet.rigidbody.AddRelativeForce(Vector3.forward, ForceMode.Impulse);
         Physics.IgnoreCollision(inst_bullet.collider, CharCont);
         inst_bullet.GetComponent<BulletManager>().damage = CurrentWeapon.weaponDamage;
         if (bulletSound)
         {
             holdSound = Instantiate(bulletSound, CurrentWeapon.Spawnpoint.transform.position, CurrentWeapon.Spawnpoint.transform.rotation) as GameObject;
         }
         if (holdSound)
         {
             holdSound.transform.parent = transform;
         }
 
         // Record fire time
         m_lastFire = Time.time;
     }
 }

EDIT: Modified to include your bullet spawning code.

Comment
Add comment · Show 7 · 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 george3d2011 · Jul 13, 2013 at 11:49 AM 0
Share

:\ dude this works exactly as $$anonymous$$e :\

avatar image ReliCWeb · Jul 13, 2013 at 01:09 PM 0
Share

Sorry, wasn't sure what exactly you wanted. This shoots one bullet on a single click (assu$$anonymous$$g you release the button before m_fireRate), and allows continuous shooting if you hold the button down.

avatar image george3d2011 · Jul 13, 2013 at 03:41 PM 0
Share

oh.. So, I have made a mistake.. this is what i want to do.. I tried to do it with your way but errors was being appeared. So I changed it a little bit :\ Could you replace "//shoot bullet code here, decrease bullets, etc" with codes?

avatar image ReliCWeb · Jul 13, 2013 at 03:52 PM 0
Share

Ok, I edited my original post to include your bullet spawning code. Also, after looking at your code again, yours should work if you put your bullet spawning code inside the "if (shoottime <= Time.time)" check of ShootController().

avatar image george3d2011 · Jul 14, 2013 at 08:46 AM 0
Share

error CS0103: The name `m_lastFire' does not exist in the current context

What should I do now? is there any value to assing in order to make m_lastFire exist in the current context?

Show more comments
avatar image
0

Answer by amphoterik · Jul 12, 2013 at 11:49 AM

I don't know what the value of CurrentWeapon.fireRate is, but you would want to use a different input method:

 if (Input.GetButtonDown("Fire1"))

GetButtonDown returns true only if the button was just pressed. The one you used is GetButton which returns true every frame if the player holds the button.

Comment
Add comment · Show 3 · 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 george3d2011 · Jul 12, 2013 at 01:53 PM 0
Share

yeah I changed it.. Now Im shooting One bullet/click i cant Shoot continuesly with my weapon :\ Would you like my full script? just to see what is going on with the other variables?

avatar image amphoterik · Jul 12, 2013 at 01:55 PM 0
Share

Wait, what are you trying to do? I thought the problem is that it shot continuously. What is the value of CurrentWeapon.fireRate?

avatar image george3d2011 · Jul 12, 2013 at 02:08 PM 0
Share

Well.. Im making an FPS Game and i installed a ShootController Statement.. everything is fine but when i press FIRE1 it shoots 3 bullets with 1 click..

I also posted the full script (just wait to be verified)

avatar image
0

Answer by george3d2011 · Jul 12, 2013 at 02:20 PM

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class PlayerController : MonoBehaviour { public CharacterController CharCont; public CharacterMotor CharMotor; //Holder for Weapons public Transform WalkAnimationHolder; public Transform JumpAnimationHolder; public Transform ADSHolder; public Transform SwayHolder; public Transform RecoilHolder; public WalkingState walkingstate = WalkingState.Idle; public float VelocityMagnitude; public float WalkSpeed; public float RunSpeed; public bool WasStanding;

 //Player Variables
 public bool IsAiming;
  public WeaponInfo CurrentWeapon;
 public List<WeaponInfo> WeaponList = new List<WeaponInfo>();
  public Vector3 CurrentRecoil1;
 public Vector3 CurrentRecoil2;
 public Vector3 CurrentRecoil3;
 public Vector3 CurrentRecoil4;
  private float shoottime = 0;
 
 //Sounds
 public GameObject bulletSound;
 public GameObject holdSound;
 
 void Start()
 {
     CurrentWeapon = WeaponList[0];
 }
 
 public void Update()
 {
     ShootController();
 }
    
 public void FixedUpdate()
 {
     AnimationController();
     SwayController();
     SpeedController();
     RecoilController();
     ADSController();
     VelocityMagnitude = CharCont.velocity.magnitude;
 }
 
 public void SpeedController()
 {
     if ((Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0) && VelocityMagnitude > 0)
     {
         if (Input.GetButton("Run"))
         {
             walkingstate = WalkingState.Running;
             CharMotor.movement.maxForwardSpeed = RunSpeed;
             CharMotor.movement.maxSidewaysSpeed = RunSpeed;
             CharMotor.movement.maxBackwardsSpeed = RunSpeed / 2;
         }
         else
         {
             walkingstate = WalkingState.Walking;
             CharMotor.movement.maxForwardSpeed = WalkSpeed;
             CharMotor.movement.maxSidewaysSpeed = WalkSpeed;
             CharMotor.movement.maxBackwardsSpeed = WalkSpeed / 2;
         }
     }
     else
     {
         walkingstate = WalkingState.Idle;
     }
 }
    
 public void AnimationController()
 {
     if(WasStanding && !CharCont.isGrounded)
     {
         WasStanding = false;
         WalkAnimationHolder.animation.Play("animationSTANDINGJUMP");
     }
     else if (!WasStanding && CharCont.isGrounded)
     {
         WasStanding = true;
         WalkAnimationHolder.animation.Play("animationSTANDINGJUMPLANDING");
     }
     if(walkingstate == WalkingState.Running)
     {
         WalkAnimationHolder.animation["animationRUN"].speed = VelocityMagnitude / RunSpeed * 1.2f;
         WalkAnimationHolder.animation.CrossFade("animationRUN", 0.2f);
     }
     else if (walkingstate == WalkingState.Walking)
     {
         WalkAnimationHolder.animation["animationWALK"].speed = VelocityMagnitude / WalkSpeed * 1.2f;
         WalkAnimationHolder.animation.CrossFade("animationWALK", 0.2f);
     }
     else
     {
         WalkAnimationHolder.animation.CrossFade("animationIDLE", 0.2f);
     }
  }
    
 public void SwayController()
     {
        
     }
 
 public void ShootController()
 {
     if (Input.GetButton("Fire1") && walkingstate != WalkingState.Running)
     {
         if (shoottime <= Time.time)
         {
             shoottime = Time.time + CurrentWeapon.firerate;
             CurrentRecoil1 += new Vector3(CurrentWeapon.RecoilRotation.x, Random.Range(-CurrentWeapon.RecoilRotation.y, CurrentWeapon.RecoilRotation.y));
             CurrentRecoil3 += new Vector3(Random.Range(-CurrentWeapon.RecoilKickback.x, CurrentWeapon.RecoilKickback.x), Random.Range(-CurrentWeapon.RecoilKickback.y, CurrentWeapon.RecoilKickback.y), CurrentWeapon.RecoilKickback.z);
         }
         
         GameObject inst_bullet = Instantiate(CurrentWeapon.Bullet, CurrentWeapon.Spawnpoint.position, CurrentWeapon.Spawnpoint.rotation) as GameObject;
         inst_bullet.rigidbody.AddRelativeForce(Vector3.forward, ForceMode.Impulse);
         Physics.IgnoreCollision(inst_bullet.collider, CharCont);
         inst_bullet.GetComponent<BulletManager>().damage = CurrentWeapon.weaponDamage;
         if (bulletSound)
         {
             holdSound = Instantiate(bulletSound, CurrentWeapon.Spawnpoint.transform.position, CurrentWeapon.Spawnpoint.transform.rotation) as GameObject;
         }
         if (holdSound)
         {
             holdSound.transform.parent = transform;
         }
     }
 }
 
 public void RecoilController()
 {
     CurrentRecoil1 = Vector3.Lerp(CurrentRecoil1, Vector3.zero, 0.1f);
     CurrentRecoil2 = Vector3.Lerp(CurrentRecoil2, CurrentRecoil1, 0.1f);
     CurrentRecoil3 = Vector3.Lerp(CurrentRecoil3, Vector3.zero, 0.1f);
     CurrentRecoil4 = Vector3.Lerp(CurrentRecoil4, CurrentRecoil3, 0.1f);
      RecoilHolder.localEulerAngles = CurrentRecoil2;
     RecoilHolder.localPosition = CurrentRecoil4;
 }
 
 public void ADSController()
 {
     if (Input.GetButton("Fire2"))
     {
         IsAiming = true;
         ADSHolder.localPosition = Vector3.Lerp(ADSHolder.localPosition, CurrentWeapon.Scopes[CurrentWeapon.CurrentScope = 0].adsposition, 0.25f);
     }
     else
     {
         IsAiming = false;
         ADSHolder.localPosition = Vector3.Lerp(ADSHolder.localPosition, Vector3.zero, 0.25f);
     }
 }

}

public enum WalkingState { Idle, Walking, Running, }

[System.Serializable] public class WeaponInfo { public string name = "Weapon"; public float firerate = 0.1f; public float bulletSpeed = 1; public float weaponDamage = 10; public Transform WeaponTransform; public Transform Spawnpoint; public Vector3 RecoilRotation; public Vector3 RecoilKickback; public GameObject Bullet; public int CurrentScope; public List Scopes = new List(); }

[System.Serializable] public class WeaponScope { public string name; public float fov; public Vector3 adsposition; public Transform scopetransform; }

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

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

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

Related Questions

Problem creating a 2D scroller shooting game 2 Answers

Bullet not moving 1 Answer

Troubles With A Shoot Script 1 Answer

Need a script for a gun that shoots bullet using raycast 2 Answers

Destroy Turret with machine Gun 0 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