Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
Object doesn't move to another hemisphere in unity

I'm trying to realize moving on an object round sphere (walk on it), but when it gets to the equator of the sphere, it stops moving.

public Transform planet;
public bool AlignToPlanet;
public float gravityConstant = -9.8f;

void FixedUpdate()
{
    Vector3 toCenter = planet.position - transform.position;
    toCenter.Normalize();

    GetComponent<Rigidbody>().AddForce(toCenter * 9.8f, ForceMode.Acceleration);

    if (AlignToPlanet)
    {
        Quaternion q = Quaternion.FromToRotation(-transform.up, -toCenter);
        q = q * transform.rotation;
        transform.rotation = Quaternion.Slerp(transform.rotation, q, 1);
    }

    Debug.Log(CrossPlatformInputManager.GetAxis("Vertical"));
    GetComponent<Rigidbody>().AddForce(transform.forward * 2, ForceMode.Impulse);
}
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

First off, you should avoid calling GetComponent more than necessary. Just call it once to get the Rigidbody in Awake then refer to the result in FixedUpdate.

Second, once you determine the object's up direction, you can use cross products to determine what would be the forward that is closest to the previous forward while still maintaining that up.

Then, you can use Quaternion.LookRotation to set that up and forward.

Finally, you should use Rigidbody.MoveRotation to set the rotation.

Altogether:

private Rigidbody rb;
public Transform planet;
public bool AlignToPlanet;
public float gravityConstant = -9.8f;

void Awake() 
{
    rb = GetComponent<Rigidbody>();
}

void FixedUpdate()
{
    Vector3 toCenter = planet.position - transform.position;
    toCenter.Normalize();

    rb.AddForce(toCenter * 9.8f, ForceMode.Acceleration);

    if (AlignToPlanet)
    {
        Vector3 newUp = -toCenter;
        Vector3 newRight = Vector3.Cross(newUp, transform.forward);
        Vector3 newForward = Vector3.Cross(newRight, newUp);

        Quaternion newRot = Quaternion.LookRotation(newForward, newUp);
        rb.MoveRotation(q);
    }

    Debug.Log(CrossPlatformInputManager.GetAxis("Vertical"));
    rb.AddForce(transform.forward * 2, ForceMode.Impulse);
}
over 4 years ago · Santiago Trujillo Denunciar

0

I set up a new project with your script, and it works fine for me.

The only things I changed (beside the AddForce on the forward to test it) are :

  • The FromToRotationin which you pass opposite vectors (one to the up, and one to the down)
  • The Slerp I removed, it is useless since you pass a value of 1.

So, it looks like this :

if (AlignToPlanet)
{
    // removed the minus in front of toCenter
    Quaternion q = Quaternion.FromToRotation(-transform.up, toCenter); 
    transform.rotation = q * transform.rotation;
}

EDIT
I agree with @Ruzhim : the LookRotation is a better way to compute your new rotation, since it takes also the forward of your object. (And for the fact to not use the GetComponent in any Update)

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda