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 DubstepDragon · Jun 04, 2013 at 06:31 PM · camerarigidbodyforcepush

Applying a force to multiple objects at once...

I wanted to create a sort of force-gun that forces everything away from the area in front of it, similar to Half Life's Gravity Gun. How can this be implemented? I have already thought of 2 ways, but waiting confirmation from anyone.

  • The first was instantiating an object that will fly forwards with a powerful RigidBody force, pushing everything in front of it, then destroying itself after a period of time.

  • The second idea was a cone-shaped collider in front of the player, and by the use of an OnTriggerEnter, I can apply a force to the objects in the direction the camera is facing...

But the question is: How do I apply a force in the direction the camera is facing?

Thanks in advance,

John

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

Answer by robertbu · Jun 04, 2013 at 06:45 PM

There are two parts to this problem. The first is identifying the targets. How you do this will depend on the nature of the gun and the nature of your targets. You could use a Physics.SphereCastAll if you gun shoots a tube of force. If your gun shoots a cone of force, you can use Physics.OverlapSphere() to identify potential targets, then use Vector3.Angle() between the forward of the gun and each potential target to see if it is in the cone of force.

As for applying force, it will depend on what you want. Rigidbody.AddExplosonForce() will give you a force that falls off with distance from the gun, so that object further away will be less impacted. Or you can just apply a fixed amount of force in the direction the gun is aiming.

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 DubstepDragon · Jun 04, 2013 at 08:33 PM 0
Share

What I really want is just a burst that weakens as it travels, in a cone-shape. Can this be done with AddExplosionForce? Does it have to be a trigger?

Thanks in advance! :D

avatar image robertbu · Jun 04, 2013 at 08:56 PM 0
Share

It is going to be something like this (untested):

 #pragma strict
 
 var radius = 5.0;
 var power = 10.0;
 var coneAngle = 20;
 
     function Update() {
         if (Input.Get$$anonymous$$ouseButtonDown(0)) {
             FireGun();
         }
     }
 
     function FireGun () {
         var explosionPos : Vector3 = transform.position;
         var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
         
         for (var hit : Collider in colliders) {
            if (hit.rigidbody != null && (Vector3.Angle(transform.forward, collider.transform.position - transform.position) <= coneAngle)) {
                 hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0);
             }
         }
     }

Note you may want to check the 'tag' of the collider so you only apply the force to the things you want to add force to. This code uses the forward vector of the game object it is attached to.

avatar image DubstepDragon · Jun 05, 2013 at 04:06 PM 0
Share

Thanks dude, you are very awesome, just with a little tweaks to suit my needs, this script is the most organized I have ever seen out of all the others regarding this issue. I had to translate it to C#, though, because that is the language I use in my projects. Thanks again dude!

avatar image NandusMasta · Oct 17, 2015 at 01:48 PM 0
Share

Thanks a lot robertbu! Your answer and code helped me a lot to get something like the original poster wanted, going in my project. It's thanks to people like you that this community is so great!

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

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

Related Questions

Change player movement 0 Answers

Movement problem? 0 Answers

Stop a rotating plane using external force 1 Answer

Add force on camera using AddForce 2 Answers

Problem when applying forces to a ball 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