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 user-4565 (google) · Oct 06, 2010 at 03:00 AM · positionbulletshoot

Bullet Positioning

I have several weapons in my game, and im starting to make it so they can shoot. I have a Desert Eagle which works fine, but then I have an F2000 that shoots, but the bullets are instantiated somewhere below the camera. I attached the script to the actual gun object, and used the same script as the Deagle, except modified to create an F200 bullet. I will link the script below, but I do not know why the F2000bullet is being made at a different position than the Deaglebullet, even though the 2 scripts are identical.

var projectile : Rigidbody; var initialSpeed = 20.0; var reloadTime = .2; var ammoCount = 60; var clipsize = 30; private var lastShot = -10.0;

function Fire() {

 if (Time.time > reloadTime + lastShot && clipsize > 0)
 {
     var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);

     instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));

     Physics.IgnoreCollision( instantiatedProjectile.collider, transform.root.collider);

     lastShot = Time.time;
     clipsize--;
 }

}

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

Answer by · Oct 06, 2010 at 03:06 AM

Check the transform gizmo / root node / pivot point of the F2000 GameObject. Instantiating the bullet at 'transform.position' via a script attached to the gun means that it will be spawned in a different place for each gun.

You could expose a public Vector3 on the gun script to define a relative position for the bullets to be instantiated from.

public var barrelPosition : Vector3;  // relative position of the barrel from the transform root

When you select one of the guns (that has this script attached), the barrelPosition will appear in the Inspector.

Then on the line where you instantiate the bullet, change it to:

var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.TransformPoint( barrelPosition ), transform.rotation);

When you fire the bullet, the position is determined by TransformPoint (the position of the gun with the local, relative position of the barrel applied). Tune the barrelPosition to get it to a point you're happy with.

Comment
Add comment · Show 7 · 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 user-4565 (google) · Oct 06, 2010 at 03:37 AM 0
Share

well the center pivot of the object is inside of the gun which I want, and I want the bullets to spawn at different places for each gun, cuz each gun has different places, heights, etc. that look good.

avatar image · Oct 06, 2010 at 04:02 AM 0
Share

Then I'd suggest declaring and defining a public Vector3, and spawn the bullets from that position, rather than the transform.position ... let me know if you're having trouble with this.

avatar image user-4565 (google) · Oct 06, 2010 at 02:37 PM 0
Share

How do I do that? Sorry im new to Unity and JS, im just slightly modifying the FPS tutorial scripts as I go.

avatar image · Oct 06, 2010 at 11:13 PM 0
Share

I've added further information + example code. Hope this helps.

avatar image user-4565 (google) · Oct 07, 2010 at 03:15 AM 0
Share

ah yes, this works nicely. Yet I still dont see how my bullet didnt spawn in the right place in the first place. The code for the 2 guns is identical, and so is the object positioning :/

Show more comments
avatar image
0

Answer by user-4565 (google) · Oct 07, 2010 at 01:55 PM

Thank you so much for you're time in helping me out, its nice to get an answer every once in awhile thats actually helpful and persistently helpful, rather than "use a public variable. search the forums first next time." :) Thank you a great much!

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 · Oct 07, 2010 at 10:07 PM 0
Share

No worries. Remember to vote up answers that help you, and mark them correct (click the tick) if it solves your problem. Cheers!

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

No one has followed this question yet.

Related Questions

Setting bullet instansiate direction? help? 1 Answer

shooting problem with raycasting 1 Answer

,I can't get the CannonBall to shoot in the expected direction 1 Answer

Setting parent in instantiated class 3 Answers

How to shoot a bullet to curser position? 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