- Home /
Trouble with controls on top down fixed camera game.
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);
}
Answer by alpaca of zion · Jun 07, 2012 at 04:51 AM
I think your problem is they you are grabbing the screen cordnance and not converting them to 3d space. This means that the mouse in the lower left hand corner of the screen is at the point 0,0,0 in 3d space, assuming the y axis is set to zero. When the mouse is at the upper left hand corner your point would be at 640,0,480 depending on your resolution. What you need to do is use Camera.ScreenPointToRay and do a raycast to the point where y is 0.