Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 backingeagle · Nov 26, 2021 at 05:52 PM · camera-movementmousepositionmouselookcamera-lookmouse-drag

Script for camera movement using Arcball,Scripting Camera Movements with Arcball

I wrote the following script, but I have a problem that the camera moves upward the moment I drag the mouse. How can I make it so that the camera does not move instantly when I drag the mouse?

using System.Collections; using System.Collections.Generic; using UnityEngine;

[RequireComponent(typeof(Camera))] public class ArcballScript : MonoBehaviour { // Is true when the user wants to rotate the camera bool ballEnabled = false;

 float rotationSpeed = 1f;
 float radius = 5f;

 // The mouse cursor's position during the last frame
 Vector3 last = new Vector3();

 // The target that the camera looks at
 Vector3 target = new Vector3(0, 0, 0);

 // The spherical coordinates
 Vector3 sc = new Vector3();

 void Start()
 {
     this.transform.position = new Vector3(radius, 0.0f, 0.0f);
     this.transform.LookAt(target);
     sc = getSphericalCoordinates(this.transform.position);
 }

 Vector3 getSphericalCoordinates(Vector3 cartesian)
 {
     float r = Mathf.Sqrt(
         Mathf.Pow(cartesian.x, 2) +
         Mathf.Pow(cartesian.y, 2) +
         Mathf.Pow(cartesian.z, 2)
     );

     float phi = Mathf.Atan2(cartesian.z / cartesian.x, cartesian.x);
     float theta = Mathf.Acos(cartesian.y / r);

     if (cartesian.x < 0)
         phi += Mathf.PI;

     return new Vector3(r, phi, theta);
 }

 Vector3 getCartesianCoordinates(Vector3 spherical)
 {
     Vector3 ret = new Vector3();

     ret.x = spherical.x * Mathf.Cos(spherical.z) * Mathf.Cos(spherical.y);
     ret.y = spherical.x * Mathf.Sin(spherical.z);
     ret.z = spherical.x * Mathf.Cos(spherical.z) * Mathf.Sin(spherical.y);

     return ret;
 }

 // Update is called once per frame
 void Update()
 {
     // Whenever the left mouse button is pressed, the
     // mouse cursor's position is stored for the arc-
     // ball camera as a reference.
     if (Input.GetMouseButtonDown(0))
     {
         // last is a global vec3 variable
         last = Input.mousePosition;

         // This is another global variable
         ballEnabled = true;
     }

     // When the user releases the left mouse button,
     // all we have to do is to reset the flag.
     if (Input.GetMouseButtonUp(0))
         ballEnabled = false;

     if (ballEnabled)
     {
         // Get the deltas that describe how much the mouse cursor got moved between frames
         float dx = (last.x - Input.mousePosition.x) * rotationSpeed;
         float dy = (last.y - Input.mousePosition.y) * rotationSpeed;

         // Only update the camera's position if the mouse got moved in either direction
         if (dx != 0f || dy != 0f)
         {
             // Rotate the camera left and right
             sc.y += dx * Time.deltaTime;

             // Rotate the camera up and down
             // Prevent the camera from turning upside down (1.5f = approx. Pi / 2)
             sc.z = Mathf.Clamp(sc.z + dy * Time.deltaTime, -1.5f, 1.5f);

             // Calculate the cartesian coordinates for unity
             transform.position = getCartesianCoordinates(sc) + target;

             // Make the camera look at the target
             transform.LookAt(target);
         }

         // Update the last mouse position
         last = Input.mousePosition;
     }
 }

}, I wrote the following script, but I have a problem that the camera moves upward the moment I drag the mouse. How can I make it so that the camera does not move instantly when I drag the mouse?

using System.Collections; using System.Collections.Generic; using UnityEngine;

[RequireComponent (typeof (Camera))] public class ArcballScript : MonoBehaviour { // Is true when the user wants to rotate the camera bool ballEnabled = false;

 float rotationSpeed = 1f;
 float radius = 5f;
 
 // The mouse cursor's position during the last frame
 Vector3 last = new Vector3();
 
 // The target that the camera looks at
 Vector3 target = new Vector3 (0, 0, 0);
 
 // The spherical coordinates
 Vector3 sc = new Vector3 ();
 
 void Start ()
 {
     this.transform.position = new Vector3 (radius, 0.0f, 0.0f);
     this.transform.LookAt (target);
     sc = getSphericalCoordinates (this.transform.position);
 }
 
 Vector3 getSphericalCoordinates(Vector3 cartesian)
 {
     float r = Mathf.Sqrt(
         Mathf.Pow(cartesian.x, 2) + 
         Mathf.Pow(cartesian.y, 2) + 
         Mathf.Pow(cartesian.z, 2)
     );
 
     float phi = Mathf.Atan2(cartesian.z / cartesian.x, cartesian.x);
     float theta = Mathf.Acos(cartesian.y / r);
 
     if (cartesian.x < 0)
         phi += Mathf.PI;
 
     return new Vector3 (r, phi, theta);
 }
 
 Vector3 getCartesianCoordinates(Vector3 spherical)
 {
     Vector3 ret = new Vector3 ();
 
     ret.x = spherical.x * Mathf.Cos (spherical.z) * Mathf.Cos (spherical.y);
     ret.y = spherical.x * Mathf.Sin (spherical.z);
     ret.z = spherical.x * Mathf.Cos (spherical.z) * Mathf.Sin (spherical.y);
 
     return ret;
 }
  
 // Update is called once per frame
 void Update ()
 {
     // Whenever the left mouse button is pressed, the
     // mouse cursor's position is stored for the arc-
     // ball camera as a reference.
     if (Input.GetMouseButtonDown(0))
     {
         // last is a global vec3 variable
         last = Input.mousePosition;
 
         // This is another global variable
         ballEnabled = true;
     }
 
     // When the user releases the left mouse button,
     // all we have to do is to reset the flag.
     if (Input.GetMouseButtonUp (0))
         ballEnabled = false;
 
     if (ballEnabled)
     {
         // Get the deltas that describe how much the mouse cursor got moved between frames
         float dx = (last.x - Input.mousePosition.x) * rotationSpeed;
         float dy = (last.y - Input.mousePosition.y) * rotationSpeed;
 
         // Only update the camera's position if the mouse got moved in either direction
         if (dx != 0f || dy != 0f)
         {
             // Rotate the camera left and right
             sc.y += dx * Time.deltaTime;
 
             // Rotate the camera up and down
             // Prevent the camera from turning upside down (1.5f = approx. Pi / 2)
             sc.z = Mathf.Clamp (sc.z + dy * Time.deltaTime, -1.5f, 1.5f);
 
             // Calculate the cartesian coordinates for unity
             transform.position = getCartesianCoordinates (sc) + target;
 
             // Make the camera look at the target
             transform.LookAt (target);
         }
 
         // Update the last mouse position
         last = Input.mousePosition;
     }
 }

}

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

0 Replies

· Add your reply
  • Sort: 

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

136 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

Related Questions

Mouse Look Help 2 Answers

Use input.mouseposition to rotate camera 1 Answer

Why the movement of the camera in the Z axis is so fast? 0 Answers

Rotate Camera to the object 0 Answers

Camera View Problem 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