Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!