• 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 JBoy · May 26, 2012 at 08:18 AM · collisionrigidbodyaddforceatposition

AddForceAtPosition instead of using rigidbody physics

I am trying to create a weapon script that instantiates a bullet rigidbody, and can hit other rigidbody objects. The problem I am having is I can't have the bullet moving too slowly, because bullets are typically very fast, and if I make the bullets at a more realistic speed the bullet will clip through rigidbodies.

So how would I be able to AddForceAtPosition when the bullet collides.

This is my code:

 var bullet : Transform;
 var bulletSpawn : Transform;
 function Update () {
 
 if(Input.GetButton("Fire1")){
     var forward = bulletSpawn.transform.forward;
     var instantiatedBullet =  Instantiate(bullet, bulletSpawn.transform.position, Quaternion.identity);
     instantiatedBullet.rigidbody.AddForce(forward * 1000);    
     instantiatedBullet.rotation=bulletSpawn.rotation;
 }

}

Comment
Add comment · Show 1
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 flamy · May 26, 2012 at 09:06 AM 0
Share

this might be a bit costly but change the collisionDetection$$anonymous$$ode to continous ins$$anonymous$$d of discrete.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by whydoidoit · May 26, 2012 at 09:17 AM

If you can consider a bullet to be a "point" you could do this by making a physics raycast from the current position, in the direction of travel, for the distance that will be moved in the next frame.

You would do that in FixedUpdate and detect the collision yourself.

The ray cast will give you the object hit and the position at which it was struck - then you could apply the force there and kill your bullet.

Doing this would save the physics simulation of the bullet, which is probably a bit pointess for a small and fast moving projectile. @flamy is right, if you want fast moving objects to intersect then you need to set the continuous dynamic mode, which is costly and probably not worth it if bullets can't hit other bullets or other fast moving objects.

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.

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

6 People are following this question.

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

Related Questions

how to make object follow mouse cursor while detecting collision 1 Answer

How I can make gameobject detect collisions and make every other gameobject be able to go through it? 1 Answer

OnCollisionEnter doesn't work if enemies and allies have rigidbody ? 1 Answer

Trouble with OnCollisionEnter and Exit 1 Answer

Rigidbody: how to get collisions for the current frame ? 0 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