Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
6
Question by Dax · Jan 21, 2010 at 09:48 PM · rotationmouseeulerangles

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

Hi there. I'm having a hard time coming up with a script that will allow me to rotate an object by all three axis based on the direction I move my mouse. To clarify a little more for example: no matter what position on it's X, Y, and Z it is in, I'd like the object in to rotate downward and toward me upon moving the mouse downward, rotate leftward no matter it's X, Y, Z position if i move my mouse leftward, ETC.

Edit: I worked around this problem by using physics to my advantage: Two positions to apply force makes the object spin as if my mouse is controlling it. Here's the script. If there are no better answers in the next few days I'll close this as solved.

function Update () { rigidbody.AddForceAtPosition(Vector3(transform.position.x+(10*Input.GetAxis("Mouse X")),transform.position.y,transform.position.z+(10*Input.GetAxis("Mouse Y"))),Vector3(transform.position.x,transform.position.y+4,transform.position.z)); print(transform.rotation); }

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

4 Replies

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

Answer by flaminghairball · Jan 21, 2010 at 10:19 PM

Why not just use transform.Rotate()?

  var speed : float = 1.0; //how fast the object should rotate
 
  function Update(){
       transform.Rotate(Vector3(Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0) * Time.deltaTime * speed);
  }


Best wishes, -Lincoln Green

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 Jamie_h88 · Oct 23, 2013 at 07:33 PM 0
Share

Is there away to put a restraint on this so it will only rotate a certain amount??

avatar image MrVerdoux · May 23, 2014 at 10:05 AM 0
Share

I'd guess you would have to keep track of the current rotation, or using transform's quaternion to check, and then do or not do the rotation.

avatar image wardevour · Jan 13, 2015 at 12:29 AM 0
Share

have u tried an attribute? http://unity3d.com/learn/tutorials/modules/intermediate/scripting/attributes

avatar image idiot333 · Jan 27, 2015 at 08:04 PM 0
Share

It says Unexpected symbol 'Time' ??? what is the problem?

avatar image Yanboys · Jun 30, 2015 at 07:29 PM 0
Share

ya, same here.

avatar image
5

Answer by TimTheTitan · Feb 12, 2016 at 01:54 PM

 using UnityEngine;
 using System.Collections;
 
 public class ExampleClass : MonoBehaviour {
     public float horizontalSpeed = 2.0F;
     public float verticalSpeed = 2.0F;
     void Update() {
         float h = horizontalSpeed * Input.GetAxis("Mouse X");
         float v = verticalSpeed * Input.GetAxis("Mouse Y");
         transform.Rotate(v, h, 0);
     }
 }


http://docs.unity3d.com/ScriptReference/Input.GetAxis.html

Comment
Add comment · Show 2 · 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 RT3DViz · Apr 19, 2016 at 06:38 PM 0
Share

Hi folks,

the snippet from TimTheTitan works well so far. But i want to add some enhancements to it. i tried a little bit but my skills are to low ^^

  1. the rotation should set itself back to 0,0,0 after 10 Seconds if theres no new input for this time. ( i do the rotation On$$anonymous$$ouseButton (0) and if after 10seconds no new input is given, it should set it back to zero) hope some1 understands me :P

  2. i want to give a maximum for the Y Axis, lets say 25degree

  3. and last but not least, the translation should be stopping smoothly after releasing the $$anonymous$$ouseButton.

I know its not Christmas but if some1 has an awnser or solution to it, it would be nice to read it. sry for my "bad" english :D

greets

avatar image TimTheTitan RT3DViz · Apr 20, 2016 at 12:27 PM 0
Share

Hi Rene T,

This is what you could do. I didn't test it, because I am busy, but it should work. You only have to reset and limit the rotation yourself. (Check links).

 //your current time
 private float currentTime = 0.0f;
 //your timer limit
 private float timerLimit = 10.0f;
     
     void Update()
     {
         //If there is no input from the left mouse button
         if(!Input.Get$$anonymous$$ouseButton(0))
             UpdateTimer();
 
         if(currentTime >= timerLimit)
         {
             //reset your rotation here
             currentTime = 0.0f;
         }
     }
 
     void UpdateTimer()
     {
         CurrentTime += Time.deltaTime;
     }

You can find most of the things yourself pretty easily.

Some useful links:

Rotating over time (smoothly)

Limiting rotations

Tim

avatar image
1

Answer by BloodK · Aug 01, 2011 at 12:26 PM

    float yawMouse = Input.GetAxis("Mouse X");
     float pitchMouse = Input.GetAxis("Mouse Y");
     Vector3 targetFlyRotation = Vector3.zero;
 
     Screen.lockCursor = true;
 
     if (Mathf.Abs(yawMouse) > 0.1f || Mathf.Abs(pitchMouse) > 0.1f)
     {
         targetFlyRotation = yawMouse * transform.right + pitchMouse * transform.up;
         targetFlyRotation.Normalize();
         targetFlyRotation *= Time.deltaTime * 3.0f;
 
         //limit x rotation if looking too much up or down
         //Log out the limitX value for this to make sense
         float limitX = Quaternion.LookRotation(moveDirection + targetFlyRotation).eulerAngles.x;
 
             //70 sets the rotation limit in the down direction
             //290 sets limit for up direction
         if (limitX < 90 && limitX > 70 || limitX > 270 && limitX < 290)
         {
             Debug.Log("restrict motion");
         }
         else
         {
             moveDirection += targetFlyRotation;
            //does the actual rotation on the object if no limits are breached
             transform.rotation = Quaternion.LookRotation(moveDirection);
         }
 
     }
Comment
Add comment · Show 2 · 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 Polaris007 · May 23, 2014 at 09:51 AM 0
Share

wats gt and amp

avatar image Shepherd_L01 · Jun 10, 2014 at 02:34 PM 0
Share

gt is greater than > amp is ampersand &

avatar image
0

Answer by RT3DViz · Apr 20, 2016 at 03:01 PM

Hi and thx Tim,

may i ask u something to the suggested solution? i tried but didnt get it :(

heres my code so fare: public GameObject Target; public float horizontalSpeed = 2.0F; public float verticalSpeed = 2.0F; public float angle = 45.0f;

    void Update()
         {
      
         
      
         if (Input.GetMouseButton(0))
 
         {
             MouseSpin();
         }
 
         if (Input.GetMouseButtonDown(1))
 
         {
            Target.transform.rotation = Quaternion.identity;
         }
      
     }
 
     void MouseSpin()
     {
         
         float h = horizontalSpeed * Input.GetAxis("Mouse X");
         float v = verticalSpeed * Input.GetAxis("Mouse Y");
         v = Mathf.Clamp(v, 1, -1);
         Target.transform.Rotate(v, h, 0);
         
         
     }
 
 
 }

the clamp doesnt work ^^ maybe im just to dumb, what did i wrong?

greets

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 TimTheTitan · Apr 25, 2016 at 10:17 AM 0
Share

You shouldn't post a question as an answer. If you can't find any similiar questions on Unity Answers you should make a new topic. Then you might get some more replies.

the clamp doesnt work ^^

Try to explain what doesn't work. Did you get an error? And try to debug with http://docs.unity3d.com/ScriptReference/Debug.Log.html I haven't tested your code yet, so I am not sure what's going wrong. Tim

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

8 People are following this question.

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

Related Questions

Lock rotation on Z Axis 1 Answer

Rotating Sphere With Mouse 2 Answers

Rotating a rigidbody on mouse click. 1 Answer

Rotation on Android vs. PC 0 Answers

How to rotate an object around a fixed point so it follows the mouse cursor? 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