Player bug fix
This commit is contained in:
@@ -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<Rigidbody>();
|
||||
client = world.GetClient();
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
camera = GetComponentInChildren<Camera>();
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user