Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Gamevara · Jun 06, 2017 at 02:44 PM · 2dgravityphysics2d

How to walk on 2d sphere (planet)

Dear developers,

I have difficulties do a 2D game walking around the planet. It goes only around 90 degrees from top (where I start it) either left or right, please see the screenshot of the play. If I change the components to 3D it will work fine.

Could you please point me the error? And hopefully some fixes, too.

Player.cs:

 public float moveSpeed;
 private Vector2 moveDirection;
 public Planet attractorPlanet;
 private Transform playerTransform;
 
     void Start()
     {
         GetComponent<Rigidbody2D>().gravityScale = 0;
         GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeRotation;
         playerTransform = transform;
     }
     void Update()
     {
         moveDirection = new Vector2(Input.GetAxisRaw("Horizontal"), 0).normalized;
     }
     void FixedUpdate()
     {
        GetComponent<Rigidbody2D>().MovePosition(GetComponent<Rigidbody2D>().position + moveDirection);
         if (attractorPlanet)
         {
             attractorPlanet.Attract(playerTransform);
         }
     }

Player3D.cs:

 public float moveSpeed;
     public Planet3d attractorPlanet;
     private Transform playerTransform;
     private Vector3 moveDirection;
     void Start()
     {
         GetComponent<Rigidbody>().useGravity = false;
         GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation;
         playerTransform = transform;
     }
     void Update()
     {
         moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0,0).normalized;
     }
     void FixedUpdate()
     {
         GetComponent<Rigidbody>().MovePosition(GetComponent<Rigidbody>().position + transform.TransformDirection(moveDirection) * moveSpeed * Time.deltaTime);
         if (attractorPlanet)
         {
             attractorPlanet.Attract(playerTransform);
         }
     }

Planet.cs (3D version is about the same: use Rigidbody instead of Rigidbody2D):

 public float gravity = -12;
     public void Attract(Transform playerTransform)
     {
         Vector3 gravityUp = (playerTransform.position - transform.position).normalized;
         Vector3 localUp = playerTransform.up;
         playerTransform.GetComponent<Rigidbody2D>().AddForce(gravityUp * gravity);
         Quaternion targetRotation = Quaternion.FromToRotation(localUp, gravityUp) * playerTransform.rotation;
         playerTransform.rotation = Quaternion.Slerp(playerTransform.rotation, targetRotation, 100f * Time.deltaTime);
     }


Thanks for your reply.alt text

2d-walk.png (98.0 kB)
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 Gamevara · Jun 07, 2017 at 11:20 AM 0
Share

One extra line of code: Debug.Log(moveDirection); pointed me that Input.GetAxisRaw("Horizontal") give me only values on x = 1 or x = -1. And that's perhaps the reason it won't go further from 90 degrees.

How can I fix it?

P.S. I also put the debug code on Player3D and it also changed the values on x-axis only, but it doesn't limited the character for moving.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by hexagonius · Jun 07, 2017 at 03:50 PM

In the case of a perfect sphere, you don't need physics at all. The surface is just the radius around the center, which means clamping the distance to the center makes sure you won't get closer than that. For jumping and the like, you just add to the distance in a curve. Movement is basically rotating around the center.

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 Gamevara · Jun 08, 2017 at 08:32 AM 0
Share

Thank you for your reply. However this is not the case, since the final version there are all shapes of planets and also caves.

avatar image
0

Answer by RealGamesss · Oct 30, 2021 at 11:55 AM

Hi, I think this video might help you - https://youtu.be/blrybi2lVqo

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

117 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 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 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 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 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 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

Horizontal gravity on One gameobject 2 Answers

how to jump at fixed height but faster 0 Answers

How to use `Physics2D.IgnoreCollision()` to implement a one-way platform? 0 Answers

Jump faster and fall slower 1 Answer

Adding impulse force cancels out gravity force on collisions. 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