added small jump

This commit is contained in:
louishorlaville 2022-10-18 23:41:28 -04:00
parent 6d0a7912e0
commit 0e32c69f2a
2 changed files with 19 additions and 7 deletions

View File

@ -415,7 +415,8 @@ MonoBehaviour:
canWalk: 1 canWalk: 1
canJump: 1 canJump: 1
movementSpeed: 1.5 movementSpeed: 1.5
jumpPower: 5 jumpPower: 10
isGrounded: 0
--- !u!1 &684542796 --- !u!1 &684542796
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@ -16,17 +16,29 @@ public class CharacterMovement : MonoBehaviour
[SerializeField] private float jumpPower; [SerializeField] private float jumpPower;
[Header("Character properties")] [Header("Character properties")]
public bool isGrounded;
private Vector3 rawInputMovement;
private Vector2 rawInputMovement;
private void FixedUpdate() private void FixedUpdate()
{ {
Debug.Log(isGrounded);
if (canWalk || canJump) if (canWalk || canJump)
{ {
rb.AddForce(rawInputMovement * movementSpeed, ForceMode.Impulse); rb.AddForce(new Vector3(rawInputMovement.x * movementSpeed, rawInputMovement.y, rawInputMovement.z * movementSpeed), ForceMode.Impulse);
} }
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), out hit, 1.5f))
{
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.down) * hit.distance, Color.yellow);
isGrounded = true;
}
else
{
rawInputMovement = new Vector3(rawInputMovement.x, 0, rawInputMovement.z);
isGrounded = false;
}
} }
public void Walk(InputAction.CallbackContext value){ public void Walk(InputAction.CallbackContext value){
@ -40,11 +52,10 @@ public class CharacterMovement : MonoBehaviour
public void Jump(InputAction.CallbackContext value) public void Jump(InputAction.CallbackContext value)
{ {
if (canJump) if (canJump && isGrounded)
{ {
Vector2 inputMovement = value.ReadValue<Vector2>(); Vector2 inputMovement = value.ReadValue<Vector2>();
rawInputMovement = new Vector3(rawInputMovement.x, jumpPower, rawInputMovement.y); rawInputMovement = new Vector3(rawInputMovement.x, jumpPower, rawInputMovement.z);
canJump = false;
} }
} }
} }