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 Mike Miller · Jun 30, 2010 at 09:25 PM · rotationmousesphereeulerangles

Rotating Sphere With Mouse

I would like to use the mouse to "grab" a point on a stationary positioned sphere and then be able to rotate the sphere by dragging the mouse around. This is similar to the earth in Google Earth.

The below code, attached to a sphere with a mesh collider partially works, but the initial grab jumps to a random place.

using UnityEngine; using System.Collections;

 public class MikesTrackBall : MonoBehaviour { 

    bool isGrabbed = false;
    Vector3 grabbedPoint;

    void Update () 
    { 
       RaycastHit hit; 

       if (Input.GetMouseButton(0)) 
       {         
         if(!isGrabbed)
         {
             isGrabbed = true;

             //Find the hit point
             Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), out hit);

             //Convert touched point to a local point on the earth
             grabbedPoint = getTouchedPointLocal();
         }
         else
         {
             Vector3 targetPoint = getTouchedPoint(); 
             transform.rotation = Quaternion.FromToRotation (grabbedPoint,targetPoint);
         }
       }
       else
         isGrabbed = false;
    }

    Vector3 getTouchedPoint()
    {
         RaycastHit hit;

         //Find the hit point
         Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), out hit);

         //Convert touched point to a local point on the earth
         return  hit.point-transform.position; 
    }

    Vector3 getTouchedPointLocal()
    {
         return transform.InverseTransformPoint(getTouchedPoint());
    }
 }

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
0
Best Answer

Answer by Mike Miller · Jul 13, 2010 at 09:51 PM

I posted a solution to this after help from cncguy on in this post: http://answers.unity3d.com/questions/14919/rotate-point-a-on-sphere-to-point-b-on-sphere

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

Answer by AnaRhisT · Jun 30, 2010 at 11:37 PM

Hey there, When u press right click with the mouse it rotates towards the Sphere or whatever gameobject u want, It's an edited code of MouseLook from Standard Assets. This is the part that does the effect :

if(Input.GetButton("Fire2")){
 x += Input.GetAxis("Mouse X") * 250.0f * 0.02f;
    y -= Input.GetAxis("Mouse Y") * 500.0f * 0.02f;
}


using UnityEngine; using System.Collections; /// edited by Creative from unity for a RPG camera.(Creative is me - AnaRhisT) [AddComponentMenu("Camera-Control/Mouse Look")] public class MouseLook : MonoBehaviour {

 public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
 public RotationAxes axes = RotationAxes.MouseXAndY;
 public float sensitivityX = 15F;
 public float sensitivityY = 15F;

 public float minimumX = -360F;
 public float maximumX = 360F;

 public float minimumY = -60F;
 public float maximumY = 60F;

 float rotationX = 0F;
 float rotationY = 0F;

 public int yMinLimit = -20;
 public int yMaxLimit = 80;

 public Transform target;
 Quaternion originalRotation;

 private float x = 0.0f;
 private float y = 0.0f;

 public float distance = 3;

// Vector3 newPosition = Camera.main.transform.position.y;

 public float playerDistance = 1.0F; //playerDistance from camera!
 void Update ()
 {

if (axes == RotationAxes.MouseXAndY) { if(Input.GetButton("Fire2")){ x += Input.GetAxis("Mouse X") 250.0f 0.02f; y -= Input.GetAxis("Mouse Y") 500.0f 0.02f; } y = ClampAngle(y, yMinLimit, yMaxLimit);

         Quaternion rotation = Quaternion.Euler(y, x, 0);
     //  Vector3 position= rotation * new Vector3(0.0f, playerDistance, -distance) + target.position;
         rotationX = ClampAngle (rotationX, minimumX, maximumX);
         rotationY = ClampAngle (rotationY, minimumY, maximumY);

         Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
         Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);

         transform.localRotation = originalRotation * xQuaternion * yQuaternion;
         transform.rotation = rotation;
      //   transform.position = rotation * 1 + target.position;
     }
     else if (axes == RotationAxes.MouseX)
     {
         rotationX += Input.GetAxis("Mouse X") * sensitivityX;
         rotationX = ClampAngle (rotationX, minimumX, maximumX);

         Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
         transform.localRotation = originalRotation * xQuaternion;
     }
     else
     {
         rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
         rotationY = ClampAngle (rotationY, minimumY, maximumY);

         Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
         transform.localRotation = originalRotation * yQuaternion;
     }

 }


 public static float ClampAngle (float angle, float min, float max)
 {
     if (angle < -360F)
         angle += 360F;
     if (angle > 360F)
         angle -= 360F;
     return Mathf.Clamp (angle, min, max);
 }

}

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 Mike Miller · Jul 09, 2010 at 05:03 PM 0
Share

What is the first "it" referring to?

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

No one has followed this question yet.

Related Questions

How to rotate an object's X, Y, Z based on mouse movement 4 Answers

object placement on sphere, rotation deform 1 Answer

Lock rotation on Z Axis 1 Answer

Problems caused by non-unique Euler angle solutions 1 Answer

How to set an exact Local angle. 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