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 reberk · Mar 06, 2013 at 03:00 AM · 2drotationjavascriptaxes

2D: Rotating to face mouse position with two fixed axes

I'm working on a 2D sidescrolling platformer with keyboard movement and mouse aiming controls. The character's arms should point in the direction of the mouse cursor on the screen, but rotate independently from the body of the character. Additionally, I'd like to have an easy reference to the rotational position of the arms so that I might apply forces in that direction.

Making the arms children of the character object, I can get them to point to the mouse with the script below, but I run into one problem. Whenever the mouse crosses the vertical centre of the screen, the y-rotation of the arms flips, essentially reversing the position of the near and far arms of the character. This also has the side effect of altering the x-rotation, making it so I can't count on anything related to it to act as expected in world space.

Is there a better way to do this?

 function Update (){
 var mousePos = Input.mousePosition;
 mousePos.z = 10.0f; //The distance from the camera to the player object
 var lookPos : Vector3 = Camera.main.ScreenToWorldPoint(mousePos);
 transform.LookAt(lookPos);
 }
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
2
Best Answer

Answer by robertbu · Mar 06, 2013 at 03:17 AM

I think this is what you are looking for, or at least a start. It is different for the code above in that it aligns the right of the object (as opposed to the forward) with the mouse. If your object is not authored that way, you might be able to fix it by making the arms children of an empty game object and attaching the script to the empty game object.

As for adding force in the direction of the arms, depending on your frame of reference it will be either transform.right or -transform.right.

 function Update () {
     var mousePos = Input.mousePosition;
     mousePos.z = 10.0f; //The distance from the camera to the player object
     var lookPos : Vector3 = Camera.main.ScreenToWorldPoint(mousePos);
     lookPos = lookPos - transform.position;
     var angle : float = Mathf.Atan2(lookPos.y, lookPos.x) * Mathf.Rad2Deg;
     transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
 }
Comment
Add comment · Show 3 · 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 reberk · Mar 06, 2013 at 03:49 AM 0
Share

Interesting! I'd never thought to align it anywhere except along the z axis, but this certainly does the job. For some reason, though, this conditional never returns true, despite the rotation values being visibly within that range:

 if ((transform.rotation.z <=270) && (transform.rotation.z >=90))

Should I be accessing the right/left split differently?

avatar image robertbu · Mar 06, 2013 at 04:07 AM 0
Share

'transform.rotation' is a Quaternion. Unless you understand Quaternions, it is recommended you not read values or set values. You can use transform.eulerAngles, though you have to be careful there as well. Quoted from the reference for eulerAngles:

Do not set one of the eulerAngles axis separately (eg. eulerAngles.x = 10; )

And as for reading, multiple different combinations of euler values can add up to the same rotations.

Here's one way to access the left/right split:

 if (Vector3.Angle(Vector3.right, transform.right) <= 90)
     // Do the right point stuff;
 else
     // Do the left pointing stuff

Vector3.Angle() returns an unsigned angle between two vectors. This code compares the world right with the 'right' vector of the game object.

avatar image reberk · Mar 06, 2013 at 04:12 AM 0
Share

Ah! That does it! Thanks a lot for all your help. I'm going to take a few good notes form all this for my ongoing and future reference. Again, very much appreciated.

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

10 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Locking move direction and rotation in JavaScript 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to tell if two objects are right next to each other in a 2d game? 2 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