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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Axteroide · May 05, 2014 at 06:57 PM · 2drotationlookat

[Solved] Rotating with lookat on 2D

I'm trying to rotate an object on 2D unity on android, just pretend to look at the position of the touch, but when i do the lookat, the object gets crazy and rotate on angle x and y but no z. My code is simple:

 if(Input.GetMouseButton(0)){
     Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     pos.z = 0;
     this.transform.LookAt(pos);
 }

What am i doing wrong?

Comment
Add comment · Show 3
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 thaiscorpion · May 05, 2014 at 07:23 PM 0
Share

is the object you want to look at the position of the click also on position.z = 0? because if it isn't that would make it move in all angles, if it is at the same z position then it would only change on the x and y axis.

avatar image Axteroide · May 05, 2014 at 08:49 PM 0
Share

All my objects are on z=0. Does not have to change the rotation on z axis? If i can only move on x and y, the rotation should be i think on z axis. I don't see the sense rotating x and y.

avatar image robertbu · May 09, 2014 at 06:20 PM 0
Share

http://answers.unity3d.com/questions/603757/2d-mouse-ai$$anonymous$$g.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Pyrian · May 06, 2014 at 12:35 AM

I never got LookAt to work, either. Arctangent does the job, though. Here's a snippet from my code (CurFlip is just 1 or -1 based on whether the sprite's X scale is 1 or -1 at the time):

 transform.rotation = Quaternion.Euler (0.0F, 0.0F, Mathf.Atan2 ((CurY - transform.position.y) * CurFlip, (CurX - transform.position.x) * CurFlip) * Mathf.Rad2Deg);
Comment
Add comment · Show 5 · 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 Axteroide · May 06, 2014 at 08:09 PM 0
Share

Didn't work neither. It point only to one side (right or left depending on CurFlip) on PC, and Android only rotate a few pixels. I'm trying everything i can see and don't get anything.

avatar image Pyrian · May 06, 2014 at 10:40 PM 0
Share

Well, that's a straight copy of working code - tested on PC, web, and Android. The symptom you describe sounds like you left out the "* $$anonymous$$athf.Rad2Deg" at the end, or just aren't getting a valid y for whatever reason.

avatar image Axteroide · May 09, 2014 at 05:00 PM 0
Share

I tried the code on my laptop and worked fine. But when i execute on an android device it just face every time to the same point (i show the coordinates of the touch point and it works fine) $$anonymous$$y code for android is:

 else if(SystemInfo.deviceType == DeviceType.Handheld){
     if(Input.touchCount > 0){
     Vector2 touchPos = Input.GetTouch(0).deltaPosition;
     Vector3 pos = Camera.main.ScreenToWorldPoint(touchPos);
     pos.z = this.transform.position.z;

         
             
     float CurFlip = 1.0f;
     float atan = $$anonymous$$athf.Atan2 ((pos.y - transform.position.y) * CurFlip, (pos.x - transform.position.x) * CurFlip) * $$anonymous$$athf.Rad2Deg;
     transform.rotation = Quaternion.Euler (0.0F, 0.0F, atan);
     Acelerate(); // Add force to the object                    
     }
 }

I'm getting mad with android...

avatar image Pyrian · May 09, 2014 at 09:51 PM 0
Share

You're using deltaPosition where you should be using position.

avatar image Axteroide · May 10, 2014 at 09:36 AM 0
Share

I just see it and was going to post it. This change fix the problem. I changed so many times my code i forget to chang that... So that is it. Problem solved. Thank you all.

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D LookAt not working as intended 1 Answer

transform.up flips gameobject by it's x-axis when transform.up.x is 0 and when transform.up.y is negative 0 Answers

LookAt() not working with mouse as target in 2D 1 Answer

BOX ROTATE ONLY ON X AXIS When Look AT!!HELP! 0 Answers

2D Sprite Rotation 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