Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Steve 1 · Mar 05, 2010 at 02:06 AM · bulletgun

Making a gun shoot

Hi, I'm new to Unity and I'm trying to make a simple FPS. My problem is that when I shoot, the bullet doesn't go where I'm aiming, it just shoots right in front of me even if I'm aiming all the way up or down. Here's a screenshot. http://img21.imageshack.us/img21/1820/screenshotxwq.png

Here's the script I used for shooting. (And I don't really know much about scripting, I just got this from tutorials and stuff)

var BulletPrefab:Transform; var force : float = 2000;

function Update() { if(Input.GetButtonDown("Fire1")) { var bullet = Instantiate(BulletPrefab,

GameObject.Find("spawnPoint").transform.position,

GameObject.Find("Gun").transform.rotation);

bullet.rigidbody.AddForce(transform.forward * force);

} }

How do I fix this?

Thanks.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Design3.com · Mar 05, 2010 at 06:58 AM

It looks like the transform "forward" that you're referring to in that last line is the transform of whatever GameObject this script is attached to, rather than the transform of the bullet or the gun, which is probably what you need:

bullet.rigidbody.AddForce(bullet.transform.forward * force);

Alternatively, you could add a Constant Force Component to the bullet prefab and it could handle its own motion.

Comment
Add comment · Show 6 · 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 Steve 1 · Mar 06, 2010 at 08:26 PM 0
Share

That worked, thanks!

avatar image Joe 8 · Oct 14, 2010 at 01:32 AM 0
Share

my bar at the bottom keeps on saying, insert a semicolon at the end

avatar image 3 · Nov 11, 2012 at 05:45 AM 0
Share

It simply means you need to insert a semicolon on the line as instructed. If you new to Unity, Please, rather then relying on people here to make the game for you, read a book.

Thank you,

avatar image Starman385 · Dec 05, 2012 at 07:00 AM 0
Share

Where do I put this script? What I have is: First Person Controller Graphics (don't worry about this) $$anonymous$$ain Camera $$anonymous$$4 (which is the texture of my gun)

I put the script in $$anonymous$$4 but it didn't work. Where should I put it?

avatar image Nick Carpeggiani · Dec 28, 2012 at 11:13 AM 0
Share

it works fantastic thanks

Show more comments
avatar image
0

Answer by John 30 · Feb 27, 2011 at 04:25 PM

if the spawnpoint is at th end of the gun, and if it follows the camera this shouldn't happen.

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 Warren Pope · Dec 07, 2012 at 02:13 PM

var BulletPrefab :Transform; var force : float = 2000;

function Update() { if(Input.GetButtonDown("Fire1")) { var bullet = Instantiate(BulletPrefab,

GameObject.Find("spawnPoint").transform.position,

Quaternion.identity);

bullet.rigidbody.AddForce(transform.forward * force);

} }

try this script.make sure that your bullet prefab has a rigidbody attached to 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

8 People are following this question.

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

Related Questions

Bullet Hole not inline with sight?(Center of Screen) 1 Answer

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

Shoot the bottle 1 Answer

Showing a dynamic amount of bullets in a Belt/Magazine/Cylinder 0 Answers

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