Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 faheemmoolla · Sep 04, 2018 at 09:47 PM · cameracamera rotateorbit

Camera Orbit around Planet

I'm creating a game in Unity3D that requires the player to be in control of a planet. I want the camera to move around the planet and rotate in the vector direction of the mouse when the player is holding down the right click anywhere on the screen. I have code written out. However, the player is only able to rotate the camera around the planet if the mouse is on the planet, and throws a "InvalidOperationException: Nullable Object must have a value" error if it is off the planet. How do I change it so that the camera will rotate around the planet even if the mouse is not on the planet? Also, I need the camera to zoom in and out using the scroll on the mouse, and I'm not entirely sure how to do that? Thank you in advance.

 public class NewCameraScript : MonoBehaviour {
 
     private Vector3? mouseStartPosition;
     private Vector3? currentMousePosition;
 
     [Header("Settings")]
     public float planetRadius = 100f;
     public float distance;
     public float zoomSensitivity;
     public float maxZoom;
     public float minZoom;
     public float slowZoomValue;
     public GameObject planet;
 
 
     void Start()
     {
         // Cameras initial position
         Camera.main.transform.position = new Vector3(planet.transform.position.x, planet.transform.position.y, planet.transform.position.z - distance);
         Camera.main.transform.LookAt(planet.transform.position);
     }
 
 
     private void LateUpdate()
     {
         if (Input.GetMouseButtonDown(1))
             mouseStartPosition = GetMouseHit();
 
         if (mouseStartPosition != null)
             DragPlanet();
 
         if (Input.GetMouseButtonUp(1))
             StaticPlanet();
             
     }
 
 
     private void DragPlanet()
     {
         currentMousePosition = GetMouseHit();
         RotateCamera((Vector3)mouseStartPosition, (Vector3)currentMousePosition);
     }
 
 
     private void StaticPlanet()
     {
         mouseStartPosition = null;
         currentMousePosition = null;
     }
 
 
     private void Zoom()
     {
 
     }
 
 
     private void RotateCamera(Vector3 dragStartPosition, Vector3 dragEndPosition)
     {
         //normalised for odd edges
         dragEndPosition = dragEndPosition.normalized *planetRadius;
         dragStartPosition = dragStartPosition.normalized * planetRadius;
 
         // Cross Product
         Vector3 cross = Vector3.Cross(dragEndPosition, dragStartPosition);
 
         // Angle for rotation
         float angle = Vector3.SignedAngle(dragEndPosition, dragStartPosition, cross);
 
         //Causes Rotation of angle around the vector from Cross product
         Camera.main.transform.RotateAround(planet.transform.position , cross, angle);
     }
 
 
     private static Vector3? GetMouseHit()
     {
         RaycastHit hit;
 
         if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
         {
             return hit.point;
         }
         return null;
     }
 
 }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by telecaster · Sep 04, 2018 at 10:15 PM

do you want to rotate the planet? or do you want to rotate around the planet. I personally would suggest rotate around: https://docs.unity3d.com/ScriptReference/Transform.RotateAround.html you could then run

 void Update()
     {
         if (Input.GetButtonDown("Fire1"))
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray))
                 {
                  ...code here to identify if y+/- center and x+/- center and 
                     rotate around by a specific amount based on x and Y
                 }
         }
     }

Comment
Add comment · Show 1 · 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 faheemmoolla · Sep 04, 2018 at 11:15 PM 0
Share

I want the camera to rotate around the planet. thank you but the above code wont work for me as Ive tried it before. Once the camera gets to the north or south poles, if the player tries to rotate the camera left or right, the camera will spin around its axis rather than move in the vector direction right or left.

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

148 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

How to set horizon level for camera orbit script? 0 Answers

How do i orbit the camera around a mesh using mouse 3 0 Answers

Problem with Camera rotating 0 Answers

Get camera rotation constrained to left/right (ie. yaw)? 2 Answers

Camera Heights 0 Answers


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