Fix Player

Destroying block when resuming game from pause menu,
Player "flying over ground" after falling down a block
This commit is contained in:
brausjonas
2022-05-16 03:18:08 +02:00
parent fd3ceac8c9
commit a9342aee2f
3 changed files with 29 additions and 17 deletions
+2 -2
View File
@@ -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}
+16 -4
View File
@@ -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,6 +136,8 @@ public class Player : MonoBehaviour
transform.Rotate(0, mouseX * Time.timeScale, 0);
//send destroy block request to server
if(Time.timeScale > 0)
{
if (Input.GetMouseButtonDown(0) || (Input.GetAxis("Right Trigger") > 0 && lastTriggerR == 0))
{
if (Physics.Raycast(camera.transform.position, camera.transform.forward, out RaycastHit hit, 8))
@@ -138,6 +146,7 @@ public class Player : MonoBehaviour
client.EditBlock(hit.point, 0);
}
}
}
lastTriggerR = Input.GetAxis("Right Trigger");
@@ -169,6 +178,8 @@ public class Player : MonoBehaviour
}
//send build block request to server
if(Time.timeScale > 0)
{
if (Input.GetMouseButtonDown(1) || (Input.GetAxis("Left Trigger") > 0 && lastTriggerL == 0))
{
if (Physics.Raycast(camera.transform.position, camera.transform.forward, out RaycastHit hit, 8))
@@ -177,6 +188,7 @@ public class Player : MonoBehaviour
client.EditBlock(hit.point, currentBlock);
}
}
}
lastTriggerL = Input.GetAxis("Left Trigger");
@@ -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;
@@ -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)