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 Bambenek · Feb 25, 2015 at 08:58 PM · rotationjavascriptmousemath

[JS]Rotate object y axis to mouse position

Hello everybody,

after all this years I'm trying to work with unity. My javascript is good but unity's javascript is a bit different. However I have a math problem. Normally I would calculate with math.deg but this function isn't present. So I did it this way. So I've created a testing-cube. It's rotating like hell but not with the mouse...

However maybe you know a solution. And no, I won't use any standart assets. I want to learn and make experience.

 #pragma strict
 
 function Start(){
 }
 
 function Update(){   
 var cursor = Camera.main.ScreenToWorldPoint( new Vector3( Input.mousePosition.x, Input.mousePosition.z, 10 ) );
 var rotation = Mathf.Rad2Deg * Mathf.Atan2( cursor.x - transform.position.x, cursor.z - transform.position.z );
 transform.Rotate( 0, rotation, 0 );
 Debug.Log( "x: " + cursor.x + ", z: " + cursor.z + ", rotation: " + rotation );
 }

So console says mouse position x and z are correct and in 3d. Only the rotation thing doesn't work...

Greetings,

Bambenek

Comment
Add comment · Show 4
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 meat5000 ♦ · Feb 25, 2015 at 08:05 PM 0
Share

Click on the object you are rotating and observe its transform component values when you click Play button.

avatar image Bambenek · Feb 25, 2015 at 08:07 PM 0
Share

hey, so I did. Y axis is changine, cube rotation but uncontrolled and not with mouse...

avatar image meat5000 ♦ · Feb 25, 2015 at 08:22 PM 0
Share

The function you chose performs an instantaneous rotation.

Look up Lerp and look up the use of deltaTime.

avatar image Bambenek · Feb 25, 2015 at 08:36 PM 0
Share

now I understand the problem. how should deltaTime solve my problem?

As I think I have to do somethink like:

 var mouseX:float = 0;
     var mouseY:float = 0;
     function Update(){ 
         var mouse = Input.mousePosition;
     if ( ( mouse.x - mouseX ) > 5 || ( mouse.y - mouseY ) > 5 ){
 

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by maccabbe · Feb 25, 2015 at 09:34 PM

You are using Rotate incorrectly. Every time transform.Rotate(x) is called, the cube is rotated x amount from its original rotation. The rotation appears random because the rotation is continuously increasing at a fast rate. Try something like the following

  function Update(){   
      var cursor = Camera.main.ScreenToWorldPoint( new Vector3( Input.mousePosition.x, Input.mousePosition.z, 10 ) );
      var rotation = Mathf.Rad2Deg * Mathf.Atan2( cursor.x - transform.position.x, cursor.z - transform.position.z );
      transform.Rotate( 0, rotation-transform.rotation.y, 0 );
      Debug.Log( "x: " + cursor.x + ", z: " + cursor.z + ", rotation: " + rotation );
  }

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 RockingGameDeveloper · Nov 21, 2019 at 02:03 AM 0
Share

Doesn't work.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rotate object with mouse 1 Answer

Authoritative Rotation 2 Answers

rotation based on mouse position 2 Answers

"Perfect" Free Rotation Method? (like Blender) 2 Answers

Rotate object following mouse movement. Object jumps/ flips 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