- Home /
How do i move a player around a planet? (2d)
so i am quite new to unity,
I am trying to make a object. In my case a player, to walk around a planet using "a" and "d" and be able to jump using "w".
My player has a Ridgetbody2d and a boxcollider2d and my Planet has a tag: "Planet" and a circlecollider2d
But everything i have tried so far resulted in the player at some point flying off and not being able to jump at all.
Answer by InfixionGames · Apr 21, 2019 at 01:58 PM
I haven't tested the code but something like this should work
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5;
public float jumpSpeed = 2;
public Rigidbody2D rb2d;
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
Vector2 movement = new Vector2(moveHorizontal, 0);
rb2d.velocity = movement * moveSpeed;
if (Input.GetKeyDown(KeyCode.W))
{
rb2d.AddForce(transform.up * jumpSpeed);
}
}
}
This is the closest I managed to get, but when the player arrives at the planets equator he starts flying up, following the x axis I guess. I don't know how to fix this.
Answer by jandd661 · Apr 21, 2019 at 02:08 PM
Greetings, What you need to do is change the direction of the gravity as your player moves around the surface (edge) of the planet. You will also need align the player transform up with the gravity. Several tutorials are here. Hope this points you in the right direction.