Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by Orggrim · Feb 14, 2013 at 07:38 AM · mouseshooteraimdowntop

How To Get Object To Aim at Mouse in Top Down 3D Shooter

Hi!

Ive looked everywhere for some code examples to maybe help solve my problem, but I have had no luck!

I have a player(represented in the picture below!)

alt text

Now I have the rectangular piece parented to the round piece, what I would like ideally is for the round piece to rotate around the ball and face the direction that the mouse is in.

Let me know if you should need anymore information!

Thanks again

pic1.jpg (69.8 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

4 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by robertbu · Feb 18, 2013 at 04:23 AM

Here is an implementation of @MatthewA 's suggestion:

 public class MouseFollowTopDown : MonoBehaviour {
     
     void Update() {
         if (Input.GetMouseButton (0))
             RotateToMouse ();
     }
     
     void RotateToMouse() {
         Vector3 v3T = Input.mousePosition;
         v3T.z = Mathf.Abs(Camera.main.transform.position.y - transform.position.y);
         v3T = Camera.main.ScreenToWorldPoint(v3T);
         v3T -= transform.position;
         v3T = v3T * 10000.0f + transform.position;
         transform.LookAt(v3T);
     }
 }
Comment
Add comment · Show 2 · 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 maihe-br · Mar 14, 2014 at 09:50 PM 1
Share

I cannot vote yet, but I would like to say that this works perfectly well for me. Thanks a lot!

avatar image robertbu · Mar 14, 2014 at 10:44 PM 0
Share

@maihe-br - Thanks for the feedback. I wrote that a year ago. I'd write it differently today:

 void RotateTo$$anonymous$$ouse() {
        Vector3 v3T = Input.mousePosition;
        v3T.z = $$anonymous$$athf.Abs(Camera.main.transform.position.y - transform.position.y);
        v3T = Camera.main.ScreenToWorldPoint(v3T);
        transform.LookAt(v3T);
     }
avatar image
0

Answer by OmarAlhaddad · Feb 14, 2013 at 03:57 PM

Use this formula to calculate the rotation angle, target would be the mouse position in your case:

 float angle = (Mathf.Atan2 (target.y, target.x) * Mathf.Rad2Deg) -90f;

you might need to tweak it a little bit depending on your setup.

Goodluck

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 Matthew A · Feb 14, 2013 at 10:19 AM

If this is a 2d shooter type thing, I think the easiest way is perhaps to use Camera.ScreenToWorldPoint, with Input.mousePosition as x and y, and the camera height for the Vector3 argument.

Then you could use transform.LookAt (or maybe RotateAround, with a bit more work to find the angle) to look at that point.

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 Orggrim · Feb 18, 2013 at 03:56 AM 0
Share

Well I tried to experiment around with these and failed horribly! Thanks for the input though!

avatar image
0

Answer by BarthaSzabolcs · May 04, 2021 at 01:35 PM

I know it's an old post, but this is still a valid problem.


I made a 3 minute tutorial about this with explanation and an example project.


My Solution

 using UnityEngine;
 
 namespace BarthaSzabolcs.IsometricAiming
 {
     public class IsometricAiming : MonoBehaviour
     {
         #region Datamembers
 
         #region Editor Settings
 
         [SerializeField] private LayerMask groundMask;
 
         #endregion
         #region Private Fields
 
         private Camera mainCamera;
 
         #endregion
 
         #endregion
 
 
         #region Methods
 
         #region Unity Callbacks
 
         private void Start()
         {
             // Cache the camera, Camera.main is an expensive operation.
             mainCamera = Camera.main;
         }
 
         private void Update()
         {
             Aim();
         }
 
         #endregion
 
         private void Aim()
         {
             var (success, position) = GetMousePosition();
             if (success)
             {
                 // Calculate the direction
                 var direction = position - transform.position;
 
                 // You might want to delete this line.
                 // Ignore the height difference.
                 direction.y = 0;
 
                 // Make the transform look in the direction.
                 transform.forward = direction;
             }
         }
 
         private (bool success, Vector3 position) GetMousePosition()
         {
             var ray = mainCamera.ScreenPointToRay(Input.mousePosition);
 
             if (Physics.Raycast(ray, out var hitInfo, Mathf.Infinity, groundMask))
             {
                 // The Raycast hit something, return with the position.
                 return (success: true, position: hitInfo.point);
             }
             else
             {
                 // The Raycast did not hit anything.
                 return (success: false, position: Vector3.zero);
             }
         }
 
         #endregion
     }
 }


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

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

Top down camera player rotating 1 Answer

Side Scrolling Shooter. How to aim my bullet trajectory at the mouse cursor? 2 Answers

Top Down Shooter Help 1 Answer

Knowing when mouse has been released 2 Answers

aim down sights of gun 3 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