From a9342aee2f58882dd32fa2e4a2a9cecb1d179131 Mon Sep 17 00:00:00 2001 From: brausjonas <47791011+jonasbraus@users.noreply.github.com> Date: Mon, 16 May 2022 03:18:08 +0200 Subject: [PATCH] Fix Player Destroying block when resuming game from pause menu, Player "flying over ground" after falling down a block --- Minecraft Client/Assets/Scenes/Game.unity | 4 +-- .../Assets/Scripts/Game/Player.cs | 36 ++++++++++++------- Minecraft Client/Assets/Scripts/Game/World.cs | 6 ++-- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/Minecraft Client/Assets/Scenes/Game.unity b/Minecraft Client/Assets/Scenes/Game.unity index ce92344..b7c3fcf 100644 --- a/Minecraft Client/Assets/Scenes/Game.unity +++ b/Minecraft Client/Assets/Scenes/Game.unity @@ -1808,7 +1808,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1789911866} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 150, y: 50.039, z: 150} + m_LocalPosition: {x: 150, y: 49.961, z: 150} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: - {fileID: 1934992309} @@ -1951,7 +1951,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1934992308} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 1.7, z: 0.036} + m_LocalPosition: {x: 0, y: 1.557, z: 0.036} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 1789911868} diff --git a/Minecraft Client/Assets/Scripts/Game/Player.cs b/Minecraft Client/Assets/Scripts/Game/Player.cs index ef44475..5feeabb 100644 --- a/Minecraft Client/Assets/Scripts/Game/Player.cs +++ b/Minecraft Client/Assets/Scripts/Game/Player.cs @@ -67,6 +67,12 @@ public class Player : MonoBehaviour CalculateVelocity(); transform.Translate(velocity, Space.World); + //fix error in calculated position y + if (isGrounded) + { + transform.position = new Vector3(transform.position.x, (int)transform.position.y, transform.position.z); + } + //check if position or rotation had changed and send an update Vector3 position = transform.position; Quaternion rotation = transform.rotation; @@ -130,12 +136,15 @@ public class Player : MonoBehaviour transform.Rotate(0, mouseX * Time.timeScale, 0); //send destroy block request to server - if (Input.GetMouseButtonDown(0) || (Input.GetAxis("Right Trigger") > 0 && lastTriggerR == 0)) + if(Time.timeScale > 0) { - if (Physics.Raycast(camera.transform.position, camera.transform.forward, out RaycastHit hit, 8)) + if (Input.GetMouseButtonDown(0) || (Input.GetAxis("Right Trigger") > 0 && lastTriggerR == 0)) { - hit.point += camera.transform.forward / 10; - client.EditBlock(hit.point, 0); + if (Physics.Raycast(camera.transform.position, camera.transform.forward, out RaycastHit hit, 8)) + { + hit.point += camera.transform.forward / 10; + client.EditBlock(hit.point, 0); + } } } @@ -169,12 +178,15 @@ public class Player : MonoBehaviour } //send build block request to server - if (Input.GetMouseButtonDown(1) || (Input.GetAxis("Left Trigger") > 0 && lastTriggerL == 0)) + if(Time.timeScale > 0) { - if (Physics.Raycast(camera.transform.position, camera.transform.forward, out RaycastHit hit, 8)) + if (Input.GetMouseButtonDown(1) || (Input.GetAxis("Left Trigger") > 0 && lastTriggerL == 0)) { - hit.point -= camera.transform.forward / 10; - client.EditBlock(hit.point, currentBlock); + if (Physics.Raycast(camera.transform.position, camera.transform.forward, out RaycastHit hit, 8)) + { + hit.point -= camera.transform.forward / 10; + client.EditBlock(hit.point, currentBlock); + } } } @@ -239,10 +251,10 @@ public class Player : MonoBehaviour private float CheckDownSpeed(float downSpeed) { if ( - world.CheckBlock(new Vector3(transform.position.x - playerWidth, transform.position.y + downSpeed, transform.position.z - playerWidth)) || - world.CheckBlock(new Vector3(transform.position.x + playerWidth, transform.position.y + downSpeed, transform.position.z - playerWidth)) || - world.CheckBlock(new Vector3(transform.position.x + playerWidth, transform.position.y + downSpeed, transform.position.z + playerWidth)) || - world.CheckBlock(new Vector3(transform.position.x - playerWidth, transform.position.y + downSpeed, transform.position.z + playerWidth)) + world.CheckBlock(new Vector3(transform.position.x - playerWidth, (int)(transform.position.y + downSpeed), transform.position.z - playerWidth)) || + world.CheckBlock(new Vector3(transform.position.x + playerWidth, (int)(transform.position.y + downSpeed), transform.position.z - playerWidth)) || + world.CheckBlock(new Vector3(transform.position.x + playerWidth, (int)(transform.position.y + downSpeed), transform.position.z + playerWidth)) || + world.CheckBlock(new Vector3(transform.position.x - playerWidth, (int)(transform.position.y + downSpeed), transform.position.z + playerWidth)) ) { isGrounded = true; diff --git a/Minecraft Client/Assets/Scripts/Game/World.cs b/Minecraft Client/Assets/Scripts/Game/World.cs index af26c6a..5d90ec9 100644 --- a/Minecraft Client/Assets/Scripts/Game/World.cs +++ b/Minecraft Client/Assets/Scripts/Game/World.cs @@ -66,9 +66,9 @@ public class World : MonoBehaviour { byte id = playersToCreate.Dequeue(); GameObject player = Instantiate(playerPrefab); - player.transform.position = new Vector3((byte)player.transform.position.x, + player.transform.position = new Vector3((int)player.transform.position.x, GetHeight((byte)player.transform.position.x, (byte)player.transform.position.z) + 2, - (byte)player.transform.position.z); + (int)player.transform.position.z); player.transform.SetParent(gameObject.transform); otherPlayers.Add(id, player); } @@ -174,7 +174,7 @@ public class World : MonoBehaviour (int)(positionInWorld.z / Data.chunkWidth)); int xInChunk = (int)(positionInWorld.x - chunk.x * Data.chunkWidth); - int yInChunk = (int)positionInWorld.y; + int yInChunk = (int)(positionInWorld.y); int zInChunk = (int)(positionInWorld.z - chunk.z * Data.chunkWidth); if (chunks[chunk.x, chunk.z] != null)