From 0e32c69f2a8ced53743659820c354abaa5c88d94 Mon Sep 17 00:00:00 2001 From: louishorlaville Date: Tue, 18 Oct 2022 23:41:28 -0400 Subject: [PATCH] added small jump --- Assets/Scenes/CharacterMovement_test.unity | 3 ++- Assets/Scripts/CharacterMovement.cs | 23 ++++++++++++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/Assets/Scenes/CharacterMovement_test.unity b/Assets/Scenes/CharacterMovement_test.unity index 7157988..0e4feba 100644 --- a/Assets/Scenes/CharacterMovement_test.unity +++ b/Assets/Scenes/CharacterMovement_test.unity @@ -415,7 +415,8 @@ MonoBehaviour: canWalk: 1 canJump: 1 movementSpeed: 1.5 - jumpPower: 5 + jumpPower: 10 + isGrounded: 0 --- !u!1 &684542796 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/CharacterMovement.cs b/Assets/Scripts/CharacterMovement.cs index 25b4546..83933c3 100644 --- a/Assets/Scripts/CharacterMovement.cs +++ b/Assets/Scripts/CharacterMovement.cs @@ -16,17 +16,29 @@ public class CharacterMovement : MonoBehaviour [SerializeField] private float jumpPower; [Header("Character properties")] + public bool isGrounded; - - private Vector2 rawInputMovement; + private Vector3 rawInputMovement; private void FixedUpdate() { + Debug.Log(isGrounded); 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){ @@ -40,11 +52,10 @@ public class CharacterMovement : MonoBehaviour public void Jump(InputAction.CallbackContext value) { - if (canJump) + if (canJump && isGrounded) { Vector2 inputMovement = value.ReadValue(); - rawInputMovement = new Vector3(rawInputMovement.x, jumpPower, rawInputMovement.y); - canJump = false; + rawInputMovement = new Vector3(rawInputMovement.x, jumpPower, rawInputMovement.z); } } }