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 arcade1982 · Jan 07, 2015 at 12:28 AM · spritequaterniontransform.positionmathfinput.mouseposition

player mouse rotation, 2D twin shooter. player won't rotate.

Hey guys, I'm trying to get my player ship to rotate with my mouse. I've checked and re-checked and still can't get it to work. The player is a 2D sprite currently in a 'still' position and haha, just won't move. My background and player are in reset positions. Here's the coding below, I hope someone has an idea or sees something that can be fixed, it has been pretty frustrating. Thanks in advance.

 void Update ()
 {
     Rotation ();
 }

 //rotate player to face mouse
 void Rotation ()
     {
     //finding where the mouse is relative to the player
     Vector3 worldPos = Input.mousePosition;
     worldPos = Camera.main.ScreenToWorldPoint(worldPos);

     //get the difference from each axis (delta x and y)
     float dx = this.transform.position.x - worldPos.x;
     float dy = this.transform.position.y - worldPos.y;

     //get the angle between the two objects
     float angle = Mathf.Atan2(dy, dx) * Mathf.Rad2Deg;

     Quaternion rotation = Quaternion.Euler(new Vector3(0, 0, angle));


         // Assign the ship's rotation
     this.transform.rotation = 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

1 Reply

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

Answer by DanSuperGP · Jan 07, 2015 at 12:50 AM

The Z element of the vector passed to Camera.ScreenToWorldPoint represents the distance from Camera. Since you want the touch to be in the same plane as the ship you are rotating, you need to set the Z value to the distance from the camera to the plane your sprite is in. If you haven't changed the camera from it's default position, that would be 10.

So, if you add

worldPos.z = 10.0f;

After line 10 before line 11 it will work.

The way you have it now, no matter where you move the mouse, it is returning the camera's location because the distance from the camera is zero.

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 arcade1982 · Jan 07, 2015 at 01:04 AM 0
Share

Thanks for the advice, tried it out and she spins like a ballerina. I'll remember that for the next time.

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

Attempting to Calculate and Change Scale of Platforms Sets Scale to 0 1 Answer

Mathf.Atan changes instantly even with a slow lerped value 1 Answer

What is the correct way to move an object to a target location? 2 Answers

Postive and Negative Button Binding 3 Answers

SmoothDamp from 360 to 1 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