- Home /
Making the position of the mouse cursor, the reference point for forward movement (2D)
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
Answer by AshleyJamesy · Oct 25, 2015 at 04:54 PM
if (Input.GetKey("d"))
{
transform.position += Vector3.right * speed * Time.fixedDeltaTime;
}
if (Input.GetKey("a"))
{
transform.position -= Vector3.right * speed * Time.fixedDeltaTime;
}
if (Input.GetKey("w"))
{
transform.position += Vector3.forward * speed * Time.fixedDeltaTime;
}
if (Input.GetKey("s"))
{
transform.position -= Vector3.forward * speed * Time.fixedDeltaTime;
}
//Rotation
Vector3 m_TankPos = Camera.main.WorldToScreenPoint(transform.position);
Vector3 m_MouseDirection = Input.mousePosition;
m_MouseDirection.x = m_MouseDirection.x - m_TankPos.x;
m_MouseDirection.y = m_MouseDirection.y - m_TankPos.y;
m_MouseDirection.Normalize();
float m_Angle = Mathf.Atan2(-m_MouseDirection.y, m_MouseDirection.x) * Mathf.Rad2Deg;
GetComponent<Rigidbody>().MoveRotation(Quaternion.Euler(new Vector3(0, m_Angle - 270, 0)));
Your answer
![](https://koobas.hobune.stream/wayback/20220612041859im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
ScreenToWorldPoint question 1 Answer
moving object toward waypoint and alignment 0 Answers
2D Movement and getting the mouse position 0 Answers