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 Noel9386 · Oct 17, 2014 at 06:22 PM · 2d2d gamelookatmouse

[SOLVED]2D lookAt is off, any ideas?

Hello everyone and thanks for your time. I recently started a small personal project and have noticed that my look at is off most of the time. It will match up sometimes but not all the time and that just won't work for me.

alt text

The red line is leading to the mouse cursor while the blue line is where the turret is firing to.

Here is the bit of code I have doing it

 lookAtPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 Quaternion rot = Quaternion.LookRotation(transform.position - lookAtPos, Vector3.forward);
         
 transform.rotation = rot;
 transform.eulerAngles = new Vector3(0, 0, transform.eulerAngles.z);

That is in the update function.

whyisitoff.png (42.9 kB)
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 robertbu · Oct 17, 2014 at 06:56 PM 0
Share

As a guess, add between line and 2:

   lookAtPos.z = transform.position.z;

Assu$$anonymous$$g an orthographic camera, this line:

    lookAtPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

...will produce an x,y position of the finger but a 'z' position at the camera.

avatar image smoggach · Oct 17, 2014 at 08:24 PM 0
Share

ScreenToWorldPoint isn't very obvious how it works. The Z coordinate is extremely important because it represents how far INTO the scene FRO$$anonymous$$ the camera's position.

avatar image Noel9386 · Oct 17, 2014 at 09:57 PM 0
Share

Setting lookAtPos.z to transform.position.z just makes the turret move at 90 degree increments ins$$anonymous$$d of actively tracking the cursor

1 Reply

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

Answer by robertbu · Oct 17, 2014 at 11:47 PM

What side of your gun is the front when the gun has the rotation of (0,0,0)? I suspect you are looking at 2D here with the front on the right or up. If so, you cannot use LookAt() in the way you are using it here. My assuming was that since your original code was sorta working that font faces positive 'z' when the rotation is (0,0,0). For rotation of an object where front is the right side and the camera is facing has rotation (0,0,0), you can do:

  Vector3 dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
  float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
  transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

 
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 Noel9386 · Oct 19, 2014 at 11:06 PM 0
Share

This is no rotation applied to it all, the top part is what faces the mouse, however it is parented to an object that is rotated 180 on the z axis. I don't think that would be an issue though.

The bit of code you posted is 90 degrees off. alt text

EDIT:

I took the image into an editor and rotated it so that the barrel would be the right edge and it works like a charm, thanks for that bit of code!

whyisitoff2.png (33.4 kB)

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

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

Related Questions

2D game unity player flip problems 1 Answer

How can I make an object stop all momentum and hold it's position in air? 1 Answer

Instantiate GameObject at Random time is not working 1 Answer

Sprite will not roate when arrow keys are pressed 2 Answers

transform.up rotation? (2d game) 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