I am trying to get a GameObject
to be able to be controlled to move freely around a sphere using a virtual on-screen joystick. I am building this for iOS and Android in Unity. I am using the standard Joystick
script that comes with the Standard Assets (Mobile) package. I can get the object to turn around it's forward vector using the joystick by this code:
if (moveJoystick.IsFingerDown())
{
// Rotate the ship according to the joystick angle
currentAngle = moveJoystick.getAngle();
thisTransform.RotateAroundLocal(Vector3.forward, Mathf.DeltaAngle(lastAngle, currentAngle)*Mathf.Deg2Rad);
lastAngle = currentAngle;
}
That rotates the ship around it's local axis correctly. My problem is trying to get the joystick to move the object around the sphere based on the axis that the joystick is on. This would mean the object moves in the angle it is pointing but staying at the same radius from the center of the sphere which is located at (0, 0, 0). Then I also need to have the camera follow the object as it moves. Any ideas?
Edit: I got the orbit to work based off of the joystick using the code below. But the rotations seem to get messed up as soon as it gets to a certain point. Around 180 degrees? Also what is the correct way to have the camera follow the ship? I tried to make the camera a child of the ship in the hierarchy and that is when I really notice the messed up rotations and the ship doesn't respond correctly to my joystick. Also now the joystick doesn't rotate the ship around it's local axis anymore. Hmmm.
if (moveJoystick.IsFingerDown())
transform.RotateAround(Vector3.zero, Vector3.up, -moveJoystick.position.x * SPEED * Time.deltaTime);
transform.RotateAround(Vector3.zero, Vector3.right, moveJoystick.position.y * SPEED * Time.deltaTime);
}