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 dfghdh · Jan 20, 2013 at 05:16 PM · javascriptfpsweaponfiring

Problem With Weapon Firing Mechanism

Hi, I am making a weapon system for a FPS game whatever that might be. I have the basic structure in place to have a player that can pickup weapons and toggle between them. All weapons have a different magazine (in some cases some weapons may share the same type of magazine) and each magazine contains bullets based on the gun the magazine is loaded into. I also have a "FirePoint" game object attached to the camera on the first person controller as well as a bullet prefab (a simple sphere with a rigid body attached).I have even created the logic that works out when to fire the weapon and when not to and that all works.

However, when I fire to the left or right the trajectory of the bullet doesn't update with the new transform of the player. Here is a picture of my scene hierarchy http://i981.photobucket.com/albums/ae300/martianxx/Heiarachy_zps7599365c.jpg (sorry for bad quality). Here is the code I use when firing.

 //Player.js in update function
 if (Input.GetKey(KeyCode.Mouse0))
 {
     Debug.Log(equipment.GetWeapon(equipment.GetCurWeapon()).canFireNonAuto);
     if (equipment.GetWeapon(equipment.GetCurWeapon()).canFire && equipment.GetWeapon(equipment.GetCurWeapon()).canFireNonAuto)
     {
         StartCoroutine(equipment.GetWeapon(equipment.GetCurWeapon()).Fire());
     }
 }
             
 if (Input.GetKeyUp(KeyCode.Mouse0))
 {
     equipment.GetWeapon(equipment.GetCurWeapon()).CeaseFire();
     equipment.GetWeapon(equipment.GetCurWeapon()).canFireNonAuto = true;
 }

 //Pistol.js
 function Fire() : IEnumerator
 {
     canFire = false;
     canFireNonAuto = false;
     var track : GameObject;
     track = GameObject.Instantiate(ammo.bullet.bullet, weaponFirePoint.transform.position, weaponFirePoint.transform.rotation);
     track.rigidbody.AddForce(track.transform.forward * 10000);
     yield WaitForSeconds(timeToNextShot);
     canFire = true;
 }

 //AssaultRifle.js
 function Fire() : IEnumerator
 {
     canFire = false;
     var track : GameObject;
     track = GameObject.Instantiate(ammo.bullet.bullet, weaponFirePoint.transform.position, weaponFirePoint.transform.rotation);
 
     track.rigidbody.AddForce(track.transform.forward * 10000);
     yield WaitForSeconds(timeToNextShot);
     canFire = true;
     canFireNonAuto = true; //its an automatic weapon so set this true here to overide the non automatic logic would be better to have an automatic class
 }

 //AssaultRifle.js example weapon constructor
 function AssaultRifle()
 {
     ID = WeaponID.ARIFLE;
     weaponName = "Assault Rifle";
     weaponIcon = Resources.Load("AssaultRifle", typeof(Texture2D));
     weaponFirePoint = GameObject.FindWithTag("FirePoint");
     ammo = new Magazine(32, BulletID.ARIFLE);
     rateOfFire = 60;
     timeToNextShot = 0.1;
     reload = false;
     reloadTime = 2.0;
 }
Comment
Add comment · Show 4
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 sparkzbarca · Jan 20, 2013 at 06:51 PM 0
Share

is your bullet spawning in the wrong spot OR is it going the wrong direction?

$$anonymous$$y guess is its "forward" isn't parallel with the barrel.

if your bullet is spawning at the right location but with the wrong direction you could try rotating the firing point or you could in the direction of gun.forward maybe?

avatar image dfghdh · Jan 20, 2013 at 10:17 PM 0
Share

Yeah its defiantly because "forward" is not parallel with the "barrel". Currently I don't have any weapon mesh to represent the various weapons. The bullet defiantly spawns at the correct location. Can I have another game object attached to the camera that the FirePoint game object rotates towards OR work out the vector between the two, normalize it and then use that ins$$anonymous$$d of forward? Which would be better or is there another better option?

avatar image dfghdh · Jan 20, 2013 at 10:30 PM 0
Share

In debug mode in scene view I can see that the positive z direction is the vector I want to fire along.

avatar image dfghdh · Jan 20, 2013 at 10:34 PM 0
Share

I made a simple script as shown below and it confirms that the forward is not in the correct direction.

   void OnDrawGizmos()
   {
      Gizmos.DrawLine(transform.position, transform.forward);    
   }

This is attached to the FirePoint game object

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TutiBueno2 · Jan 20, 2013 at 10:56 PM

Is it feasible for you if you use the rotation of the camera instead?

Like this:

 track = GameObject.Instantiate(ammo.bullet.bullet, weaponFirePoint.transform.position, Camera.main.transform.rotation);

Since in FPS games the camera is always aligned with what the player is viewing, this will (hopefully) work.

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 dfghdh · Jan 21, 2013 at 01:01 AM 0
Share

Sorry for the trouble, I was being an idiot. Had sphere collider attached to the game object i was spawning a rigid body at >.<

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

11 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

Related Questions

Weapon Switch Animations 1 Answer

Weapon Switching 2 Answers

FPS Weapon Animation Mixing 1 Answer

Checkpoint script? 1 Answer

force player to crouch when under an object with collisio 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