- Home /
Orbit controlled by virtual joystick
I currently have a planet in my scene located at the origin. I have a space ship placed at (0, 0, -10) which shows my ship in the center of the screen and just outside of the planet.
I am using the Dual Joysticks included in the Standard Assets (Mobile) package for my Android game. I am trying to get the joystick to control the direction the ship moves, so that the ship will orbit the planet continuously in the direction the joystick is pointing. I have tried Transform.RotateAround()
by passing in arbitrary values (between -1 and 1) and it works. But I can't seem to get the joystick to control the rotation axis correctly. Any ideas?
If this doesn't make sense, let me know and I will try to explain it differently. Thanks!
Edit:
Here is my current hierarchy:
- Camera
Directional light
- EmptyGameObject
Ship
Planet
Here is my code so far that seems to almost work. When I move the joystick a lot the ship seems to rotate around it's local axis on it's own. Also when I attach the camera to move with the ship by putting it under the ship in the hierarchy, the camera keeps flipping and starts restricting movement quite a bit.
private const float MOVE_SPEED = 50.0f;
private Vector3 rotationAxis;
if (moveJoystick.IsFingerDown())
{
rotationAxis.Set(moveJoystick.position.y, -moveJoystick.position.x, 0.0f);
transform.RotateAround(Vector3.zero, rotationAxis, MOVE_SPEED*Time.deltaTime);
}
Answer by robertbu · Feb 04, 2013 at 08:18 PM
In dealing with this kind of problem, my usual solution is to use an empty game object. Create an empty game object and place it at the center of the planet. Make the ship a child of the empty game object (placed at 0,0,-10) as you have above. Attach your movement script to the empty game object and any rotations will be mirrored by the ship.
O$$anonymous$$, so that seems to be pretty close. See my edits above for what is happening now, as well as my current code and hierarchy setup. What is the best way to allow the camera to follow the ship freely? Thanks for the help!
I don't know how to answer concerning "camera to follow ship freely" because I have no idea how your game works. For a static position with respect to the ship, you can make the camera a child of the ship. For something more dynamic, there are a number of camera follow scripts around. Take a look at SmoothFollow2. It's out on the Unity Wiki and is billed as, "This is designed to make a camera smoothly follow a ship in space."
Sorry, that was bad wording haha... I just need the camera to follow the ship's movement on the same orbit. I tried just making the camera a child of the ship and that works until it gets to about 180 degrees of movement then everything seems to flip and the camera does a quick 180 degree turn until it gets back around and then does it again. Does that makes sense? It seems close, if I can just figure out this small issue. Thanks!
It doesn't make sense for the camera to flip and not the ship. Is the ship flipping rotating and then causing the camera to flip?