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):
When I move it up I want to accomplish this:
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?
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
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.
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
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