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
1
Question by LukaKotar · Jan 29, 2012 at 08:16 PM · javascriptshootnot-working

I'm having problems with the FPS Tutorial MachineGun script

I put the modified PlayerWeapons script to an empty game object, and the MachineGun script to my game object I called "MachineGun", holding my gun. Before you ask what the hell am I doing, I am working on an iOS game, and I modified the PlayerWeapons script to respond on GUI Button presses. When I click on my MachineGun game object, I look at the Inspector, and the MachineGun script enables as it should,

but the problem is here: -It does not show muzzle flash -It does not show hit particles -And it does not apply damage.

Here are the scripts (JS):

The MachineGun script:

 var range = 100.0;
 var fireRate = 0.05;
 var force = 10.0;
 var damage = 5.0;
 var bulletsPerClip = 40;
 var clips = 20;
 var reloadTime = 0.5;
 private var hitParticles : ParticleEmitter;
 var muzzleFlash : Renderer;
 
 private var bulletsLeft : int = 0;
 private var nextFireTime = 0.0;
 private var m_LastFrameShot = -1;
 
 function Start () {
     hitParticles = GetComponentInChildren(ParticleEmitter);
     
     // We don't want to emit particles all the time, only when we hit something.
     if (hitParticles)
         hitParticles.emit = false;
     bulletsLeft = bulletsPerClip;
 }
 
 function LateUpdate() {
     if (muzzleFlash) {
         // We shot this frame, enable the muzzle flash
         if (m_LastFrameShot == Time.frameCount) {
             muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360, Vector3.forward);
             muzzleFlash.enabled = true;
 
             if (audio) {
                 if (!audio.isPlaying)
                     audio.Play();
                 audio.loop = true;
             }
         } else {
         // We didn't, disable the muzzle flash
             muzzleFlash.enabled = false;
             enabled = false;
             
             // Play sound
             if (audio)
             {
                 audio.loop = false;
             }
         }
     }
 }
 
 function Fire () {
     if (bulletsLeft == 0)
         return;
     
     // If there is more than one bullet between the last and this frame
     // Reset the nextFireTime
     if (Time.time - fireRate > nextFireTime)
         nextFireTime = Time.time - Time.deltaTime;
     
     // Keep firing until we used up the fire time
     while( nextFireTime < Time.time && bulletsLeft != 0) {
         FireOneShot();
         nextFireTime += fireRate;
     }
 }
 
 function FireOneShot () {
     var direction = transform.TransformDirection(Vector3.forward);
     var hit : RaycastHit;
     
     // Did we hit anything?
     if (Physics.Raycast (transform.position, direction, hit, range)) {
         // Apply a force to the rigidbody we hit
         if (hit.rigidbody)
             hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
         
         // Place the particle system for spawing out of place where we hit the surface!
         // And spawn a couple of particles
         if (hitParticles) {
             hitParticles.transform.position = hit.point;
             hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
             hitParticles.Emit();
         }
 
         // Send a damage message to the hit object            
         hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
     }
     
     bulletsLeft--;
 
     // Register that we shot this frame,
     // so that the LateUpdate function enabled the muzzleflash renderer for one frame
     m_LastFrameShot = Time.frameCount;
     enabled = true;
     
     // Reload gun in reload Time        
     if (bulletsLeft == 0)
         Reload();            
 }
 
 function Reload () {
 
     // Wait for reload time first - then add more bullets!
     yield WaitForSeconds(reloadTime);
 
     // We have a clip left reload
     if (clips > 0) {
         clips--;
         bulletsLeft = bulletsPerClip;
     }
 }
 
 function GetBulletsLeft () {
     return bulletsLeft;
 }

And the modified PlayerWeapons script:

 var fireButton : GUITexture;
 
 function Start () {
     SelectWeapon(0);
 }
 
 function Update(){
 
     for (var evt : Touch in Input.touches) {
 
         var HitTest1 = fireButton.HitTest(evt.position);
 
         if (evt.phase == TouchPhase.Began) {
 
             if(HitTest1){
 
             //do something when button is hit
 
             BroadcastMessage("Fire");
 
             Debug.Log("Shooting");
 
             } 
         }
     } 
 }
 
 function SelectWeapon (index : int) {
     for (var i=0;i<transform.childCount;i++)    {
         // Activate the selected weapon
         if (i == index)
             transform.GetChild(i).gameObject.SetActiveRecursively(true);
         // Deactivate all other weapons
         else
             transform.GetChild(i).gameObject.SetActiveRecursively(false);
     }
 }

Please let me know if you find the cause of it not working.

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
Best Answer

Answer by aldonaletto · Jan 29, 2012 at 09:34 PM

You should read the FPS_Tutorial_2 document carefully: it shows the solutions for the three problems. Basically, the solutions are listed below - but is advisable to have the document above at hand for more detailed explanations:

Muzzle Flash: The machinegun has a muzzle flash object childed to it: you must click the machine gun to make it appear in the Inspector, then drag the muzzle flash to the field Muzzle Flash in the MachineGun script.
Particles: You must child the Sparks prefab to the machine gun, and the script will find it automatically.
Damage: The target objects must have a damage script attached, or nothing happens: when an object is shot, if a function ApplyDamage(damage) exists in any object script, it's called by the instruction SendMessageUpwards.
There are two different damage scripts in the FPS tutorial: DamageReceiver.js, for objects, and CharacterDamage.js, to be used in the live creatures (the robots, in this case).

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 LukaKotar · Jan 29, 2012 at 09:43 PM 0
Share

Thanks! :D

avatar image eeveelution8 · Mar 13, 2013 at 12:42 AM 0
Share

where did the FPS tutorial go? i click to the link but it doesn't lead me to it :(

avatar image aldonaletto · Mar 15, 2013 at 09:47 AM 0
Share

Apparently, Unity has removed the FPS Tutorial because it became obsolete and incompatible with Unity 4. That's a bad thing, since the FPS tutorial was very instructive.

avatar image
0

Answer by gardian123 · Jan 04, 2013 at 08:15 PM

Rename the gun script to "MachineGun" case sensitive and make sure it's parented to the correct object. And the gun with the model should be called "machineGun" also sensitive. the weapon controller for machinegun should be "MachineGun" starting with a cap G unlike the actual model.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Increase energy bar by collecting item, partially working 0 Answers

Setting Scroll View Width GUILayout 1 Answer

My Lerp() is not working 2 Answers

I THINK that i have aplied a force of 1000 but it wont work 1 Answer

Make enemy more intelligent 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