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: 2D Movement in Unity (Tutorial) - YouTube

and here’s a video of what happens:

Thanks in advance!

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

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 :slight_smile: