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 MrBunBuns6969 · Feb 13, 2021 at 12:03 AM · 2dmovementgravity2d-physics

How to walk on a circular object unity 2d

I am making a game where you have to walk around a small planet. I have gravity working (after 2 days) and am trying to make my player walk. I tried following a normal tutorial on movement but it doesn't work because the x-axis doesn't change even when the payer is rotating around the planet. I saw a post saying if you can change the up axis from being the same as the y-axis it would work but I don't know how to do that since I'm very new to coding. Does anyone know how I would do that?

Heres my gravity script:

 using UnityEngine;
 using System.Collections;
  
 public class Player : MonoBehaviour
 {
        public Transform planet;
 private float forceAmountForRotation = 20;
 private Vector3 directionOfPlanetFromPlayer;
        private bool allowForce;
  
        void Start()
        {
             directionOfPlanetFromPlayer = Vector3.zero;
        }
  
 void Update ()
 {
  
             allowForce = false;
  
             if (Input.GetKey(KeyCode.Space))
                 allowForce = true;
  
             directionOfPlanetFromPlayer = transform.position - planet.position;
             transform.right = Vector3.Cross(directionOfPlanetFromPlayer, Vector3.forward);
 }
  
 void FixedUpdate ()
 {
 if (allowForce)
 GetComponent<Rigidbody2D>().AddForce (transform.right * forceAmountForRotation);
 }
 }

and here's the movement tutorial I followed: https://www.youtube.com/watch?v=dwcT-Dch0bA&ab_channel=Brackeys

and here's a video of what happens: https://youtu.be/peSuJslmkxk

Thanks in advance!

Comment
Add comment · Show 5
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 Eno-Khaon · Feb 13, 2021 at 12:40 AM 0
Share

Well, since it's 2D, there's actually a much simpler way to get your current "right" relative to your current "up" vector.

 Vector2 localUp = (transform.position - planet.position).normalized;
 Vector2 localRight = new Vector2(-localUp.x, localUp.y);
avatar image MrBunBuns6969 Eno-Khaon · Feb 13, 2021 at 12:48 AM 0
Share

How would I implement this script into my game? sorry, im a complete noob lol

avatar image MUG806 · Feb 15, 2021 at 09:52 PM 1
Share

Can you add the code you are using to move left and right? The code I see here looks okay, but the video you posted shows moving left and right, which the script doesnt do.

avatar image MrBunBuns6969 MUG806 · Feb 15, 2021 at 10:02 PM -2
Share

I followed the Brackeys tutorial I linked

avatar image jackmw94 MrBunBuns6969 · Feb 15, 2021 at 10:29 PM 1
Share

Yeah but the people skim$$anonymous$$g these forums looking for people to help don't want to find the exact part of a tutorial where he shows the completed code then assume you have used it with no changes and then cross reference this tab with the youtube tutorial code to figure out what's going on when you could copy+paste it as part of your question. Not trying to be too sassy, just getting ur question answered :)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by VoidPhoenix96 · Feb 15, 2021 at 10:50 PM

What you could do is just rotate the planet when you want your character to move and disable the characters movement.

Comment
Add comment · Show 4 · 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 MUG806 · Feb 15, 2021 at 10:52 PM 0
Share

Galaxy brain move there.

avatar image Eno-Khaon MUG806 · Feb 15, 2021 at 10:55 PM 0
Share

... until you have any other physics-driven entities you want to worry about, and need to get the problem solved for them. A proper solution to this wouldn't be limited to affecting only the (single) player.

avatar image VoidPhoenix96 Eno-Khaon · Feb 15, 2021 at 10:57 PM 0
Share

Yes, of course. I was just giving a simple solution that could be built upon.

Show more comments
avatar image
0

Answer by jackmw94 · Feb 15, 2021 at 10:53 PM

I think movement around a planet comes in 2 parts:

  1. Rotate ur boy so his upward direction is the direction from the planet centre to him.

  2. Move your boy right or left using his local right or left.

Bare in mind there's a conceptual issue that since he'll be upside down at one point, his left and right will be flipped and the controls might not feel intuitive. Rotating your camera with the player could fix this but you might like it as is

However, continuing with left and right being correct at the top of the planet; you are very close to the rotation code as you already calculate directionOfPlanetFromPlayer, use this to set your players upwards direction on the transform for him to keep his feet on the ground.

 transform.up = directionOfPlanetFromPlayer;


Now to handle the movement.

Since we handle the rotation first, we can use his local left and right directions to keep him going roughly around the world. If he's walked round to 3 o'clock on the planet and we've rotated him so that his upwards direction is pointing globally to the right, his local left will be globally upwards and his local right globally downwards. When I say globally, I just mean his direction disregarding his rotation - if you've turned your player upside down then his local upwards direction is pointing globally downwards. We don't have your current move player code but I expect that using this theory it'll look something along the lines of this:

 if (Input.GetKey(KeyCode.RightArrow))
 {
     transform.position += transform.right * _speed * Time.deltaTime;
 }
 
 if (Input.GetKey(KeyCode.LeftArrow))
 {
     transform.position += transform.left * _speed * Time.deltaTime;
 }


With _speed being a serialized field that you can use to adjust your character's speed. If you want to do this with physics rather than setting position then add the right side of these line as forces in fixed update.

These are the only two things you need. Plus your gravity code. Without gravity it won't perfectly follow the shape of the planet, unless the timestep is infinite there will be a gradual drift from the planet.

Can't quite tell what's happening at the moment but you can use (planet.position - transform.position).normalised as the gravity direction then scale it with your gravity strength and, if you want to, have this inversely proportional to the distance as this is how real gravity works but at the end of the day, game feel is paramount so you make that choice :)

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

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

[2D] Moving the player 1 tile at a time using rigidbody movement 0 Answers

Gravity switch 1 Answer

How To Create 2D Ragdoll Movement? 1 Answer

How to make a Grid movement (tile per tile) 1 Answer

Player Bump on map border 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