• 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 Brandon236 · Feb 21, 2017 at 07:03 AM · movementmousecontrolsaim

Controlling player with a mouse

I'm making a top down 2.5d game where the player controls a tank and uses the WASD keys to move and the mouse to aim. What I want is for the take turret to point towards wherever the mouse cursor is. The turret is attached to the tank body and moves wherever the body moves to. Here's what I have:

     void Update () {
         Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition + Vector3.forward * 10f);
         float angle = AngleBetweenPoints(transform.position, mouseWorldPosition);
         Vector3 mousePos = Input.mousePosition;
 
         //To make mousePos relative to center of screen
         mousePos.x -= Screen.width / 2;
         mousePos.y -= Screen.height / 2;
 
         //To make mousePos relative to transform
         mousePos += transform.position;
         angle = Vector3.Angle(mousePos, Vector3.up);
 
         //For 360 degree angle
         if (mousePos.x > 0)
             angle = 360 - angle;
 
         transform.rotation = Quaternion.Euler(0, -angle, 0);
         if (Input.GetKeyDown (KeyCode.Mouse0)) {
             Instantiate(bullet, bulletPosition.transform.position, transform.rotation);
         }
     }
 
     float AngleBetweenPoints(Vector2 a, Vector2 b) {
         return Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg;
     }

Now this works when the tank is in the center of the screen however when the tank is at one side of the screen then it doesn't work anymore. So how can I make the mouse aim work no matter where the player is on screen?

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

2 Replies

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

Answer by LiloE · Feb 21, 2017 at 09:55 AM

This should work:

 var worldUp = transform.TransformDirection(Vector3.up);
 var rotation = Quaternion.LookRotation(mouseWorldPosition  - transform.position, worldUp); 
 transform.rotation = new Quaternion(0, 0, rotation.z, rotation.w);
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 Brandon236 · Feb 22, 2017 at 03:22 AM 0
Share

I'm having problems with your script. Am I supposed to replace my code with yours or just replace a certain part of my code with yours?

avatar image LiloE Brandon236 · Mar 04, 2017 at 01:22 PM 0
Share

Sorry for the late response, for some reason I'm not getting notifications about comments :-( You should replace most of the code, just get the mouseWorldPosition like you did, and the rest is replaced with these three lines.

avatar image Brandon236 LiloE · Mar 04, 2017 at 05:30 PM 0
Share

It looks like this could work however it's not rotating on the correct axis. $$anonymous$$y game is a 3d game but the camera is looking down so I would have to rotate it on the y axis and your script looks like it's rotating on the z axis. What would I need to change to make this happen?

avatar image
0

Answer by SirDogey · Feb 21, 2017 at 12:26 PM

You Could Import the first person or third person character assets from the default assests

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

116 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 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 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 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

Unity 3D: Third person movement and mouse camera control 0 Answers

Move object away or closer while dragging with mouse 0 Answers

when my character rotates the controls of the initial rotation stay and not change with it 0 Answers

Using MoveTowards to move GameObject from starting point to mouse cursor 0 Answers

How to unable player to move on GUI buttons 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