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 erda123 · Aug 18, 2013 at 12:38 PM · bulletgunreloadammo

Ammo Script

Hello, i have my basic shooting script with bullet clone, muzzle flash & bullet texture instantieted and i dont know how to make reload function, heres my script:

 var bulletTex : GameObject[]; // creates an array to use random textures of bullet holes
 
 var projectile : Rigidbody;
 
 var speed = 100;
 
 var power : int = 10; //provides power to our raycast that can affect our rigidbody components
 
 var muzzelFlash : GameObject;
 
  
 
 var fireRate : float = 0.5;
 
  
 
 private var nextFire : float = 0.0;
 
  
 
 var gunShot : AudioClip;
 
  
 
 private var fullAuto = false;
 
  
 
 function Start () {
 
  
 
 }
 
  
 
 function Update () {
 
  
 
 Fire ();
 
  
 
 }
 
  
 
 function Fire () {
 
  
 
 if (Input.GetButtonDown("Fire1") && Time.time > nextFire) {
 
  
 
 nextFire = Time.time + fireRate;
 
  
 
 AudioSource.PlayClipAtPoint(gunShot, transform.position, 1);
 
 
 
 clone = Instantiate(projectile, transform.position, transform.rotation);
 
 clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
 
 
 
 if (muzzelFlash)
 
 holdMuzzelFlash = Instantiate(muzzelFlash, transform.position, transform.rotation);
 
 
 
 if (holdMuzzelFlash)
 
     holdMuzzelFlash.transform.parent = transform;
 
 
 
 
 
  
 
 ForceFire();
 
  
 
 }
 
  
 
 if (Input.GetKeyDown("w")) {
 
  
 
 fullAuto = !fullAuto;
 
  
 
 }
 
  
 
 if ( fullAuto == true) {
 
  
 
 fireRate = 0.5;
 
  
 
 }
 
  
 
 else {
 
  
 
 fireRate = 0.5;
 
  
 
 }
 
  
 
 }
 
  
 
 function ForceFire () {
 
  
 
 var fwd = transform.TransformDirection(Vector3.forward); //casts our raycast in the forward direction
 
  
 
 var hit : RaycastHit;
 
  
 
 Debug.DrawRay(transform.position, fwd * 10, Color.green); //drays our raycast and gives it a green color and a length of 10 meters
 
  
 
 if(Input.GetButtonUp("Fire1") && Physics.Raycast(transform.position, fwd, hit, 10)){ //when we left click and our raycast hits something
 
 
 
 clone = Instantiate(projectile, transform.position, transform.rotation);
 
 clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
 
 
 
 animation.Play();
 
 
 
 
 
  
 
 Instantiate(bulletTex[Random.Range(0,3)], hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)); //then we'll instantiate a random bullet hole texture from our array and apply it where we click and adjust
 
  
 
 // the position and rotation of textures to match the object being hit
 
  
 
 if (hit.rigidbody !=null)
 
  
 
 hit.rigidbody.AddForceAtPosition(fwd * power, hit.point);  //applies a force to a rigidbody when we click on it. Multiples our forward raycast times our power variable at the position we click
 
  
 
 }
 
 
 
 
 
 }
Comment
Add comment · Show 2
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 73cn0109y · Aug 18, 2013 at 01:08 PM 0
Share

Take a look at THIS

avatar image erda123 · Aug 18, 2013 at 01:44 PM 0
Share

$$anonymous$$Y GOD YOU AND CypherGames ARE WORLD $$anonymous$$OST S$$anonymous$$ARTEST PERSONS ON WORLD THAN$$anonymous$$S!

1 Reply

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

Answer by CypherGames · Aug 18, 2013 at 01:16 PM

