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 /
  • Help Room /
avatar image
3
Question by petrbena · Sep 09, 2014 at 12:04 PM · 2d

How can I rotate 2d object in direction of mouse pointer

I am working in 2d. I need to accomplish a very simple thing. I have a prefab and I need to rotate it during the runtime in a direction where mouse pointer is. Basically I need this (black point is a mouse pointer): alt text

When I move it up I want to accomplish this:

alt text

I created a simple script for that:

 Vector3 position = Input.mousePosition;
 position.z = this.transform.position.z;
 this.objrigid.transform.rotation = Quaternion.FromToRotation(position, this.objrigid.position);

I was assuming that FromToRotation will rotate object in direction from 1 point to another point, but it doesn't really do that, object is randomly rotated when I move mouse, but in totally wrong directions. How would I accomplish this?

q2.png (14.5 kB)
q1.png (10.4 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
5
Best Answer

Answer by lepickle · Sep 09, 2014 at 01:07 PM

I use this script to make my player sprite look towards the mouse

     Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position);

Hope it'll work for you. :D

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 0243 · Mar 27, 2016 at 01:26 PM 0
Share

Thank you, it fit perfectly even with my left/right sprite swapping.

It made my character's head forward direction though, so, if anybody experiences this, here's how I dealt with that.

 if (facingRight)
         {
             transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position) * Quaternion.Euler(0, 0, 90);
         }
         else if (!facingRight)
         {
             transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position) * Quaternion.Euler(0, 0, -90);
         }

So a Quaternion is enough to rotate it anywhere. It took me an hour to realize that (mmm, quaternions), so mb anybody will need it too.

avatar image c2_cc · May 02, 2016 at 10:12 PM 0
Share

Thank you so much!!

avatar image alexjhones286 · Feb 02, 2019 at 06:12 PM 0
Share

Obrigado'. Brasil aqui'. :)

avatar image
1

Answer by VesuvianPrime · Sep 09, 2014 at 12:44 PM

Hey Petrbena

Your mistake is a very common one! Input.mousePosition returns a Vector3 in screen co-ordinates, whereas Transform.position returns a Vector3 in world co-ordinates.

In order to convert the co-ordinates you will need to use a Camera: http://docs.unity3d.com/ScriptReference/Camera.WorldToScreenPoint.html

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
1

Answer by ThePersister · Sep 09, 2014 at 01:03 PM

I've made this script that gets a normalized Direction towards the mouse. It does this based on your ViewPort, Just give it a shot and play around with it.

 using UnityEngine;
 using System.Collections;
 
 public class NormMouse : MonoBehaviour {
 
     public static NormMouse Instance;
     void Awake() { Instance = this; }
 
     Vector3 viewPortActual, viewPortDelta, directionToMouse;
 
     /// <summary>
     /// Retrieves a Normalized Vector3 Direction from mPosition towards the MousePos
     /// </summary>
     /// <returns>a Normalized Vector3 Direction from mPosition towards the MousePos (as WorldPos)</returns>
     public Vector3 GetNormalizedDirectionToMouse()
     {
         // Calculate a direction based on the Mouse Position (using Viewport)
         viewPortActual = Camera.main.ScreenToViewportPoint(Input.mousePosition);
         viewPortDelta = new Vector3(0.5f, 0.5f, 0); // Detract Half the screen
         directionToMouse = viewPortActual - viewPortDelta; // Caculate Direction
         return directionToMouse.normalized; // Return normalized Direction
     }
 }

Here, I made another Class for you that looks at the position using my above code from afar :) Make sure the NormMouse script is somewhere in the scene, and there can be only one of them, because Singleton is applied :)

 using UnityEngine;
 using System.Collections;

 public class LookAtMouse : MonoBehaviour {

 Transform mTransform;

 void Start()
 {
     mTransform = transform;
 }
 
 // Update is called once per frame
 void Update () {
     mTransform.LookAt(mTransform.position + NormMouse.Instance.GetNormalizedDirectionToMouse());
 }


I think that's about the best explanation I can give you, but if you have any more questions, or need any details, fire away! If not, please accept this answer ;) Best of luck ;3

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2d fixed path patrolling 0 Answers

Issue: deactivated animated sprites reappear and go crazy on animation state transition 0 Answers

For Dimetric Game, use Unity 2D or 3D? 0 Answers

Random position, overlap problem. 1 Answer

Raw image flickering when texture changed 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