Add player collision
This commit is contained in:
parent
3583e2ba3b
commit
6f0af4334a
2 changed files with 140 additions and 1 deletions
|
@ -15,7 +15,41 @@ public class Player : MonoBehaviour
|
|||
Vector2 inputVector = gameInput.GetMovementVectorNormalized();
|
||||
|
||||
Vector3 moveDir = new Vector3(inputVector.x, 0f, inputVector.y);
|
||||
transform.position += moveDir * moveSpeed * Time.deltaTime;
|
||||
|
||||
float moveDistance = moveSpeed * Time.deltaTime;
|
||||
float playerRadius = .7f;
|
||||
float playerHeight = 2f;
|
||||
bool canMove = !Physics.CapsuleCast(transform.position, transform.position + Vector3.up * playerHeight, playerRadius, moveDir, moveDistance);
|
||||
|
||||
if (!canMove)
|
||||
{
|
||||
// Cannot move towards moveDir
|
||||
|
||||
// Attempt only X move
|
||||
Vector3 moveDirX = new Vector3(moveDir.x, 0, 0).normalized;
|
||||
canMove = !Physics.CapsuleCast(transform.position, transform.position + Vector3.up * playerHeight, playerRadius, moveDirX, moveDistance);
|
||||
|
||||
if (canMove)
|
||||
{
|
||||
moveDir = moveDirX;
|
||||
} else
|
||||
{
|
||||
// Cannot move on X so attempt to move on Z
|
||||
Vector3 moveDirZ = new Vector3(0, 0, moveDir.z).normalized;
|
||||
canMove = !Physics.CapsuleCast(transform.position, transform.position + Vector3.up * playerHeight, playerRadius, moveDirZ, moveDistance);
|
||||
if (canMove)
|
||||
{
|
||||
moveDir = moveDirZ;
|
||||
} else
|
||||
{
|
||||
// Cannot move at all
|
||||
}
|
||||
}
|
||||
}
|
||||
if (canMove)
|
||||
{
|
||||
transform.position += moveDir * moveDistance;
|
||||
}
|
||||
|
||||
isWalking = moveDir != Vector3.zero;
|
||||
|
||||
|
|
Reference in a new issue