Hey, erda123! I may have thae answer to your question. Try this: var bulletTex : GameObject[]; // creates an array to use random textures of bullet holes

 var projectile : Rigidbody;
  
 var speed = 100;
  
 var power : int = 10; //provides power to our raycast that can affect our rigidbody components
  
 var muzzelFlash : GameObject;
  
  
  
 var fireRate : float = 0.5;
  
  
  
 private var nextFire : float = 0.0;
 
 
 var clip : int = 30;
 
 
 var totalBullets : int = 180f;
 
 
 private var clipSize : int;
  
  
 var gunShot : AudioClip;
 var reloadSpund : AudioClip;
 var emptyClickSound : AudioClip;
  
  
  
 private var fullAuto = false;
  
  
  
 function Start () {
  
 clipSize = clip;
  
 }
  
  
  
 function Update () {
  
  
  
 if (clip > 0) {
 Fire();
 }
 
 else {
 if (totalBullets > 0) {
 Reload();
 }
 
 else {
 Empty();
 }
 }
  
  
  
 }
  
  
  
 function Fire () {
  
  
  
 if (Input.GetButtonDown("Fire1") && Time.time > nextFire) {
  
  
  
 nextFire = Time.time + fireRate;
  
  
  
 AudioSource.PlayClipAtPoint(gunShot, transform.position, 1);
  
  
  
 clone = Instantiate(projectile, transform.position, transform.rotation);
  
 clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
  
  
  
 if (muzzelFlash)
  
 holdMuzzelFlash = Instantiate(muzzelFlash, transform.position, transform.rotation);
  
  
  
 if (holdMuzzelFlash)
  
     holdMuzzelFlash.transform.parent = transform;
  
  
  
  
  
  
  
 ForceFire();
  
  
  
 }
  
  
  
 if (Input.GetKeyDown("w")) {
  
  
  
 fullAuto = !fullAuto;
  
  
  
 }
  
  
  
 if ( fullAuto == true) {
  
  
  
 fireRate = 0.5;
  
  
  
 }
  
  
  
 else {
  
  
  
 fireRate = 0.5;
  
  
  
 }
  
  
  
 }
  
 function Reload() {
 animation.Play("Reload");
 AudioSource.PlayClipAtPoint(reloadSound, transform.position, 1);
 var reloadTimer : float = 0.0;
 var reloadMax : float = animation.GetClip("Reload").length;
 reloadTimer += Time.deltaTime;
 
 if (reloadTimer >= reloadMax) {
 clip = clipSize;
 totalbullets -= clipSize;
 }
 }
 
 function Empty() {
 AudioSource.PlayClipAtPoint(emptyClickSound, transform.position, 1);
 }
  
 function ForceFire () {
  
  
  
 var fwd = transform.TransformDirection(Vector3.forward); //casts our raycast in the forward direction
  
  
  
 var hit : RaycastHit;
  
  
  
 Debug.DrawRay(transform.position, fwd * 10, Color.green); //drays our raycast and gives it a green color and a length of 10 meters
  
  
  
 if(Input.GetButtonUp("Fire1") && Physics.Raycast(transform.position, fwd, hit, 10)){ //when we left click and our raycast hits something
  
  
  
 clone = Instantiate(projectile, transform.position, transform.rotation);
  
 clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
  
  
  
 animation.Play();
  
  
  
  
  
  
  
 Instantiate(bulletTex[Random.Range(0,3)], hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)); //then we'll instantiate a random bullet hole texture from our array and apply it where we click and adjust
  
  
  
 // the position and rotation of textures to match the object being hit
  
  
  
 if (hit.rigidbody !=null)
  
  
  
 hit.rigidbody.AddForceAtPosition(fwd * power, hit.point);  //applies a force to a rigidbody when we click on it. Multiples our forward raycast times our power variable at the position we click
  
  
  
 }
  
  
  
  
  
 }
Comment
Add comment · Show 1 · 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 erda123 · Aug 18, 2013 at 01:25 PM 0
Share

i love you

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

Help on a pick up/reload script 3 Answers

Gun - Ammo , reloading and UI problem. 1 Answer

Gun with ammo help 1 Answer

How can I shooting script c#? 0 Answers

c# weapon shooting script using raycasting help 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