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 Parzivell · May 15, 2014 at 07:37 AM · javascriptraycastvariable

Gun Script Help

Hello everyone, i am making a gun script, so far it has bullet holes, sound, and ammo. But there is a few things that arnt working the way i want. In the script, i was wondering if there was a way that when my variable "clip" gets down to 0, then play another sound, but that doesnt work, and i was also wondering how to make the bullethole texture stop appearing when my variable "clip" gets down to zero. One last question, is there a way to display a hitmarker when the raycast hits a certain tag? i have no idea how to use tags, i should though, but i cant.

Here is the Script (if anyone wants to use it, you just have to put it on your "MainCamera")

 var bulletTex : GameObject[]; // creates an array to use random textures of bullet holes
  
 var power : int = 10; //provides power to our raycast that can affect our rigidbody components
  
 var fireRate : float = 0.1;
 
 var clip : float = 30;
 
 var reloadTime : int = 2;
 
 
 
  
 private var nextFire : float = 0.0;
  
 var gunShot : AudioClip;
 
 var dryShot : AudioClip;
  
 function Update () {
  
 Fire ();
  
 }
  
 function Fire () {
  
 if(Input.GetButton("Fire1") && Time.time > nextFire) {
  
 nextFire =  Time.time + fireRate;
  
 AudioSource.PlayClipAtPoint(gunShot, transform.position, 1);
  
 ForceFire();
  
 }
  
 }
  
 function ForceFire () {
  
 var fwd = transform.TransformDirection(Vector3.forward); //casts our raycast in the forward direction
  
 var hit : RaycastHit;
  
 Debug.DrawRay(transform.position, fwd * 100, Color.green); //drays our raycast and gives it a green color and a length of 10 meters
  
 if(Input.GetButton("Fire1") && Physics.Raycast(transform.position, fwd, hit, 10)){ //when we left click and our raycast hits something
  
 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
  
 }
  
 
 
 
 if(Input.GetButton("Fire1")) {
 
 Debug.Log ("It's getting to function Reload");
 
 yield WaitForSeconds (fireRate);
 clip = clip -1;
 }
 
 if(clip == 0) {
  yield WaitForSeconds (reloadTime);
  
  clip = 30;
 
 if(Input.GetButtonDown("Fire1") && clip == 0); {
 
 AudioSource.PlayClipAtPoint(dryShot, transform.position, 1);
 
 if(Input.GetKey("r")) {
 clip = 30;
 }
 
 }
 
 } 
 }
  
 
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
0

Answer by Cherno · May 15, 2014 at 12:39 PM

Instead of calling the Fire function every frame in Update(), check for any mouse button presses in there, and if a button is pressed, check if the current bullets are > 0. If yes, call your normal firing function. If not, only play a clicking sound instead.

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 Parzivell · May 16, 2014 at 05:57 AM 0
Share

hmm, how would i do that in the script?

avatar image
0

Answer by johnson07 · May 16, 2014 at 07:56 AM

function Update(){

if(Input.GetButton("Fire1"){ //check for any mouse button presses in there. if(BulletLeft > 0){ //check if the current bullets are > 0

FireFunction();//call your normal firing function

}else{

NoBulletFunction();

} } }

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

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Change variable value of a script from another script. both in different Game object 1 Answer

Putting numbers on the end of variables via scripting 1 Answer

Countdown code not working 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