Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

165
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda