Im trying to code a top down game with geometry wars type controls but it It has a single fixed top down camera looking straight down in the middle of the room. I want the player to move around the room freely with the arrow keys and always look in the direction of the mouse. Right now the movement works but the character rotation/always looking toward the mouse is messed up. The character sort of looks at the mouse but it sorta lags and sometimes looks in the wrong direction like if i move the mouse quickly to the opposite side of the player it wont rotate 180 and continue looking at the mouse. I want it to be snappy and always fixed facing the direction of the mouse. Heres what I have so far, I started out by borrowing some code from the Evac-City tutorial. Thanks a lot
function Update () {
FindPlayerInput();
ProcessMovement();
}
//game objects (variables which point to game objects)
var objPlayer : GameObject;
//input variables (variables used to process and handle input)
var inputRotation : Vector3;
var inputMovement : Vector3;
var moveSpeed = 100;
var tempVector : Vector3;
var tempVector2 : Vector3;
function FindPlayerInput ()
{
// find vector to move
inputMovement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
//position of player
tempVector2 = new Vector3(objPlayer.transform.position.x, 0 , objPlayer.transform.position.z);
tempVector = Input.mousePosition; // find the position of the mouse on screen
tempVector.z = tempVector.y; // input mouse position gives us 2D
//coordinates, I am moving the Y coordinate to the Z coorindate in temp Vector and setting the Y
//coordinate to 0, so that the Vector will read the input along the X (left and right of screen) and Z
//(up and down screen) axis, and not the X and Y (in and out of screen) axis
tempVector.y = 0;
}
function ProcessMovement()
{
objPlayer.rigidbody.AddForce (inputMovement.normalized * moveSpeed * Time.deltaTime);
//Debug.Log(tempVector);
objPlayer.transform.LookAt(tempVector);
objPlayer.transform.rotation.x = 0;
objPlayer.transform.rotation.z = 0;
objPlayer.transform.position = new Vector3(transform.position.x,transform.position.y,transform.position.z);
}