- Home /
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.
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.
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.
Thank you for your reply. However this is not the case, since the final version there are all shapes of planets and also caves.
Answer by RealGamesss · Oct 30, 2021 at 11:55 AM
Hi, I think this video might help you - https://youtu.be/blrybi2lVqo