• 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
1
Question by watsonre63 · Feb 13, 2013 at 03:19 AM · rotationrelative

Continuous relative rotation.

Hello, I was wondering if someone might point me in the right direction RE a problem I'm having. Basically I want to continuously rotate a game object by a ratio which is defined by the distance (Vector3) of the mouse from Screen.height/2 & Screen.width/2. I already have the script that calculates this value (var dist) that uses Vector3.Distance, however I'm not really sure how to implement it beyond this. The key point is that the rotation is continuous, if I simply use Input.GetAxis("Mouse Y...X") * dist etc, it returns a linear value which rotates the game object by a set value, not a continuous ratio. If anyone could help it would be much appreciated; incidentally this is my first post so I apologize if my noobness is painful to you lol :)

Comment
Add comment · Show 1
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 iwaldrop · Feb 13, 2013 at 03:46 AM 1
Share

Welcome, and congrats on your first post. We like code here. :) Would you $$anonymous$$d posting the code that you're using to rotate your object?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Feb 13, 2013 at 04:13 AM

If you want to make the object look at the mouse pointer, attach this script to the object:

 function Update () {
     var mousePos: Vector3 = Input.mousePosition; // get the mouse coordinates
     // calculate the 3D point under the mouse pointer midway
     // between the camera and the object:
     mousePos.z = Vector3.Distance(Camera.main.transform.position, transform.position)/2;
     var target: Vector3 = Camera.main.ScreenToWorldPoint(mousePos);
     // look at the target point:
     transform.LookAt(target);
 }

But if you simply want to rotate the object proportionally to the mouse position, attach this one:

 var maxRot: float = 60; // angle at the screen borders
 
 private var angles: Vector3;
 
 function Start(){
   angles = transform.eulerAngles; // save the initial rotation
 }
 
 function Update(){
   var pos = Input.mousePosition; // read mouse coordinates
   // convert horizontal and vertical position to range -1..1
   var ah = (2 * pos.x / Screen.width) - 1;
   var av = (2 * pos.y / Screen.height) - 1;
   var newAngles: Vector3;
   // rotate the object from the initial rotation
   newAngles.y = (angles.y - ah * maxRot)% 360;
   newAngles.x = (angles.x - av * maxRot)% 360;
   newAngles.z = 0;
   transform.eulerAngles = newAngles;
 }

This code rotates the object proportionally to the mouse deviation from the center, reaching +/-maxRot degrees at the screen borders.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Rotate object in 90 degrees relative to camera. 1 Answer

Where to find original unity Parent/Child script? 2 Answers

Instantiate gun & arms rig relative to rotation & position of first person controller camera 0 Answers

alculate the position and rotation of the object relative to its subobject 3 Answers

How do I check a rotation relative to another object's rotation? 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