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 Kayleb · Mar 07, 2016 at 03:21 PM · physicsmouserotateaxis

Rotate Cube Depending On Where The Cursor Is

Hello, I am making a top down perspective game I want the player (in this case a cube) to rotate around like a compass North, West,East,South but also having the ability to be facing NorthWest and SouthEast and everywhere in between. Can anyone throw me in the right direction with a script tutorial?

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

Answer by Magius96 · Mar 07, 2016 at 04:26 PM

If this were a 3d game, I'd say transform.LookAt() but that doesn't exactly work correctly for 2D games. Instead I use this method:

 public static void LookAt2D(ref GameObject obj, Transform target)
 {
     float angle = 0;

     Vector3 relative = obj.transform.InverseTransformPoint(target.position);
     angle = Mathf.Atan2(relative.x, relative.y) * Mathf.Rad2Deg;
     obj.transform.Rotate(0, 0, -angle);
 }

This rotates the object on the Z plane to look at the target.

Comment
Add comment · Show 2 · 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 Statement · Mar 07, 2016 at 05:11 PM 0
Share

Out of total curiosity, why do you pass the game object by ref?

avatar image Magius96 Statement · Mar 08, 2016 at 02:17 PM 0
Share

It allows me to make changes to the game object without having to return it. To be honest, I have this method in my code as an extension method so I don't have to pass it by ref. To change it to an extension method, make sure you put it into a static class an change "ref" to "this".

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

X-Axis rotation quirk 0 Answers

Rotate player with mouse. 0 Answers

Rotate to look at object, but x and y axis has to be static,How to make a game object look at another game object? 1 Answer

Rotate OBJECT to face mouse cursor for x/z based top down shooter 3 Answers

Como puedo eliminar el error CS1026 )expected? 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