Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by TitaniumVanguard · Aug 28, 2016 at 12:13 PM · quaternion2d rotation

My object keeps turning when I move my mouse, how do I fix this?

I have been pretty new to programming in Unity. I want my object/sprite to look at the position of my mouse so I found some code:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController_02 : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
         //position of the mouse = x-value and y-value and z-value
         Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y,0);
         //get the position of the mouse on the screen
         Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
         //the angle == tan(z/x) * radiant to degree
         float angle = Mathf.Atan2(lookPos.y, lookPos.x) * Mathf.Rad2Deg;
         //rotate the GameObject (transform.rotation, axis around the point to turn)
         transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
        // transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
 
         Debug.Log(transform.rotation);
     }
 }
 

I am using this for a 2D game. But the problem is, is that when I move my mouse over 90 degrees on the screen relative to my object, the player will keep turning in one direction very slowly, when I increase or decrease the degrees relative to the 90, the object will start turning faster and faster. I tried to find a solution and I know that the object must stop moving when it has aligned with the mouse position but I have no idea how to fix this.

Sorry for the English grammer if it's bad, but I tried my best :P I would appreciate it to get some help!

BTW the "// text" is what I wrote myself to help me understand what is going on. If this is wrong I would appreciate it if you could correct it as well.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Potatobob · Aug 28, 2016 at 03:41 PM

I found this answer for you by googling , "transform.lookat 2d".

Many topics such as this one appeared and were answered in the past. I did use many of them myself.

 void Update()
 {
          Vector3 diff = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
          diff.Normalize();
  
          float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
      transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 90);
 }

this code is probably what you want in your Update function. It is shorter , cleaner and work perfectly with 2D.

The only problem you might encounter with this is that your object might be pointing in the wrong direction (example he could have 90 degrees of offset with what you want.)

This can also be solved by changing the last part of the last line I shown you.

         transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 90); <------- the "-90" can be replaced by another number or simply removed.

I hope my english is not too bad and you will be able to use this to fix your issue.

Finally here is the link of the answer I looked up for you:http://answers.unity3d.com/questions/585035/lookat-2d-equivalent-.html

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 TitaniumVanguard · Aug 28, 2016 at 03:51 PM 0
Share

Thank you for your answer, and your English is good enough so I understand :P

But It hasn't fixed the problem for me, I just tried it out but it basically does the same thing :/

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

Changing a Transform's rotation to match a Vector using Quaternions 0 Answers

Order of the rotations around the axes with Euler angles 1 Answer

Quaternion (look)rotation issue (gimbal lock?) 0 Answers

Rotate clockwise/counterclockwise using Quaternion.Slerp() 0 Answers

strange rotation in 2d 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