- Home /
Question by
Supercrusher9000 · Jul 29, 2014 at 12:59 AM ·
2dmouselook
Top Down 2D Mouse Look
So, I am making a top down game, and instead of having my code make the player look to where ever the mouse cursor is, I want it to find where the mouse cursor is on start and then only move in response to what direction the mouse moves. Here is the code that I used, please respond in JS.
var mouse_pos : Vector3;
var target : Transform; //Assign to the object you want to rotate
var object_pos : Vector3;
var angle : float;
function Update ()
{
mouse_pos = Input.mousePosition;
mouse_pos.z = 5.23; //The distance between the camera and object
object_pos = Camera.main.WorldToScreenPoint(target.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(Vector3(0, 0, angle));
}
Comment
Your answer
Follow this Question
Related Questions
2D Mouse Look with Collisions 1 Answer
Problem with Mathf.Clamp and Mouse Follow Script 1 Answer
How to stop object from glitching when coming too close to mouse? 0 Answers
2D Animation does not start 1 Answer
2D mouselook 4 Answers