- Home /
How can I let my Sprite look towards the mouse in Unity 4.3 2D?
I know the last part is wrong, but this is my code: sing UnityEngine; using System.Collections;
public class LookAtMouse : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 mouseLoc = Input.mousePosition;
Vector3 Position = transform.position;
Vector3 Direction = mouseLoc - Position;
float Angle = (float)(Mathf.Atan2(Direction.x, Direction.y)) * Mathf.Rad2Deg;
transform.rotation = (Angle);
}
}
Comment
Answer by cadw96 · Nov 17, 2013 at 08:35 PM
Fixed it :) using UnityEngine; using System.Collections;
public class LookAtMouse : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 upAxis = new Vector3(0,0,-1);
Vector3 mouseScreenPosition = Input.mousePosition;
mouseScreenPosition.z = transform.position.z;
Vector3 mouseWorldSpace = Camera.mainCamera.ScreenToWorldPoint(mouseScreenPosition);
transform.LookAt(mouseWorldSpace, upAxis);
transform.eulerAngles = new Vector3(0,0,-transform.eulerAngles.z - 90);
}
}
Hey Cadw96, could you show me how you resolved this? I'm having the same issue and I can't seem to resolve it. Thanks for your help!
Your answer
Follow this Question
Related Questions
Look at mouse 1 Answer
smooth raycast LookAt ? 1 Answer
Mouse look Y axis sensitivity doesn't change 0 Answers
Animation blocks my vertical mouse look? 0 Answers
Object reference not set to an instance of an object 0 Answers