- Home /
2D Top Down, Making Player rotate & move with mouse?
I am working on an all 2D top-down stealth game, and i would like for the player to rotate when i move the mouse. I have half of this done. Right now the player turns when the mouse moves, but the player doesn't move in the direction they're facing. i would like that to happen.
Here is the script i'm using, it is attached to the player,
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public int playerSpeed = 5;
// Update is called once per frame
void Update () {
transform.position += Vector3.right * Input.GetAxis ("Horizontal") * playerSpeed * Time.deltaTime;
transform.position += Vector3.up * Input.GetAxis("Vertical") * playerSpeed * Time.deltaTime;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
transform.LookAt(ray.GetPoint(0),Vector3.forward);
Camera.main.transform.position = new Vector3 (transform.position.x, transform.position.y, Camera.main.transform.position.z);
}
}
Sorry if that question was a bit confusing to any. Also, i am trying hard to learn how to script, most of my beginners knowledge being in Javascript.
Answer by robertbu · Jul 22, 2014 at 05:06 PM
Answering a question like this one always involves figure out how you've setup your scene. From this code I assume:
The camera is looking towards positive 'z' making your 2D action on the XY plane
The front of your player is the positive 'z' side.
You are using an Orthographic camera.
You don't indicate what controls forward movement, so I'll use 'Input.GetAxis("Vertical")'.
Note if you consider the positive 'z' side of your object the front, then 'transform.forward' will be the direction your object is facing regardless of the way it is facing. So one way to move forward would be:
transform.position * Time.deltaTime * playerSpeed * Input.GetAxis("Vertical") * transform.Forward;
Another way is to use Transform.Translate:
transform.Translate(0.0f, 0.0f, Time.deltaTime * playerSpeed * Input.GetAxis("Vertical);
You will need to remove lines 10 and 11. Also playerSpeed is better as a float rather than an int.
I would also suggest to put all the code in "FixedUpdate" and remove deltaTime.
@Bojidar - why? FixedUpdate() generally runs at a slower frame rate than Update(). Generally code dealing with Physics is moved into FixedUpdate() due to physics calculations.
Oh yeah, probably should have mentioned that stuff.
The camera is towards the positive Z axis
$$anonymous$$y character is the attached image, so i assume the Front is actually positive Y, the bottom of his feet being positive Z
I am using an Orthographic camera
WASD for controlling the player, I want the mouse to control what direction is facing/moves towards.
Ex. moving the mouse Northwest, the player rotates to the Northwest,Pressing "W" then results in moving forward,well, Northwest
The code solutions I outlined should work for this setup. 'W' and 'S' are the positive and negative keys for the "Vertical" axis. Note you have a problem here:
transform.LookAt(ray.GetPoint(0),Vector3.forward);
The second parameter to LookAt() is the preferred axis of rotation. For the setup you describe, I believe you want Vector3.up for that parameter.
Your answer

Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Having problems with 2d collision triggers (javascript) 1 Answer
GetComponent().enabled = true; 1 Answer
Help setting maxAceleration 0 Answers
Lifting box 2D C# 0 Answers