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 Blue Astronaut · Jan 25, 2014 at 10:38 PM · multiplayer

Help with a C# Gun script

HI! :D I am following a Unity multiplayer tutorial and I just finished his gun tutorial. It fires semi-automatic(it only fires once when you pull the trigger), but I want it to be full-auto. I've programmed a full-auto script in Javascript, but never c#. This is my code: using UnityEngine; using System.Collections; using System.Collections.Generic;

 public class Gun : MonoBehaviour {
     public float FireRate;
     private float nextFireTime; 
     public float MinDamage;
     public float MaxDamage;
     private float ActualDamage;
     private float FireTime;
     public float Range = 800;
     public Transform SpawnPoint;
     public string Name;
     public GameObject ImpactEffect;
     public Transform AimObj;
     public List<Sight> Sights = new List<Sight>();
     public int CurSight;
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if(Input.GetButtonDown("Fire1"))
             Fire();
         
         
         
         foreach(Sight s in  Sights)
         {
             if(Sights.IndexOf(s) == CurSight)
             {
                 s.Obj.SetActive(true);
             }
             else
             {
                 s.Obj.SetActive(false);
             }
         }
     }
     
     public void Fire()
     {
         if(FireTime <= Time.time)
         {
             FireTime = FireRate + Time.time;
             ActualDamage = Random.Range(MinDamage,MaxDamage);
             audio.Play();
             NetworkManager.Instance.MyPlayer.Manager.Client_DoSound(Name);
             RaycastHit hit;
             if(Physics.Raycast(SpawnPoint.position,SpawnPoint.forward,out hit,Range)){
                 if(hit.collider.rigidbody)
                 {
                     hit.collider.rigidbody.AddForceAtPosition((ActualDamage * 5) * transform.forward, hit.collider.transform.position);
                 }
                 UserPlayer hitter = hit.transform.root.GetComponent<UserPlayer>();
                 Network.Instantiate(ImpactEffect,hit.point,Quaternion.FromToRotation(hit.normal,Vector3.up),0);
                 if(hitter != null && hitter.MyPlayer.Team != NetworkManager.Instance.MyPlayer.Team)
                 {
                     hitter.networkView.RPC("Server_TakeDamge", RPCMode.All, ActualDamage);
                     hitter.networkView.RPC("FindHitter", RPCMode.All, NetworkManager.Instance.MyPlayer.PlayerName, Name);
                 }
             }
         }
         
     }
     
     
     void FixedUpdate()
     {
         if(Input.GetButton("Fire2"))
         {
             AimObj.transform.localPosition = Vector3.Lerp(AimObj.transform.localPosition,Sights[CurSight].AimPos, 0.25f);
         }
         else
         {
             AimObj.transform.localPosition = Vector3.Lerp(AimObj.transform.localPosition,Vector3.zero,0.25f);
         }
     }
 }
 
 [System.Serializable]
 public class Sight
 {
     public string Name;
     public GameObject Obj;
     public Vector3 AimPos;
 }

What would I add to this script and where? Would I add a delay timer or what? Any help is appreciated! Thanks in advance :D - Nikolai

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 Lockstep · Jan 25, 2014 at 10:50 PM 0
Share

Why don't you just use your working code? If you only want to convert it, then you can use this converter on the web.

2 Replies

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

Answer by HuskyPanda213 · Jan 25, 2014 at 10:48 PM

