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
1
Question by Frostbite23 · Dec 27, 2013 at 05:02 AM · 2dunity4.3

2D Mouse Aiming

Hey guys, I'm creating a 2D sidescroller game and I came across a problem I can't resolve, I've Spent 2 Days googling "2d mouse aiming" and others but I can't find any luck, can someone at least give a suggestion or a script that allows "2d mouse aiming" (an object that actually looks at the mouse cursor and rotates the object on its z axis) on a Sprite or Gameobject in 2d space?

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
13
Best Answer

Answer by robertbu · Dec 27, 2013 at 05:10 AM

There have been half a dozen different posts this issue at least since Unity 4.3 came out. I believe most of the code posted should do the job. Here is yet another implementation...a bit different than the other's I've seen:

 #pragma strict
 
 function Update () {
     var pos = Camera.main.WorldToScreenPoint(transform.position);
     var dir = Input.mousePosition - pos;
     var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
     transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward); 
 }
Comment
Add comment · Show 5 · 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 Frostbite23 · Dec 28, 2013 at 06:28 AM 0
Share

sadly this doesn't work, I'm actually planning to modify this hoping it will work

avatar image Frostbite23 · Dec 28, 2013 at 06:41 AM 0
Share

I'm using a different approach so ins$$anonymous$$d of making the object rotate to the cursor i will actually create a custom cursor (a game object in the scene) make it stick to the mouse actual mouse position and than make the object rotate to the custom cursor using "transform.LookAt()" and than 0 out the x and y rotation. I got my script to work but I don't know how to make it follow the cursor WITHIN the game/screen view.

heres what I have

 #pragma strict
 
 var mousePos : Vector3;
 var cursor : Transform;
 
 function Update () {
     mousePos = Input.mousePosition;
     cursor.position = (mousePos);
 }
avatar image robertbu · Dec 28, 2013 at 10:13 AM 2
Share

The code I posted absolutely does work. I tested it before I posted it, and I just now tested it again. It expects the standard 2D setup of the camera looking towards positive 'z'.

Do the following:

  • Start a new scene

  • Create a new Quad or Sprite and attach material or texture

  • Attach this script to the quad or sprite

  • Run the app

As for your script, you are mixing screen coordinates and world coordinates. In order to make the script work, you have to decide or derive a distance you want the object from the camera then convert to world coordinates. For example:

 public var distanceFromCamera = 10.0;
 
 function Update() {
     var pos = Vector3(Input.mousePosition.x, Input.mousePosition.y, distanceFromCamera);
     pos = Camera.main.ScreenToWorldPoint(pos);
     transform.position = pos;
 }
avatar image Frostbite23 · Dec 28, 2013 at 10:41 PM 0
Share

oh thanks it finally works

avatar image Minchuilla · Aug 06, 2014 at 01:21 PM 0
Share

For me robertbu first answer worked, its just simple trigonometry which will most likely be more efficient and accurate than using a gameObject tracking the cursors position.

avatar image
0

Answer by koosa · Oct 25, 2017 at 03:14 PM

how to decrease the speed of rotation @robertbu ,how do u decrease the speed of the rotation

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

19 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

Related Questions

How to move 2D object on touch in Unity 4.3 1 Answer

What will be better approach either to clone or create new object Unity4.3 1 Answer

A node in a childnode? 1 Answer

Pick 2D object with touch in Unity 4.3 0 Answers

App get crash on animation of 2d object in Unity 4.3 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