Question by
wetcoco · Jul 20, 2021 at 11:39 AM ·
collisionmovementplayermovement scriptcollision issues
Unexisting blocks colliding and existing block not colliding
Hey, so I'm creating a bad copy of minecraft that I won't release and I'm having problems with collisions, for some reasons I sometimes with unexisting blocks and I don't collide with existing blocks, anyone that can help me with it?
Movement code (and collision code too)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public bool isGrounded;
public bool isSprinting;
public Transform camera;
public World world;
public float walkSpeed = 3f;
public float sprintSpeed = 6f;
public float jumpForce = 5f;
public float gravity = -9.8f;
public float playerWidth = 0.15f;
public float playerHeight = 1.8f;
private float horizontal;
private float vertical;
private float mouseHorizontal;
private float mouseVertical;
private float verticalMomentum;
private bool jumpRequest;
private Vector3 velocity;
private void FixedUpdate()
{
CalculateVelocity();
if (jumpRequest)
Jump();
transform.Rotate(Vector3.up * mouseHorizontal);
camera.Rotate(Vector3.right * -mouseVertical);
transform.Translate(velocity, Space.World);
}
private void Update()
{
GetPlayerInputs();
}
void Jump()
{
verticalMomentum = jumpForce;
isGrounded = false;
jumpRequest = false;
}
private void CalculateVelocity()
{
if (verticalMomentum > gravity)
verticalMomentum += Time.fixedDeltaTime * gravity;
if (isSprinting)
velocity = ((transform.forward * vertical) + (transform.right * horizontal)) * Time.fixedDeltaTime * sprintSpeed;
else
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);
}
private void GetPlayerInputs()
{
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
mouseHorizontal = Input.GetAxis("MouseX");
mouseVertical = Input.GetAxis("MouseY");
if (Input.GetButtonDown("Sprint"))
isSprinting = true;
if (Input.GetButtonUp("Sprint"))
isSprinting = false;
if (isGrounded && Input.GetButtonDown("Jump"))
jumpRequest = true;
}
private float checkDownSpeed(float downSpeed)
{
if
(
(world.CheckForVoxel(transform.position.x - playerWidth, transform.position.y + downSpeed, transform.position.z - playerWidth) && (!left && !back)) ||
(world.CheckForVoxel(transform.position.x + playerWidth, transform.position.y + downSpeed, transform.position.z - playerWidth) && (!right && !back)) ||
(world.CheckForVoxel(transform.position.x + playerWidth, transform.position.y + downSpeed, transform.position.z + playerWidth) && (!right && !front)) ||
(world.CheckForVoxel(transform.position.x - playerWidth, transform.position.y + downSpeed, transform.position.z + playerWidth) && (!left && !front))
)
{
isGrounded = true;
return 0;
}
else
{
isGrounded = false;
return downSpeed;
}
}
private float checkUpSpeed(float upSpeed)
{
if
(
(world.CheckForVoxel(transform.position.x - playerWidth, transform.position.y + playerHeight + upSpeed, transform.position.z - playerWidth) && (!left && !back)) ||
(world.CheckForVoxel(transform.position.x + playerWidth, transform.position.y + playerHeight + upSpeed, transform.position.z - playerWidth) && (!right && !back)) ||
(world.CheckForVoxel(transform.position.x + playerWidth, transform.position.y + playerHeight + upSpeed, transform.position.z + playerWidth) && (!right && !front)) ||
(world.CheckForVoxel(transform.position.x - playerWidth, transform.position.y + playerHeight + upSpeed, transform.position.z + playerWidth) && (!left && !front))
)
{
verticalMomentum = 0;
return 0;
}
else
{
return upSpeed;
}
}
public bool front
{
get
{
if
(
world.CheckForVoxel(transform.position.x, transform.position.y, transform.position.z + playerWidth) ||
world.CheckForVoxel(transform.position.x, transform.position.y + 1f, transform.position.z + playerWidth)
)
return true;
else
return false;
}
}
public bool back
{
get
{
if
(
world.CheckForVoxel(transform.position.x, transform.position.y, transform.position.z - playerWidth) ||
world.CheckForVoxel(transform.position.x, transform.position.y + 1f, transform.position.z - playerWidth)
)
return true;
else
return false;
}
}
public bool left
{
get
{
if
(
world.CheckForVoxel(transform.position.x - playerWidth, transform.position.y, transform.position.z) ||
world.CheckForVoxel(transform.position.x - playerWidth, transform.position.y + 1f, transform.position.z)
)
return true;
else
return false;
}
}
public bool right
{
get
{
if
(
world.CheckForVoxel(transform.position.x + playerWidth, transform.position.y, transform.position.z) ||
world.CheckForVoxel(transform.position.x + playerWidth, transform.position.y + 1f, transform.position.z)
)
return true;
else
return false;
}
}
}
Comment