Added jump and tweaked jump while walking interaction

This commit is contained in:
louishorlaville 2022-10-19 19:38:06 -04:00
parent 0e32c69f2a
commit f58dd0ae5b

View File

@ -17,21 +17,30 @@ public class CharacterMovement : MonoBehaviour
[Header("Character properties")] [Header("Character properties")]
public bool isGrounded; public bool isGrounded;
public bool isJumping;
private Vector3 rawInputMovement; private Vector3 rawInputMovement;
private void FixedUpdate() private void FixedUpdate()
{ {
Debug.Log(isGrounded);
if (canWalk || canJump) if (canWalk || canJump)
{ {
rb.AddForce(new Vector3(rawInputMovement.x * movementSpeed, rawInputMovement.y, rawInputMovement.z * movementSpeed), ForceMode.Impulse); if (isGrounded)
{
//The walk is actually handled here
rb.AddForce(rawInputMovement * movementSpeed, ForceMode.Impulse);
}
} }
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.down) * 1, Color.yellow);
RaycastHit hit; RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), out hit, 1.5f)) if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), out hit, 1f, LayerMask.GetMask("Floor")) ||
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.up), out hit, 1f, LayerMask.GetMask("Floor")) ||
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, 1f, LayerMask.GetMask("Floor")) ||
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.back), out hit, 1f, LayerMask.GetMask("Floor")) ||
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), out hit, 1f, LayerMask.GetMask("Floor")) ||
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), out hit, 1f, LayerMask.GetMask("Floor")))
{ {
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.down) * hit.distance, Color.yellow);
isGrounded = true; isGrounded = true;
} }
else else
@ -52,10 +61,10 @@ public class CharacterMovement : MonoBehaviour
public void Jump(InputAction.CallbackContext value) public void Jump(InputAction.CallbackContext value)
{ {
if (canJump && isGrounded) if(canJump && isGrounded)
{ {
Vector2 inputMovement = value.ReadValue<Vector2>(); rb.velocity = new Vector3(0, jumpPower, 0);
rawInputMovement = new Vector3(rawInputMovement.x, jumpPower, rawInputMovement.z);
} }
} }
} }