Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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);
    }
share|improve this question
    
As a query does the object in orbit have a camera snapped to it? –  Blue Apr 4 '13 at 9:29
1  
Yes, it does... I have since gotten this to work. I will have to post my solution back here when I get a chance. –  DiscGolfer Apr 4 '13 at 17:15
    
Don't forget you can provide your own answer! –  Blue Apr 5 '13 at 7:08
    
Don't forget to post your solution and accept it as the answer. –  Byte56 Jun 16 at 22:41

1 Answer 1

So if I understand the issue, you are having a problem orbiting a point, rather than rotating around your own forward vector? The first coordinate allows you to specify an axis to rotate around within the world, and you are currently passing in the objects own forward vector. What you would need to do (if you want to use a function similar to this) is pass in a vector that represents an axis/location that you would like to rotate around, in this situation i am expecting you would like to rotate around the "Up" axis of the target sphere. Also, be sure you are transforming your objects on the correct coordinate system (local/world).

Here is a link to the unity reference page, offering more specifics. http://docs.unity3d.com/Documentation/ScriptReference/Transform.RotateAround.html

share|improve this answer
    
Ok, so that works if I rotate around the "up" axis based on the joystick position. I can get it to orbit horizontally. Same if I use the "right" axis for vertical. When I combine them I get the effect I want. But the rotations seem to get stuck and my joystick movement doesn't match after about 180 degrees of rotations. I don't notice it until I make the camera a child of the ship. Does that make sense? It seems close but something is getting messed up with the rotations. See my edits in the original post. –  DiscGolfer Feb 3 '13 at 2:32
    
I guess i'm still a tad confused about the overall objective you are trying to achieve. Are you trying to get one object to orbit around another? Or are you trying to get an object to rotate around its own center point? Joy stick input gives you a normalized x/y position on a 2D plane. If you are trying to rotate on a single plane in 2 dimensions, you will have to use some trig to convert those components into rotations, which are applied to a specific axis. –  Evan Feb 3 '13 at 3:18
    
Sorry, I am trying to do both actually. The angle the joystick is at would map to the local rotation of my ship. So if the joystick is pointing diagonally up to the right. The ship would be at the same angle (which I've already gotten to work) ... Now I want to move the ship around a sphere based on that same angle that the joystick is at. Hopefully that clears it up. –  DiscGolfer Feb 3 '13 at 3:22
    
So assuming your ship is at a fixed radius, one way you could do this is to "move your ship" to the center of the object you want to orbit about, apply your rotations, and then move your ship back out. This provides a snapping movement though that wouldn't look very great. Rather than simply move your ship back out to the appropriate radius at the same angle, interpolate an angle in between the current and target angle based on some factor (delta time works). This will give you smoother rotations. –  Evan Feb 3 '13 at 3:29
    
If you want your ship to not be at a fixed radius, then you will have to customize this functionality to fit your needs. Something to keep in mind when performing 3D transformations is the pneumonic ISROT. Identity, Scale, Rotation, Orbit, Transformation. This is the order of operations that matrix transformations should occur. Orbit of course being a multi-step operation itself. –  Evan Feb 3 '13 at 3:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.