- Home /
How to control camera orbit using joystick
Can someone fill me in on how to get values from a virtual joystick, using the standard joystick script, and apply it to a camera to allow the camera to freely orbit around a sphere's surface?
Answer by gribbly · Feb 05, 2013 at 04:51 AM
Well, you don't provide much detail - I'm not 100% sure what you're after. But here's a simple approach that might help:
In the editor, create an empty game object called "Pivot"
Make your camera a child of Pivot.
Move the camera away from Pivot, whatever distance you need.
Now attach a script to Pivot. Something like:
public class RotateWithJoystick : MonoBehaviour {
public Camera camera; void Update(){ //this will make the camera look "inwards" towards Pivot camera.transform.LookAt(transform); float speed = 10.0f; transform.Rotate(Vector3.up, Input.GetAxis("horizontal") * speed); transform.Rotate(Vector3.left, Input.GetAxis("vertical") * speed); } }
Assign your camera to the camera var in the script attached to Pivot
You'll have to mess with the rotations (e.g., you might not want Vector3.up assigned to the horizontal axis, and speed might be wrong)
That seems to give me the exact effect I am after! As soon as I move the past somewhere around 180 degrees'ish the camera seems to "flip" and my controls are backwards until it comes back around. Does that make sense? I see you said something about not mapping the horizontal axis to Vector3.up, and I tried some others but I think I'm a little lost with that. I really appreciate your help on this thanks!
Glad it worked - mark it as an answer!
I'm not in front of my dev machine, so can't test this too easily, but...
EDIT: forget all this, it was bad advice - correct answer below
Try something more along the lines of the examples in the Rotate documentation:
//note no comma
transform.Rotate(Vector3.up * Input.GetAxis("horizontal") * speed);
If that doesn't work you can probably do something along the lines of:
if(transform.rotation.x < 180){
transform.Rotate(Vector3.up, Input.GetAxis("horizontal") * speed);
}
else {
//note the $$anonymous$$us sign!
transform.Rotate(Vector3.up, -Input.GetAxis("horizontal") * speed);
}
You should also probably multiply speed by Time.deltaTime (i.e., framerate compensation).
I tried your suggestions and it still doesn't seem to help unfortunately... I find it hard to believe that I'm the only person having this issue. I can tell you exactly how I have my project set up if you would like? I can recreate the problem in about two $$anonymous$$utes of setup work. This is so frustrating!
Sorry, the fix is easy - the Transform.LookAt is causing the problem (long story - more detail here). Just omit it and it should work fine (I tested it this time). I shouldn't have told you to put that one there! Just set it up like I described and put this code on Pivot:
void Update () {
float speed = 3.0f;
float xRot = speed * Input.GetAxis("Vertical");
float yRot = speed * Input.GetAxis("Horizontal");
transform.Rotate(xRot, yRot, 0.0f);
}
$$anonymous$$ake sure the camera is pointing exactly at Pivot. I did this by putting both objects at (0,0,0) then entering a value of -10 in the inspector for $$anonymous$$ain Camera's z position.
If you have any trouble, I exported a working version as a Unity package - you can grab it here and check it out:
RotateTest.unitypackage.zip (I had to make it a zip so it would upload to the board)
All I can say is thank you!!! That works perfect, I tried playing around with it so much and never took the LookAt() out because I assumed it needed that. Oh well haha... I really appreciate your help, I've been staring at this for a couple days now and finally I can move on!
Answer by georgepear1 · Apr 14, 2020 at 02:30 PM
Hi, can I download this code in some way please? camera.transform.LookAt(transform); should be there or not?
Your answer
![](https://koobas.hobune.stream/wayback/20220613092148im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Player movement against skybox 0 Answers
Third Person Camera Orbit 0 Answers
Is there a Unity Package for this? 1 Answer
Mouse Orbit snapping issues 0 Answers