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 Tuah · Jun 10, 2010 at 10:57 PM · gravityplanet

Centric gravity issues...

'allo, I'm trying to finish this gravity system, but the character never wants to be attracted to my game object. Instead, it keeps getting attracted to the world origin. Here are the segments in question:

public GameObject centricGravityTarget;

else if (gravityMode == 2) {
            if (Physics.Raycast((transform.position + transform.up),(centricGravityTarget.transform.position + transform.position).normalized * -1, out hit, gravityField, groundLayers.value)) {
                desiredUp = (centricGravityTarget.transform.position + transform.position).normalized;
            }
        }

else if (gravityMode == 2) {
            Vector3 gravityDir = centricGravityTarget.transform.position + transform.position;
            gravityDir = gravityDir.normalized;
            rigidbody.AddForce(gravityDir * -gravity * rigidbody.mass);
        }

I suspect that the former will work once the latter works.

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

1 Reply

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

Answer by Peter G · Jun 11, 2010 at 08:59 PM

Ok, your problem with both appears to be you are adding/subtracting backwards for what you are trying to accomplish.

For the first one, I had a little trouble figuring out exactly what you are doing, I think you are sending a ray towards the planet to get the normal so that you can align the rotation correctly. So here is what I came up with. The trick is to subtract the player's position from the attractor instead of add them and flip the sign. Think of it like this. 5 - 2 != -(5 + 2)

    RaycastHit hit;
    if (Physics.Raycast((transform.position + transform.up),(centricGravityTarget.transform.position - transform.position).normalized, out hit)) {
        desiredUp = hit.normal;
        print (desiredUp);
    }

The second one had a nearly identical problem. So here it goes.

        //subtract instead of add and multiply by positive gravity in the last step.
        Vector3 gravityDir = centricGravityTarget.transform.position - transform.position;
        gravityDir = gravityDir.normalized;
        rigidbody.AddForce(gravityDir * gravity * rigidbody.mass);

One physics side note that I might be completely off on, so if I am, sorry, but don't objects fall at different speeds based on drag, not mass. Because don't heavier objects fall at the same speed as lighter ones (drop two balls outside to test it). If my understanding of physics is utterly wrong :) , please "save" me.

Comment
Add comment · Show 2 · 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 Tuah · Jun 12, 2010 at 01:17 AM 0
Share

This worked perfectly! I can't believe I missed something so simple. But hey, I guess that's how things work, eh?

Thanks a bunch! Just a little scripting to blend planet weights together, and my platformer system will be complete.

And yes, it's based on drag. The reason it's multiplied by mass is because it takes more force to move heavier objects. It's the same in real-world physics. With more massive objects, it takes more force to move it. And those objects have more mass by which to be affected by gravity.

avatar image Peter G · Jun 12, 2010 at 02:40 AM 0
Share

Ok, thanks for clarifying that.

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

2 People are following this question.

avatar image avatar image

Related Questions

Converting Local to World Forces in gravity simulation. 1 Answer

Adding a force(gravity) to a planet. 0 Answers

Implementing FPS view into locomotion planet walk 1 Answer

how to make gravity in the center of a sphere 1 Answer

how to apply orbit to the player 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