• 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 Jackv949 · May 17, 2015 at 08:25 AM · gravity

How can I make my player run around small planets?

I am attempting to make my player run around small planets, similar to Mario Galaxy, but only on spherical planets. I have set up a scene like so:

alt text

I have managed to get the player to move around the planet, by aligning it to the surface normals like in the code below

 Vector3 inputVector = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
         Vector3 moveVector = inputVector * moveSpeed;
 
         if (planet)
         {
             RaycastHit2D hit = Physics2D.Raycast(new Vector2(transform.position.x, transform.position.y), -transform.up, 10f, planetLayer);
 
             if (hit)
             {
                 transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
             }
 
             moveVector.y -= planetInfo.pullStrength * Time.deltaTime;
 
             transform.Translate(moveVector * Time.deltaTime);
         }

My problem is that I would like the player to be able to jump out of a planet's orbit, and not be affected by gravity until it falls into the orbit of another planet.

I've tried some things involving rigid bodies, but it just hasn't worked so well.

planet.png (73.5 kB)
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

2 Replies

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

Answer by Cherno · May 17, 2015 at 08:55 AM

Ok, so generally speaking, for this to work you can use either a rigidbody or characterController, but they can't use gravity. Each planet needs a sphere collider that is larger than the planet itself, as a trigger, which represent the gravitational field. Continuously check if the player is inside the field, and if yes, apply force toward the planet depending on the distance (and possibly size of the planet), basically simulating gravity.

Now, when the player is on a planet aligned to the surface, and he jumps, apply force to him away from the planet's core (or surface normal). The jump force needs to be higher than the gravitational pull, of course. Since we continuously check if the play is in a gravitational field trigger, as outlined above, once he leaves the planet's field, the force towards the planet is no longer applied, so if there's any force left from the jump impulse, that force will make the player continue flying away from the planet.

Comment
Add comment · Show 5 · 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 Jackv949 · May 18, 2015 at 07:54 AM 0
Share

Using this solution works well for moving left and right using transform.Translate, but produces irregular movement because it's fighting the physics engine.

I tried using AddForce on the rigidbody, but moving left and right ends up throwing the character off the planet with the force because it is constantly adding force.

I also tried changing the x velocity of the rigidbody, but as this is not the local velocity it causes problems with gravity.

Any suggestions?

avatar image Cherno · May 18, 2015 at 09:11 AM 0
Share

check the API for AddForce. You can specifythe Forcemode, try using Impulse.

avatar image Jackv949 · May 18, 2015 at 09:39 AM 0
Share

Unfortunately, this also makes the player leave the planet's surface, and it just ends up orbiting the planet after a while.

Do you have any ideas how I could move the player at a reasonable speed without it leaving the planet's surface? (until I press the jump button, of course).

avatar image Eno-Khaon · May 18, 2015 at 10:00 AM 0
Share

If your character's being attracted to the surface by applying physics to a rigidbody, you'll have to fake it to maintain attachment.

One possible way to approach it is to reset your character's velocity (while on the ground/not jumping) to a new "forward" relative to the current direction of gravity (especially simplified on a sphere, in this case). Occasional adjustments might need to be made to ensure your character doesn't still fly off the ground as a result of rounding errors or hard edges (for any reason).

Another approach could be to apply a strong downward force every frame the character's not intentionally jumping, but numerous accommodations would need to be made to ensure that any edges the character walks near don't pull you off and down.

avatar image Jackv949 · May 18, 2015 at 10:21 AM 0
Share

Thanks for all the help. I guess I'll just try something less complicated and difficult for now. :)

avatar image
0

Answer by fafase · May 18, 2015 at 10:29 AM

Add a script to each planet that checks for distance to the player. Then consider that if the player is within reach then grab a method on the player that will divert its path. That force vector could be:

  Vector3 direction = centerPlanet.position - player.position;
  float force = 1f / Mathf.Sqr(Vector3.Distance(direction));
  Vector3 gravity = direction.normalized * force;

then when you get closer to the planet you can rotate the player with the same vector aligning the transform.up with the invert of the force vector (or the transform.down with your vector).

In order to leave the planet, you just need to make the jump force higher than the gravity force. Then start a long process of designing the appropriate gravity and jump forces.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

How to set gravity for model? 1 Answer

Mecanim movement on asteroids? 0 Answers

Can I access Physics2dSettings in Project Settings via script 1 Answer

OncollisonEnter2D does not work on moving object 0 Answers

How to only allow the user to press a button when they are touching a surface 0 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