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 Cory · Jun 06, 2010 at 08:07 PM · fpsgun

FPS Scripting Problems

Okay, for some reason my machinegun doesnt have a projectile or it doesnt do anything other than shoot(not even apply damage, here is my code:

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 (Input.GetButtonDown("Fire1")) {

 // Instantiate the projectile at the position and rotation of this transform
 var clone : Rigidbody;
 clone = Instantiate(projectile, transform.position, transform.rotation);

 // Give the cloned object an initial velocity along the current
 // object's Z axis
 clone.velocity = transform.TransformDirection (Vector3.forward * speed);
 }


 // 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  0) {
     clips--;
     bulletsLeft = bulletsPerClip;
 }

}

function GetBulletsLeft () { return bulletsLeft; }

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
2

Answer by spinaljack · Jun 07, 2010 at 12:06 PM

Seeing as damage is declared but never used in your code example it must mean that it's being applied in another script (if at all). Try posting that one instead.

Assuming the apply damage script is on the projectile you need to make sure how it's detecting collisions (colliders, triggers or rays).

Then you need to make sure that it's detecting collisions, you can use a print or a debug log to say when it detects a collision

If it isn't triggering properly you need to make sure you've got the right type of colliders. If the bullet and the target both have non-convex mesh colliders then they wont ever collide. One of the objects needs to have a non-kinematic rigidbody (probably the projectile)

Then check that it's sending the damage message to the target and then you need to make sure that the target can take damage. Give it a script called ApplyDamage and reduce its health accordingly

etc. etc.

You simply need to read the FPS tutorial through carefully to get a fully functioning shooter game.

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

Answer by deadfroggy · Dec 24, 2012 at 12:13 AM

Add var projectile : GameObject;

That should do it.

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

1 Person is following this question.

avatar image

Related Questions

Gun Manager only working once 1 Answer

Camera Recoil,Please help me. 2 Answers

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

C# How to have weapon pickup 1 Answer

gun bob and jerk animation 2 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