• 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
1
Question by user-3061 (yahoo) · Jun 16, 2010 at 09:52 AM · projectileweapon

Shooting a bullet/ projectile PROPERLY

I'm working on a FPS game, and have only a little knowledge about scripting and animations. I encountered a problem, spent all day long searching answers for it, still no hope. I attached this script :

 var projectile : Rigidbody;
 var speed = 20;
 
 function Update () {
     // Put this in your update function
     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);
     }
 }

into my camera, but when i press shoot, it shoots the bullet all around. Even if im aming at the same spot, sometimes it shoots upward, sometimes it shoots downwards, sometimes from the centre, sometimes from below, even from on top. How do i fix this problem?

Also, can anyone give me a scrip suitable for a gun that shoots using raycast? REALLY needed help, 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

9 Replies

· Add your reply
  • Sort: 
avatar image
9
Best Answer

Answer by · Jun 16, 2010 at 10:49 AM

From the Unity Rigidbody.velocity documentation:

In most cases you should not modify the velocity directly, as this can result in unrealistic behaviour.

Instead of setting the object velocity, add force to the object:

clone.AddForce(clone.transform.forward * speed);

I've not tried instantiating a Rigidbody before, only a Transform/GameObject prefab, but I can't think of a reason it wouldn't work. Try pasting this code over yours.

 var projectile : Transform;
 var bulletSpeed : float = 20;
 
 function Update () {
     // Put this in your update function
     if (Input.GetButtonDown("Fire1")) {
 
     // Instantiate the projectile at the position and rotation of this transform
     var clone : Transform;
     clone = Instantiate(projectile, transform.position, transform.rotation);
 
     // Add force to the cloned object in the object's forward direction
     clone.rigidbody.AddForce(clone.transform.forward * shootForce);
     }
 }
Comment
Add comment · Show 16 · 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-3061 (yahoo) · Jun 16, 2010 at 11:11 AM 0
Share

its still the same.... my real problem is that the projectile is being shot randomly... when im standing on a plane, sometimes the bullet even goes beneath the plane....

avatar image · Jun 16, 2010 at 11:39 AM 0
Share

I suspected that adding a velocity directly to the Rigidbody was the cause of the random direction. Does your script look exactly like the code I've got in my answer? (updated to include the whole script)

avatar image user-3061 (yahoo) · Jun 16, 2010 at 11:43 AM 0
Share

its exactly the same..

avatar image user-3061 (yahoo) · Jun 16, 2010 at 11:45 AM 0
Share

By the way, i put the script inside the camera, which is a child of my controller

avatar image · Jun 16, 2010 at 12:06 PM 0
Share

I've tested this locally, adding the script to the camera, and it works fine. I've changed the Rigidbody variables to Transform.

Can you try the script in the updated answer and let me know how it goes? Are there any scripts on the projectile prefab?

Show more comments
avatar image
10
Best Answer

Answer by Novodantis 1 · Jun 16, 2010 at 12:48 PM

Is it possible that you have a collider on your camera that is colliding with your projectile? Use Physics.IgnoreCollision(clone.collider, collider) to ignore collisions between the fired object and the object doing the firing.

Edit: moved from comment to answer

Comment
Add comment · Show 10 · 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-3061 (yahoo) · Jun 16, 2010 at 12:49 PM 0
Share

how do i put it inside the script?

avatar image Novodantis 1 · Jun 16, 2010 at 12:51 PM 0
Share

You add this to the part of your code where you instantiate the bullet, any time after it has been created. The first value is the collider on the new object, "clone.collider"; and the second is the collider on the thing that fired it, "collider" (that is the one attached to the same object as the script).

avatar image user-3061 (yahoo) · Jun 16, 2010 at 12:56 PM 0
Share

Nope, its not the collision who caused this, because ive tried using an empty object attached with the script, and the result is the bullet falling below the ground..... this is really frustrating.....

avatar image Novodantis 1 · Jun 16, 2010 at 01:10 PM 0
Share

If nothing is working still, try making the projectile from scratch: a sphere with the collider removed and a rigidbody added (gravity disabled).

avatar image user-3061 (yahoo) · Jun 16, 2010 at 01:21 PM 0
Share

thanks, finally got it working, but still, i have to set the speed to 10000 to get it working like rocket speed...

Show more comments
avatar image
1

Answer by nari0015 · Oct 07, 2011 at 06:20 PM

Your Bullet will always fire at what it is attached to, so if you attach the bullet with the firing script, and a small invisible square without a mesh, then it should fire at, and through, the square

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
1

Answer by FizzyBear · Apr 09, 2012 at 11:14 AM

I changed this

 clone.AddForce(clone.transform.forward * speed);

Into

 clone.AddForce(clone.transform.forward * 1000);

And it worked :o

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
1

Answer by wilczek01 · Feb 27, 2013 at 10:44 PM

 rigidbody.AddRelativeForce(Vector3.forward * Time.deltaTime * speed); 

I hope that it works ;)

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
  • 1
  • 2
  • ›

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Click button to fire weapon. 3 Answers

Shooting multiple bullets 2 Answers

Best way to make multiplayer projectiles and enemy shooting player (discussion) ? 0 Answers

shoot projectile from weapon 2 Answers

How can I rotate a rigid body based on its velocity? 3 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