diff --git a/Minecraft Client/.idea/.idea.Minecraft Client/.idea/misc.xml b/Minecraft Client/.idea/.idea.Minecraft Client/.idea/misc.xml
new file mode 100644
index 0000000..283b9b4
--- /dev/null
+++ b/Minecraft Client/.idea/.idea.Minecraft Client/.idea/misc.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Minecraft Client/Assets/Scenes/Game.unity b/Minecraft Client/Assets/Scenes/Game.unity
index 63377a2..a46f0d8 100644
--- a/Minecraft Client/Assets/Scenes/Game.unity
+++ b/Minecraft Client/Assets/Scenes/Game.unity
@@ -599,8 +599,6 @@ GameObject:
serializedVersion: 6
m_Component:
- component: {fileID: 1789911868}
- - component: {fileID: 1789911869}
- - component: {fileID: 1789911867}
- component: {fileID: 1789911870}
m_Layer: 0
m_Name: Player
@@ -609,22 +607,6 @@ GameObject:
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
---- !u!54 &1789911867
-Rigidbody:
- m_ObjectHideFlags: 0
- m_CorrespondingSourceObject: {fileID: 0}
- m_PrefabInstance: {fileID: 0}
- m_PrefabAsset: {fileID: 0}
- m_GameObject: {fileID: 1789911866}
- serializedVersion: 2
- m_Mass: 1
- m_Drag: 0
- m_AngularDrag: 0.05
- m_UseGravity: 1
- m_IsKinematic: 0
- m_Interpolate: 0
- m_Constraints: 112
- m_CollisionDetection: 0
--- !u!4 &1789911868
Transform:
m_ObjectHideFlags: 0
@@ -633,27 +615,13 @@ 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, z: 150}
+ m_LocalPosition: {x: 150, y: 50.039, z: 150}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 1934992309}
m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
---- !u!136 &1789911869
-CapsuleCollider:
- m_ObjectHideFlags: 0
- m_CorrespondingSourceObject: {fileID: 0}
- m_PrefabInstance: {fileID: 0}
- m_PrefabAsset: {fileID: 0}
- m_GameObject: {fileID: 1789911866}
- m_Material: {fileID: 0}
- m_IsTrigger: 0
- m_Enabled: 1
- m_Radius: 0.4
- m_Height: 1.9
- m_Direction: 1
- m_Center: {x: 0, y: 0, z: 0}
--- !u!114 &1789911870
MonoBehaviour:
m_ObjectHideFlags: 0
@@ -667,8 +635,8 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
mouseSpeed: 4
- moveSpeed: 5
- jumpForce: 5
+ walkSpeed: 5
+ jumpForce: 8
world: {fileID: 948138834}
--- !u!1 &1934992308
GameObject:
@@ -696,7 +664,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: 0.585, z: 0}
+ m_LocalPosition: {x: 0, y: 1.484, z: 0}
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 1a39405..6393dca 100644
--- a/Minecraft Client/Assets/Scripts/Game/Player.cs
+++ b/Minecraft Client/Assets/Scripts/Game/Player.cs
@@ -7,11 +7,23 @@ public class Player : MonoBehaviour
{
//game
[SerializeField] private float mouseSpeed;
- [SerializeField] private float moveSpeed;
+ [SerializeField] private float walkSpeed = 5;
[SerializeField] private float jumpForce;
[SerializeField] private World world;
+ private float playerWidth = 0.15f;
+ private float playerOffsetWidth = 0.1f;
+ private float horizontal;
+ private float vertical;
+ private float mouseX;
+ private float mouseY;
+ private Vector3 velocity = new Vector3(0, 0, 0);
+ private float rotX = 0;
+ private float verticalMomentum = 0;
+ private bool jumpRequest;
+ private bool isGrounded = true;
+
+ private float gravity = -20f;
private Camera camera = null;
- private Rigidbody rigidbody;
private Vector3 lastPosition = new Vector3();
//network
@@ -19,7 +31,6 @@ public class Player : MonoBehaviour
private void Start()
{
- rigidbody = GetComponent();
client = world.GetClient();
Cursor.lockState = CursorLockMode.Locked;
camera = GetComponentInChildren();
@@ -27,10 +38,22 @@ public class Player : MonoBehaviour
world.GetHeight((byte)transform.position.x, (byte)transform.position.z) + 2, (byte)transform.position.z);
}
+ private void Jump()
+ {
+ verticalMomentum = jumpForce;
+ isGrounded = false;
+ jumpRequest = false;
+ }
+
private void FixedUpdate()
{
- transform.Translate(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.fixedDeltaTime, 0,
- Input.GetAxisRaw("Vertical") * moveSpeed * Time.fixedDeltaTime);
+ if (jumpRequest)
+ {
+ Jump();
+ }
+
+ CalculateVelocity();
+ transform.Translate(velocity, Space.World);
Vector3 position = transform.position;
@@ -44,17 +67,23 @@ public class Player : MonoBehaviour
private void Update()
{
- transform.Rotate(0, Input.GetAxis("Mouse X") * mouseSpeed, 0);
- camera.transform.Rotate(-Input.GetAxis("Mouse Y") * mouseSpeed, 0, 0);
+ horizontal = Input.GetAxisRaw("Horizontal");
+ vertical = Input.GetAxisRaw("Vertical");
+ mouseX = Input.GetAxis("Mouse X");
+ mouseY = Input.GetAxis("Mouse Y");
- if (Input.GetKeyDown(KeyCode.Space))
+ if (isGrounded && Input.GetButton("Jump"))
{
- if (Physics.Raycast(transform.position, Vector3.down, 0.96f))
- {
- rigidbody.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
- }
+ jumpRequest = true;
}
+
+ rotX -= mouseY * 1.4f;
+ rotX = Mathf.Clamp(rotX, -90, 90);
+
+ camera.transform.localRotation = Quaternion.Euler(rotX, 0, 0);
+ transform.Rotate(0, mouseX, 0);
+
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(camera.transform.position, camera.transform.forward, out RaycastHit hit))
@@ -73,4 +102,135 @@ public class Player : MonoBehaviour
}
}
}
+
+ private void CalculateVelocity()
+ {
+ if (verticalMomentum > gravity)
+ {
+ verticalMomentum += Time.fixedDeltaTime * gravity;
+ }
+
+ velocity = ((transform.forward * vertical) + (transform.right * horizontal)) * Time.fixedDeltaTime * walkSpeed;
+ velocity += Vector3.up * verticalMomentum * Time.fixedDeltaTime;
+
+ if ((velocity.z > 0 && front) || (velocity.z < 0 && back))
+ {
+ velocity.z = 0;
+ }
+
+ if ((velocity.x > 0 && right) || (velocity.x < 0 && left))
+ {
+ velocity.x = 0;
+ }
+
+ if (velocity.y < 0)
+ {
+ velocity.y = CheckDownSpeed(velocity.y);
+ }
+ else if (velocity.y > 0)
+ {
+ velocity.y = CheckUpSpeed(velocity.y);
+ }
+ else
+ {
+ velocity.y = 0;
+ }
+ }
+
+ 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))
+ )
+ {
+ isGrounded = true;
+ return 0;
+ }
+
+ isGrounded = false;
+ return downSpeed;
+ }
+
+ private float CheckUpSpeed(float upSpeed)
+ {
+ if (
+ world.CheckBlock(new Vector3(transform.position.x - playerWidth, transform.position.y + upSpeed + 2f, transform.position.z - playerWidth)) ||
+ world.CheckBlock(new Vector3(transform.position.x + playerWidth, transform.position.y + upSpeed + 2f, transform.position.z - playerWidth)) ||
+ world.CheckBlock(new Vector3(transform.position.x + playerWidth, transform.position.y + upSpeed + 2f, transform.position.z + playerWidth)) ||
+ world.CheckBlock(new Vector3(transform.position.x - playerWidth, transform.position.y + upSpeed + 2f, transform.position.z + playerWidth))
+ )
+ {
+ return 0;
+ }
+
+ return upSpeed;
+ }
+
+ public bool front
+ {
+ get
+ {
+ if (
+ world.CheckBlock(new Vector3(transform.position.x, transform.position.y, transform.position.z + playerWidth + playerOffsetWidth)) ||
+ world.CheckBlock(new Vector3(transform.position.x, transform.position.y + 1f, transform.position.z + playerWidth + playerOffsetWidth))
+ )
+ {
+ return true;
+ }
+
+ return false;
+ }
+ }
+
+ public bool back
+ {
+ get
+ {
+ if (
+ world.CheckBlock(new Vector3(transform.position.x, transform.position.y, transform.position.z - playerWidth - playerOffsetWidth)) ||
+ world.CheckBlock(new Vector3(transform.position.x, transform.position.y + 1f, transform.position.z - playerWidth - playerOffsetWidth))
+ )
+ {
+ return true;
+ }
+
+ return false;
+ }
+ }
+
+ public bool left
+ {
+ get
+ {
+ if (
+ world.CheckBlock(new Vector3(transform.position.x - playerWidth - playerOffsetWidth, transform.position.y, transform.position.z )) ||
+ world.CheckBlock(new Vector3(transform.position.x - playerWidth - playerOffsetWidth, transform.position.y + 1f, transform.position.z))
+ )
+ {
+ return true;
+ }
+
+ return false;
+ }
+ }
+
+ public bool right
+ {
+ get
+ {
+ if (
+ world.CheckBlock(new Vector3(transform.position.x + playerWidth + playerOffsetWidth, transform.position.y, transform.position.z)) ||
+ world.CheckBlock(new Vector3(transform.position.x + playerWidth + playerOffsetWidth, transform.position.y + 1f, transform.position.z))
+ )
+ {
+ return true;
+ }
+
+ return false;
+ }
+ }
}
+