- Home /
Question by
PervertDude · May 08, 2016 at 06:36 AM ·
2d gameaiming
rotate a 2D sprite towards mouse
I want to create a top-down 2D game and I have trouble with making the sprite rotate towards the mouse. When I boot up the game, the sprite sits there and does nothing. Here is my code
using UnityEngine;
using System.Collections;
public class Aim : MonoBehaviour {
private Vector3 mouse_pos;
public Transform Player;
private Vector3 object_pos;
private float angle;
void Update()
{
mouse_pos = Input.mousePosition;
mouse_pos.z = -20;
object_pos = Camera.main.WorldToScreenPoint(Player.position);
mouse_pos.x = mouse_pos.x - object_pos.x;
mouse_pos.y = mouse_pos.y - object_pos.y;
angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0, 0, angle);
}
}
Comment