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
2
Question by MoonBeans · Dec 12, 2012 at 01:22 AM · javascriptrigidbodygravityexplosion

Add Explosion Force without torque?

Desired Result: An object is pulled towards a planet similarly to gravity, without interfering with the rotation of said object.

Problem: Object gets pulled around and rotated.

I create a sphere, then put the main camera as a child of it (this is the player). Then I apply these two scripts to the player:

SpaceLook.js

 #pragma strict
 
 var strafe:boolean = true;
 var leap:boolean = true;
 var speed:int = 10;
 var sensativity:int = 1;
 
 function FixedUpdate () {
     if (Input.GetKey(KeyCode.W)) {rigidbody.AddRelativeForce(Vector3.forward * speed);}
     if (Input.GetKey(KeyCode.S)) {rigidbody.AddRelativeForce(Vector3.forward * -1 * speed);}
     if (Input.GetKey(KeyCode.A) && strafe) {rigidbody.AddRelativeForce(Vector3.left * speed);}
     if (Input.GetKey(KeyCode.D) && strafe) {rigidbody.AddRelativeForce(Vector3.right * speed);}
     if (Input.GetKey(KeyCode.LeftControl) && leap) {rigidbody.AddRelativeForce(Vector3.down *  speed);}
     if (Input.GetKey(KeyCode.Space) && leap) {rigidbody.AddRelativeForce(Vector3.up *  speed);}
     
     if (Input.GetAxis("Mouse X")) {rigidbody.AddRelativeTorque(0, Input.GetAxis("Mouse X"), 0);}
     if (Input.GetAxis("Mouse Y")) {rigidbody.AddRelativeTorque(-Input.GetAxis("Mouse Y"), 0, 0);}
     if (Input.GetKey(KeyCode.Q)) {rigidbody.AddRelativeTorque(0, 0, 10 * speed * Time.deltaTime);}
     if (Input.GetKey(KeyCode.E)) {rigidbody.AddRelativeTorque(0, 0, 10 * -speed * Time.deltaTime);}
     
     if (Input.GetKey("c")) {Application.Quit();}
 }

PlanetGravity.js

 #pragma strict
 
 var gravity:float = 0.005;
 var center:Vector3 = Vector3(0, -177, 0);
 var range = 400;
 
 function FixedUpdate() {
     rigidbody.AddExplosionForce(-gravity, center, range);
 }

The look script doesn't really matter, but my problem is that when I run this, the sphere keeps spinning slightly from a force I can't pinpoint. Even when I don't move my viewport with the mouse at all, it still slightly rotates on it's local Z and X axii.

Any idea how to fix this?

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 pixelmixer · Jan 27, 2013 at 02:10 AM 0
Share

Hey $$anonymous$$oon, Did you ever figure this one out? I'm dealing with exactly the same issue.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Jan 27, 2013 at 03:14 PM

This is a collateral effect of AddExplosionForce: it applies a torque to the rigidbody proportional to the force. This isn't mentioned in the docs, but I believe that it's an intended feature: AddExplosionForce is supposed to throw things away to simulate explosion effects, and it would be very disappointed if these things didn't spin a lot!
The solution is: don't use AddExplosionForce for anything but explosions. If you want to simulate gravity, replace the second script with something like this:

 #pragma strict
  
 var gravity:float = 0.005;
 var center:Vector3 = Vector3(0, -177, 0);
 var range = 400;
  
 function FixedUpdate() {
     var dir = center - transform.position; // find vector ship->planet
     var g: float = range / dir.magnitude; // gravity reduces with distance
     rigidbody.AddForce(gravity * g * dir.normalized); // apply gravity
 }

This isn't 100% physically correct, but is close enough to even make the ship orbit the center position. If you want to implement a real gravity, take a look at this question.

Comment
Add comment · Show 1 · 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 pixelmixer · Jan 28, 2013 at 02:50 PM 0
Share

great advice! Thanks for the help. (I'd mark your answer correct if I were the op).

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

11 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

Related Questions

How to set velocity of Rigidbody without changing gravity? 1 Answer

Manipulate gravity in an area? 1 Answer

Input.acceleration on sphere with rigidbody(gravity) 1 Answer

Rigidbody not responding to setting gravity, but setting in inspector works. 1 Answer

Turning a gameobject's gravity on/off 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