Ok I know a solution, use Input.GetButton(YourButton) instead of Input.GetButtonDown(YourButton). I will write some code, which will be auto and semi auto :). Just tweak in the inspector, also I know what tutorial your are following, if you decide to add ammo, make sure you reset when you respawn. :)

 using UnityEngine;
     using System.Collections; 
     using System.Collections.Generic;
     
     public class Gun : MonoBehaviour {
         public float FireRate;
         private float nextFireTime; 
         public float MinDamage;
         public float MaxDamage;
         private float ActualDamage;
         private float FireTime;
         public float Range = 800;
         public Transform SpawnPoint;
         public string Name;
         public GameObject ImpactEffect;
         public Transform AimObj;
         public List<Sight> Sights = new List<Sight>();
         public int CurSight;
         //Change in inspector for result.
         public FireMode MyFiremode = FireMode.Semi;
         
         // Use this for initialization
         void Start () {
             
         }
         
         // Update is called once per frame
         void Update ()
         {
             if (MyFiremode == FireMode.Semi) {
                 if (Input.GetButtonDown ("Fire1")){
                     Fire ();
                 }
             }
 
             if (MyFiremode == FireMode.Auto) {
                 if (Input.GetButton ("Fire1")){
                     Fire ();
                 }
             }
             
             
             foreach(Sight s in  Sights)
             {
                 if(Sights.IndexOf(s) == CurSight)
                 {
                     s.Obj.SetActive(true);
                 }
                 else
                 {
                     s.Obj.SetActive(false);
                 }
             }
         }
         
         public void Fire()
         {
             if(FireTime <= Time.time)
             {
                 FireTime = FireRate + Time.time;
                 ActualDamage = Random.Range(MinDamage,MaxDamage);
                 audio.Play();
                 NetworkManager.Instance.MyPlayer.Manager.Client_DoSound(Name);
                 RaycastHit hit;
                 if(Physics.Raycast(SpawnPoint.position,SpawnPoint.forward,out hit,Range)){
                     if(hit.collider.rigidbody)
                     {
                         hit.collider.rigidbody.AddForceAtPosition((ActualDamage * 5) * transform.forward, hit.collider.transform.position);
                     }
                     UserPlayer hitter = hit.transform.root.GetComponent<UserPlayer>();
                     Network.Instantiate(ImpactEffect,hit.point,Quaternion.FromToRotation(hit.normal,Vector3.up),0);
                     if(hitter != null && hitter.MyPlayer.Team != NetworkManager.Instance.MyPlayer.Team)
                     {
                         hitter.networkView.RPC("Server_TakeDamge", RPCMode.All, ActualDamage);
                         hitter.networkView.RPC("FindHitter", RPCMode.All, NetworkManager.Instance.MyPlayer.PlayerName, Name);
                     }
                 }
             }
             
         }
         
         
         void FixedUpdate()
         {
             if(Input.GetButton("Fire2"))
             {
                 AimObj.transform.localPosition = Vector3.Lerp(AimObj.transform.localPosition,Sights[CurSight].AimPos, 0.25f);
             }
             else
             {
                 AimObj.transform.localPosition = Vector3.Lerp(AimObj.transform.localPosition,Vector3.zero,0.25f);
             }
         }
     }
     
     [System.Serializable]
     public class Sight
     {
         public string Name;
         public GameObject Obj;
         public Vector3 AimPos;
     }
     public enum FireMode{
         Auto,
         Semi
     }
Comment
Add comment · Show 4 · 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 Blue Astronaut · Jan 25, 2014 at 10:50 PM 0
Share

Thanks man! You are so awesome! :D

avatar image Blue Astronaut · Jan 25, 2014 at 10:54 PM 0
Share

Wait 1 more thing please :D How would I make the audio repeat? :3

avatar image HuskyPanda213 · Jan 25, 2014 at 11:09 PM 0
Share

It should repeat, because it is based upon the fire method, which does not change. It's only that it is called every frame the click is held down in auto and the frame that the mouse is clicked in semi.

avatar image iwaldrop · Jan 26, 2014 at 12:33 AM 0
Share

For what it's worth, I would do something like this:

 public enum Fire$$anonymous$$ode
 {
     Safe,
     Single,
     Semi,
     Auto,
 }
 
 public Fire$$anonymous$$ode fire$$anonymous$$ode = Auto;
 public int roundsFiredCounter = 0;
 public int burstFireCount = 3;
 private $$anonymous$$agazine magazine;
 
 void Update()
 {
     if (!magazine.isEmpty)
     {
         switch(fire$$anonymous$$ode)
         {
         case Safe:
             // don't do anything
             break;
         case Single:
             if (GetButtonDown("Fire1"))
                 Fire();
             break;
         case Semi:
             if (roundsFiredCounter++ < burstFireCount && GetButton("Fire1"))
                 Fire();
             break;
         case Auto:
             if (GetButton("Fire1"))
                 Fire();
             break;
         }
     }

     // reset the rounds fired counter
     if (GetButtonUp("Fire1"))
         roundsFiredCounter = 0;
 }

