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 runonthespot · Oct 04, 2010 at 10:23 AM · 2dgravityplanetary

2d planetary gravity

Hi there,

I'm quite far along making a 2d space shooter and have used the Unity Wiki gravity script to create some gravity-like effects on some planets that I have revolving around a sun (that orbit using simple rotateAround, as this isn't meant to be a realistic space sim!)

Now a feature of the games that I've played (SpaceWars! et al) and their "gravity wells" is the ability to perform a manoeuvre called a gravity slingshot, i.e. just zipping tangentially past the planet to gain some velocity beyond that which the ship is capable of. I've played a bit with the script and also the rigidbody parameters but either the gravity becomes too weak to have much effect, or the ship simply gets sucked towards the centre of the planet and bounces off it.

Does anyone have any idea how I might go about altering the Gravity.cs script to create this sort of effect, or a viable strategy for faking this effect (purely for gameplay purposes!)

Many thanks in advance for any thoughts, however random!

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

3 Replies

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

Answer by Wolfram · Oct 04, 2010 at 11:27 AM

A "real" gravity slingshot relies on a moving mass, it won't work on stationary planets. If your planetary mass is moving at velocity v, an object swinging by can gain up to 2*v speed, in the direction of the planet's movement. Maybe the Unity physics engine is realistic enough to produce a real slingshot if your planet does have a velocity.

If not, you would need some scripted trick to apply a "non-realistic" slingshot. For this you would need to think of some method for the player to enter his desired target direction, and then applying some rigidbody forces to create the slingshot effect towards that direction.

See also http://en.wikipedia.org/wiki/Gravity_assist

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 Edy · Oct 04, 2010 at 11:43 PM 0
Share

wow! offtopic, but simple and great explanation for slingshots. I used to play the Orbiter simulator and understood the underlying orbital mechanics, but I had never fully understood why slingshots are possible - until now.

avatar image
0
Best Answer

Answer by runonthespot · Oct 04, 2010 at 11:36 PM

Ooh, I should add it is helpful to clamp it between 0 & 1

so

rb.AddForce( rb.velocity * Mathf.Clamp((range - (Vector3.Distance(rb.transform.position,transform.position))/range),0,1), ForceMode.Acceleration);

to prevent any weirdness!

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

Answer by runonthespot · Oct 04, 2010 at 11:11 PM

I settled for a scripted trick in the end :)

the "Leyland gravity whip" which is more or less the effect I'm going for effectively adds forward velocity to the object caught in the gravitational field, to do a pseudo slingshot effect. In games like Star Control, the closer you were to the planet, the more extreme the effect. My algorithm therefore added forward velocity to the object equal to the (range - distance from planet) / range, e.g. 100% speed increase if you are 0 distance from the planet, and proportionally less the further you are away.

The end result was that after this line in the UnityWiki Gravity.cs

rb.AddForce( offset / offset.sqrMagnitude * rigidbody.mass);

//I added this line

rb.AddForce( rb.velocity * (range - (Vector3.Distance(rb.transform.position,transform.position))/range), ForceMode.Acceleration);

I'm not going to mark this as the answer yet as some testing required and any thoughts welcome too.

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Change the "down" axis for 2D 2 Answers

2D Top Down Character being Pulled down and movement script not working? 2 Answers

Why is there no gravitational acceleration with Unity 4.3.1 2D? 0 Answers

How could I simulate planetary gravity that has an orbit point? 1 Answer

How can I make a object get bigger as it falls? 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