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 LeftyTwoGuns · Oct 25, 2013 at 12:21 AM · screentoworldpointlookrotationmouse-look

Having trouble with LookRotation at mouse on one axis

I have a 2D game with a vehicle that moves left or right on the X-axis and a turret that points up a long the Y-axis. I tried writing my own script getting this turret to rotate left or right on the Z-axis to look at the mouse cursor, but it didn't quite work out as nothing is happening. The turret won't move at all. Here is my script:

 public float rotateSpeed;
     
     
     void FixedUpdate () {
         
         Vector3 mousePos = Input.mousePosition;
         Vector3 lookPos = Camera.mainCamera.ScreenToWorldPoint(mousePos);
         lookPos.x = 0;
         lookPos.y = 0;
         
         Quaternion targetRotation = Quaternion.LookRotation(lookPos - transform.position);
         transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotateSpeed);
     
     }
 }

I understand basically what LookRotation does but I think I'm plugging the wrong info into it. What am I doing wrong here?

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 · Oct 25, 2013 at 12:39 AM

Two issues here. For the calculation of ScreenToWorldPoint(), the 'z' parameter needs to be the distance in front of the camera. By default 'mousePosition' as a 'z' value of 0.0, so ScreenToWorldPoint() will return the position of the camera. Say your camera is at -10 on the 'z' axis and your 2D game has played a 'z' = 0.0. You would set 'mousePos.z = 10.0f' before you call ScreenToWorldPoint(). Again, you are setting the 'z' parameter to the positive distance in front of the camera to calculate the point.

The second issue is the axis that LookRotation uses as up. By default, it use Vector3.up, but since you are rotating around the 'z' axis, you need to use Vector.forward. Putting the two changes together:

 public float rotateSpeed = 45.0;
 
     void Update () {
        Vector3 mousePos = Input.mousePosition;
        mousePos.z = 10.0f;
        Vector3 lookPos = Camera.mainCamera.ScreenToWorldPoint(mousePos);
  
        Quaternion targetRotation = Quaternion.LookRotation(lookPos - transform.position, Vector3.forward);
        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotateSpeed);
     }

Note that this code assumes that you are aiming the forward of the game object this script it is attached to at the mouse position.

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 LeftyTwoGuns · Oct 25, 2013 at 02:06 AM 0
Share

Thank you so much, you explained it perfectly. Now I understand how ScreenToWorldPoint works. The script works now but the turret isn't rotating how I need it to. I'm just going to post a screenshot ins$$anonymous$$d of trying to explain it. I penciled in the axes of the gameworld and the arrows represent which way I need the turret to rotate.

alt text

The second screen is what the turrets do when the script is applied.

alt text

The model I made in Blender where the up is Z and forward is Y but Unity converts that to Y up and Z forward. $$anonymous$$anipulating the Z rotation of the turret in Unity gets it to move in the direction I need it to. So what exactly do I have to do with this model to aim up, point at the mouse, and rotate correctly?

example1.jpg (133.0 kB)
example2.jpg (127.8 kB)
avatar image robertbu · Oct 25, 2013 at 04:24 AM 0
Share

For your code to work, the barrel of the gun must point to positive 'z' when the rotation is (0,0,0). The other possible issue is that you did not correctly set the distance parameter. The distance in the ScreenToWorldPoint() needs to be the distance from the camera plane to the 2D plane you play your game on. Set it too far, and the gun rotation will not stay on the XY plane.

avatar image LeftyTwoGuns · Oct 25, 2013 at 05:16 AM 0
Share

I believe my set the distance correctly. The camera is set to -31 on Z-axis and I set it to 31.0f in the script.

I think the issue lies with my model. I'll mess around with that I think I might understand what's going on. But you have helped me fix and understand the script so thank you very much!

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

15 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

Related Questions

Can't use camera.main [SOLVED] 1 Answer

How to make a mesh into a FirstPerson Character? 2 Answers

What am I doing wrong with these rotations? 0 Answers

transform.right = (point on axis) after using LookAt? 2 Answers

Rotating about and only one axis 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