In that way you can have multiple fire modes that only work when there is ammo in the currently inserted magazine.

avatar image
0

Answer by black1ops22 · Mar 17, 2015 at 10:52 PM

I would make a fully functional script...

using UnityEngine; using System.Collections; //Bulletspawnpoint MUST be named Bulletspawnpoint public class Tryintoscriptpewpews : MonoBehaviour {

 public Rigidbody Bullet; // Bullet, Bulletspawnpoint, Automatic, isShooting,Bulletsound, Magsize, Ammo, firerate, Blood, Range, Damage, Counter, Bullethole, Magsize, Ammo, Bulletsound, Bulletanim, Bulletholesmoke, Barrelflash
 public GameObject Barrelflash;
 public GameObject Bulletholesmoke;
 public GameObject Bulletspawnpoint;
 public GameObject Bullethole;
 public GameObject Blood;
 public bool Automatic;
 public float Range = 100f;
 public float Firerate = 8f;
 public float Magsize = 30f;
 public float Amountofclips = 4f;
 public float Damage = 15f;
 private float Ammo;
 private float Counter = 0f; 
 private bool isShooting;
 public AudioClip Bulletsound;
 public AudioClip Reloadsound;
 private RaycastHit hit;
 public Animation Reloadanim;

// These are the customizable values. // Create a spawnpoint and place it infront of the gun with a tag of "Bulletspawnpoint"

 void Update(){
     Counter += Time.deltaTime;
     if(Automatic == true) {
         if(Input.GetKey (KeyCode.Mouse0) && Firerate > Counter && Ammo > 0 && Amountofclips > -1){
             isShooting = true;
             if(isShooting == true){Fire();}
             }
         Counter = 0f;
         }
     else {
             if(Input.GetKeyDown (KeyCode.Mouse0) && Ammo > 0 && Firerate > Counter && Amountofclips > -1){
             isShooting = true;
             if(isShooting == true){Fire();}
         }
     } 
 }

 void Fire (){
     Instantiate (Barrelflash, GameObject.Find ("Bulletspawnpoint").transform.position, GameObject.Find ("Bulletspawnpoint").transform.rotation);
         Instantiate (Bullet, GameObject.Find ("Bulletspawnpoint").transform.position, GameObject.Find ("Bulletspawnpoint").transform.rotation);
         Bullet.velocity = Vector3.forward * 8;
         AudioSource.PlayClipAtPoint (Bulletsound, transform.position);
         Ammo = Magsize;
         Ammo --; 
     if (Ammo == 0 && Amountofclips > -1) {

     }
     if (Physics.Raycast (GameObject.Find ("Bulletspawnpoint").transform.position, transform.forward, Range)) {
         if(hit.collider.gameObject.tag == "Player"){
             Instantiate(Blood, hit.point, hit.transform.rotation);
             hit.collider.gameObject.SendMessage("ApplyDamage", Damage);
         }
         else {
             Instantiate(Bullethole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
             Instantiate(Bulletholesmoke, hit.point, Quaternion.identity);
         }
     }
 }

 void Reload(){
     GetComponent<Animation>().Play ("Reloadanim", PlayMode.StopAll);
         AudioSource.PlayClipAtPoint(Reloadsound, transform.position);

 }

}

just remember to reset the ammo when u respawn, OR (easier) in a health script destroy the object and in a game/network manager after X seconds intantiate the player

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

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

Related Questions

adding a new player into a 2d game in unity3 using a player prefab? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

GUILayoutGroup Missing From Unity 3.0 0 Answers

Unity 3D Audio Clip Disabled 1 Answer

unity3d motorScript 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