Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 StrawberryJellyfish · Mar 01, 2017 at 07:15 AM · rigidbody2dvelocityphysics2ddebug.logforces

Adding a force to RigidBody2d - Velocity returns 0

I'e been working on making a type of planetary gravity in my 2D game. I've made a center of gravity, and attached this script to the RigidBody2D that's supposed to be attracted to this center. I've removed the ability for this RididBody2D to use the natural gravity from the Physics Engine.

  //The gravity centre
     public Transform ring;
     
 
     public float gravityStrength;
 
     Rigidbody2D myRB;
 
     void Start()
     {
         myRB = GetComponent<Rigidbody2D>();
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         attract();
     }
 
     void attract()
     {
         //Pulling the object down
         Vector3 downDirection = (transform.position - ring.position).normalized;
         myRB.AddForce(downDirection * gravityStrength);
 
         Debug.Log(myRB.velocity);
 
         //Keeping the object upright
         Quaternion targetRotation = Quaternion.FromToRotation(transform.up, downDirection) * transform.rotation;
         transform.rotation = targetRotation;
     }

However, for some reason, the object doesn't seem to accelerate when it falls towards this centre of gravity (which is also slow for some reason), which isn't very realistic for gravity. I want to fix this acceleration issue, but I can't even debug the velocity of the rigidbody. It return 0 all the time. Any idea why?

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
0

Answer by N1warhead · Mar 01, 2017 at 09:31 AM

I'm going to assume it has to deal with your Vector3 downDirection not working good with your addforce.

From what you're explaining it's sounding like you want it to sorta work like a blackhole, the player falls towards the ring. Which if that's the case you can do an inverse explosive force. (Just google "Unity Explosive Force") and just change the value to a negative number, so instead of blowing the player outward you can give it a suction like force that gravitationally tries to pull it to an object.

It's technically a Vector3, but you can just leave the Z axis at 0 or where-ever you have them at. E.G. Vector3(SomePos,SomePos,0);

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 StrawberryJellyfish · Mar 01, 2017 at 10:27 AM 0
Share

Do you think there's a way to make it like a constant acceleration toward the point ins$$anonymous$$d (more or less like real gravity)? I plan to add jumping mechanics.

avatar image N1warhead · Mar 02, 2017 at 04:46 AM 0
Share

It should be a constant force, so long as your player is within the boundaries of the trigger. That's how I made my one game I made awhile back... If the player go within the range of the suction it would constantly try to pull you into the center of the (ring) for your case. So if you want it constant just make sure the the player is within the range of it and it will be constant as if it were gravity.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Objects displacement after applying force doesn't match calculations 1 Answer

2D rigidbody velocity relative to rotation? 0 Answers

How can I get 2 rigidbody2d objects to "share" physics? 1 Answer

Slow a rigidbody to a stop over a set distance? 1 Answer

Velocity doesnt change properly 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