Hello
Complete newbie here so I would be very grateful for any help.
I am trying to make an overhead shooter thing (similar to Hotline Miami in terms of style) where the player will can be moved forward, depending on which way they are facing.
So I managed to work out how to move a gameobject in 2d, using getkey. Now a block will move, using the WASD keys. W is up, D is down, A for left, D for right
I managed to work out how to get the gameobject to rotate towards the mouse position.
However, when I put them together, it doesn’t seem to work how I want. It moves just like Hotline Miami but I want something slightly different. I want to make it so that wherever the mouse cursor is, by pressing W, I want the character to move forwards towards it and not move up the screen.
How do I make it so the gameobject will move forward towards the mouse cursor (and back and left and right) in relation to its current rotation.
Here is the code for movement:
var speed : int = 2;
function Start () {
}
function Update () {
if (Input.GetKey ("d")){
transform.position += Vector3(speed * Time.deltaTime, 0, 0); }
if (Input.GetKey ("a")){
transform.position -= Vector3(speed * Time.deltaTime, 0, 0); }
if (Input.GetKey ("w")){
transform.position += Vector3(0, speed * Time.deltaTime, 0); }
if (Input.GetKey ("s")){
transform.position -= Vector3(0, speed * Time.deltaTime, 0); }
}
Here is the code for rotation:
function Update ()
{
var mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
var rot = Quaternion.LookRotation(transform.position - mousePosition, Vector3.forward);
transform.rotation = rot;
transform.eulerAngles = Vector3 (0, 0, transform.eulerAngles.z);
GetComponent.<Rigidbody2D>().angularVelocity = 0;
}
Hope that makes sense. Thank you in advance