• 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
3
Question by user5200 · Jun 05, 2012 at 03:40 AM · 2drotationspritemousepositioncamera.screentoworld

(solved) 2d sprite rotation towards mouse

Hello,

I am trying to get a 2D sprite, the main character in my game, to point his head towards my mouse upon right click. After looking through a bunch of questions on this topic, I finally got my sprite to rotate along its Z axis to point at my mouse. However, there is still a horrible problem!

At first, I tried fancy stuff like transform.Lookat or Quaternion.LookRotation. But these were messing with my 2D sprites, changing the X and Y rotation. I finally settled on something that I got from here:

http://answers.unity3d.com/questions/10615/rotate-objectweapon-towards-mouse-cursor-2d.html

This is the code I am using:

 if (Input.GetButton("Fire2"))
             {
                  cameraDif = Camera.main.transform.position.y - transform.position.y;
                  mouseX = Input.mousePosition.x;
                     mouseY = Input.mousePosition.y;
                  mWorldPos = Camera.main.ScreenToWorldPoint( new Vector3(mouseX, mouseY, cameraDif));
                  mainPos = Camera.main.ScreenToWorldPoint(transform.position);
             
                 diffX = mWorldPos.x - mainPos.x;
                 diffY = mWorldPos.y - mainPos.y;
             
                 float angle = Mathf.Atan2(diffY, diffX) * Mathf.Rad2Deg;
                 transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle + 90));

This works exactly as I want it to. Except for one problem. Wherever I click on screen, my character's head points to the right. Only if I click far off-screen to the left, he begins to point his head to the left. The point that he changes his orientation, is precisely the left edge of the screen bounds. He also doesn't like to point down although he's apparently cool with pointing up.

It seems like problem here is that either my mouse coordinates are iffy somehow or something went wrong with camera.ScreentoWorld. But I cannot see what could be wrong.

What can I do to fix this?

EDIT:

The problem has been fixed. I was mindlessly converting transform.position with ScreentoWorld when it is already a world position. Silly me.

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 kolban · Jun 05, 2012 at 04:41 AM

I am not understanding your code ... but there is something that seems off here:

mainPos = Camera.main.ScreenToWorldPoint(transform.position);

The ScreenToWorldPoint() function takes as input a screen position and returns a world position ... but transform.position is already a world position (and not a screen position). I have no idea what calling ScreenToWorldPoint() with input of a world position would do.

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 user5200 · Jun 05, 2012 at 06:42 PM 0
Share

Ah thanks! That was it! I knew it was something stupid!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

2D LookAt not working as intended 1 Answer

2 fingers sprite rotation 1 Answer

Tryna get the position of the mouse in world space 1 Answer

Simulate character torso rotation in 2d game 0 Answers

Rotate the weapon sprite with a joystick 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