I’m trying to get a plane to rotate on its Z axis only for a 2D top down shooter concept. I finished the Evac City tutorial and now am rewriting the code so that the games horizontal and vertical axes are X & Y (rather than X & Z like in the tutorial).
Here is my code that does not rotate the player sprite at all:
void Update () {
FindInput();
ProcessMovement();
if (thisIsPlayer == true) {
HandleCamera();
}
}
void FindPlayerInput () {
// find vector to move
inputMovement = new Vector3( Input.GetAxis("Horizontal"),Input.GetAxis("Vertical"),0 );
tempVector2 = new Vector3(Screen.width * 0.5f,Screen.height * 0.5f,0);
tempVector = Input.mousePosition;
tempVector.z = 0;
inputRotation = tempVector - tempVector2;
}
void ProcessMovement () {
rigidbody.AddForce( inputMovement.normalized * moveSpeed * Time.deltaTime );
transform.rotation = Quaternion.LookRotation(inputRotation);
transform.eulerAngles = new Vector3(0,0,transform.eulerAngles.z);
transform.position = new Vector3(transform.position.x,transform.position.y,0);
}
I’ve been stuck on this for about 8 hours of coming back to it and I’m losing my mind. The code in the tutorial (see below) works perfectly fine, all I am doing is leaving the Z axis perpendicular to the camera instead of swappying Z with Y like the tutorial shows.
Here is the tutorial code:
void Update () {
FindInput();
ProcessMovement();
if (thisIsPlayer == true) {
HandleCamera();
}
}
void FindPlayerInput () {
// find vector to move
inputMovement = new Vector3( Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical") );
// find vector to the mouse cursor / the position of the middle of the screen
tempVector2 = new Vector3(Screen.width * 0.5f,0,Screen.height * 0.5f);
// find the position of the mouse cursor on screen
tempVector = Input.mousePosition;
// input mouse position gives us 2D coordinates, moving Y coordinate to the Z coordinate in tempVector and setting the Y coordinate to 0 so the Vector will read the input along the X and Z axes (instead of X and Y)
tempVector.z = tempVector.y;
tempVector.y = 0;
// the direction to face/aim/shoot is from the middle of the screen to the mouse cursor
inputRotation = tempVector - tempVector2;
}
void ProcessMovement () {
rigidbody.AddForce (inputMovement.normalized * moveSpeed * Time.deltaTime);
transform.rotation = Quaternion.LookRotation(inputRotation);
transform.eulerAngles = new Vector3(0,transform.eulerAngles.y + 180,0);
transform.position = new Vector3(transform.position.x,0,transform.position.z);
}
Am I missing something about the world space in relation to the local space of the Player game object?