mrboberson
mrboberson

Reputation: 3

Unity top down 2d rotate to mouse

/So I am new to unity and I'm building a game. I want it to be a top down shooter similar to hotline Miami. I am trying to get the sprite to rotate to face to mouse. How would I accomplish this in JavaScript?/

pragma strict

public var moveSpeed : float = 12f;

public var turnSpeed : float = 50f;

function Start () {

}

function Update () {

 var mousePosition = Vector2;

if(Input.GetKey(KeyCode.D))
transform.Translate(Vector3.right * moveSpeed * Time.deltaTime );

if(Input.GetKey(KeyCode.S))
transform.Translate(Vector3.down * moveSpeed * Time.deltaTime );

if(Input.GetKey(KeyCode.A))
transform.Translate(Vector3.left * moveSpeed * Time.deltaTime );

if(Input.GetKey(KeyCode.W))
transform.Translate(Vector3.up * moveSpeed * Time.deltaTime );

transform.LookAt(mousePosition);

}

Upvotes: 0

Views: 1389

Answers (1)

Erik Uggeldahl
Erik Uggeldahl

Reputation: 1146

You probably want Transform.LookAt. This function requires that you fix one vector as the "up" direction, which in your case would be the one axis that you're not using (e.g. Z if your characters move in X and Y).

Upvotes: 0

Related Questions