0
votes

/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);

}

1
What have you tried so far? What is your problem exactly? You can read a lot of tutorial about making games for Unity. - Ludovic Feltz
Are you in 2d mode? If so try this solution: answers.unity3d.com/answers/1141082/view.html The general idea though is to find the angle between your transform and the angle you want to look towards, and lerp the difference with the delta time step. - asawyer
Ive tried so much I can't remember it all. I worked on it for two hours and I'm writing this on the way home. - mrboberson

1 Answers

0
votes

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).