- Home /
Making an object rotate around a sphere
I'm trying to make a ship sail around a sphere but i'm having some issues turning it when i need too. The code i have makes it sail perfectly in a straight line around the sphere but the moment i need it to turn it flips out.
If i comment out the transform.rotation in the Update() and use RotateTowards(). It will turn towards the target but it wont align the ship with the surface. If i leave that line uncommented anything RotateTowards does is ignored.
void Update()
{
GetFSM().Update();
lastPosition = transform.position;
Vector3 newPos = lastPosition + transform.rotation * ForwardSpeed * Time.fixedDeltaTime * 0.1f;//
newPos = newPos.normalized * altitude;
transform.position = newPos;
transform.rotation = Quaternion.FromToRotation( Vector3.up, newPos ) * Quaternion.Euler(0, angle, 0);*/
}
public void RotateTowards (Vector3 targetPos)
{
Vector3 relative = transform.InverseTransformPoint( targetPos );
angle = (Mathf.Atan2(relative.x, relative.z) * Mathf.Rad2Deg) * Time.deltaTime ;
transform.Rotate (0, angle * Time.deltaTime, 0);
}
Answer by ocularcash · Nov 10, 2011 at 05:59 AM
I don't see why have all that but if your just orbiting just make an empty gameobject and place it as a child so you can use it as a rotation point and attach this and it should work just fine. public int rotSpeed;
void Update ()
{
transform.Rotate(rotSpeed * Time.deltaTime, 0, 0);
}
hope this works for you
I'd like to avoid attaching objects to the sphere. But let's say I do, how would i be able to navigate (move forward around the sphere and turn to targets) using that?
Your answer
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
Changing object rotation in animation automatically changes it's position 1 Answer
Cannot rotate GameObject from C# Animator attached 1 Answer
Detecting the rotation of the current object? Shield resets when undertaking orbit. 2 Answers
How to Rotate camera around object SMOOTHLY when using Input.GetKeyDown 1 Answer