Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DiscGolfer17 · Feb 05, 2013 at 02:31 AM · orbit

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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)

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DiscGolfer17 · Feb 05, 2013 at 11:53 AM 0
Share

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!

avatar image gribbly · Feb 05, 2013 at 09:44 PM 0
Share

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).

avatar image DiscGolfer17 · Feb 06, 2013 at 12:29 AM 0
Share

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!

avatar image gribbly · Feb 06, 2013 at 06:36 AM 0
Share

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)

avatar image DiscGolfer17 · Feb 06, 2013 at 11:59 AM 0
Share

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!

avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Placing an object around character in 3d space? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges