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 321everybodycares · Mar 06, 2012 at 03:33 AM · gunfire

Making a gun shoot

I have this script it is supposed to make a gun shoot but the bullet does not go forward here it is:

var projectile : Rigidbody;

var speed = 50; var fireRate = 0.11; private var lastShot = -10.0; function Update () { if(Input.GetButtonDown("Fire1")){ if(Time.time > fireRate+lastShot){ clone = Instantiate(projectile, transform.position, transform.rotation); projectile.tag = "Bullet"; clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed)); lastShot = Time.time; } Destroy(clone.gameObject, 3); } }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DGArtistsInc · Mar 06, 2012 at 03:46 AM

if it is a rigidbody then i would not use velocity and transform direction. I would use rigidbody.AddForce instead for example

take out:

clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));

and put in:

clone.rigidbody.AddForce(Vector3.forward * speed);

Comment
Add comment · Show 4 · 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 rutter · Mar 06, 2012 at 04:33 AM 0
Share

Vector3.forward will always return (0,0,1); we probably want to use something like transform.forward here, to launch our bullet in the direction the shooter is currently facing.

avatar image DGArtistsInc · Mar 07, 2012 at 03:51 AM 0
Share

What are you talking about? Its the Vector3 of the rigidbody so it has nothing to do with the direction the character is facing. It matters what direction the object doing the instantiating is facing. Commonly a gun model is the child of an empty GameObject and that empty GameObject has the script on it usually. And when an object is Instantiated it is facing the direction that that empty GameObject is facing. SO it will shoot in the direction of which the shooter is facing so long that the empty GameObject and the Shooter are facing the same Direction.

avatar image rutter · Mar 07, 2012 at 05:19 PM 1
Share

As described by Unity's documentation: AddForce() uses world coordinates, which implies that force in the direction (0,0,1) would push it along the world's z-axis. Perhaps you're thinking of AddRelativeForce()?

If this is code that's worked for you in the past, more power to you.

avatar image DGArtistsInc · Mar 08, 2012 at 03:21 AM 0
Share

yes it does work for me... every time... when it says it implies force on the direction(0,0,1) it does not imply force in the WORLD'S Z Axis it implies force on the OBJECT'S Z axis Causing it to move forward. I see the point yo are making however AddForce has always worked for me. AddRelativeForce can be used as well though

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

Reload clips on Gun-script not applied properly 1 Answer

Is there a script out there that actully works for shooting bullets? 2 Answers

**[SOLVED]** Gun not firing after certain amount of shots 1 Answer

Need help with my gun script! 1 Answer

Fire rate problem [C#] 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