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 Wakeful · Jan 12, 2015 at 07:03 AM · c#screentoworldpointrotatetowardsinput.mouseposition

Unity Look at Mouse Position 2D C#

Hey

I'm trying to make a top down 2D tank game where the turret looks at the mouse. I have a simple script that I thought was working but I have noticed that if the mouse is far away from the tank and the mouse is at an angle to the tank it becomes inaccurate. I want the tank turret to look directly at the mouse at any angle.

Script:

 var mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
 Quaternion rot = Quaternion.LookRotation (turretFollow.transform.position - mousePosition, Vector3.forward);
 
 Debug.DrawRay(turretFollow.transform.position, mousePosition);
 Debug.DrawRay(turretFollow.transform.up, turretFollow.transform.up * 2, Color.red);

 turretFollow.transform.rotation = rot;
 turretFollow.transform.eulerAngles = new Vector3 (0, 0, turretFollow.transform.eulerAngles.z);
 turretFollow.rigidbody2D.angularVelocity = 0;
 float step = turretSpeed * Time.deltaTime;
 turret.transform.rotation = Quaternion.RotateTowards(turret.transform.rotation, turretFollow.transform.rotation, step);


I have some photos to help explain what I'm talking about. The red line is the current angle its looking at which is wrong. The white line is the angle I want it to look at but its not and I cant figure out why.

The angle is wrong when around a 45 degree angle and when the cursor is far away: alt text

It does work tho when the cursor is in front of the tank: alt text

Hope you can understand my issue and can help! Thx in advance

If anyone has any questions just ask! ~Wakeful

tank problem 1.png (490.7 kB)
tank problem 2.png (488.9 kB)
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

3 Replies

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

Answer by Taxen0 · Jan 12, 2015 at 08:51 AM

Try the following and see if it works for you, I haven't tried it but it should be ok:

 var mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
 Quaternion rot = Quaternion.LookRotation (mousePosition - turretFollow.transform.position, turretFollow.transform.TransformDirection(Vector3.forward));

 turretFollow.transform.rotation = new Quaternion(0, 0, rot.z, rot.w);
  



Comment
Add comment · 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
0

Answer by Baste · Jan 12, 2015 at 08:53 AM

You are setting the rotation three times in the same algorithm. The two first ones are being overridden by the third. You're probably a bit confused about what you want to do.

The last one you can safely ditch:

 turret.transform.rotation = Quaternion.RotateTowards(turret.transform.rotation, turretFollow.transform.rotation, step);


This will have your tank rotate towards looking in the same direction as the turretFollow is looking, which is not what you want.

Then you have these two lines:

 turretFollow.transform.rotation = rot;
 turretFollow.transform.eulerAngles = new Vector3 (0, 0, turretFollow.transform.eulerAngles.z);

which are trying to do the same thing. The second line will override the first one.

If you want the tank to just look at the thing, use one of those two lines. The one where you set rotation directly is the one you want unless your tank tends to rotate on the other axes.

If you want the tank to rotate over time, the last line is correct, but you want to rotate towards the look rotation, not the rotation of the target:

  turret.transform.rotation = Quaternion.RotateTowards(turret.transform.rotation, rot, step);

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 Wakeful · Jan 12, 2015 at 05:46 PM 0
Share

Sorry just to clarify I do what the turret to rotate over time. I use the turret follow to set the wanted rotation then I make the turret rotate over time towards the turret follow but something is not working and giving me the wrong rotation.

Edit: sorry for the poorly named variables

avatar image
0

Answer by skylem · Jan 12, 2015 at 05:52 PM

use LookAt(); heres an example LookAt(transform); // what you need to use,

 How i've used this in the past.
 
         if(hudTarget != null) {
             if(hudObject.activeSelf == false) {
                 hudObject.SetActive(true);
             }
             hudObject.transform.rotation = Quaternion.LookRotation(hudObject.transform.eulerAngles, hudTarget.transform.eulerAngles);
             hudObject.transform.LookAt(hudTarget.transform);
         }
         else if(hudTarget == null) {
             if(hudObject.activeSelf == true) {
                 hudObject.SetActive(false);
             }

this code makes n Objects xform(in this case my HUD Arrow) point at another transform.

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Trying To Find Mouse Position On Tap 1 Answer

Converting Input.mousePosition to world coordinates 1 Answer

Making a bubble level (not a game but work tool) 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