diff --git a/Assets/Script/PlayerMovement.cs b/Assets/Script/PlayerMovement.cs index 949fb69..ed2b0a1 100644 --- a/Assets/Script/PlayerMovement.cs +++ b/Assets/Script/PlayerMovement.cs @@ -1,8 +1,19 @@ using UnityEngine; +[RequireComponent(typeof(Rigidbody))] public class PlayerMovement : MonoBehaviour { + [SerializeField] private int speed = 10; + [SerializeField] private int airSpeed = 5; + [SerializeField] private int jumpForce = 400; + + private float jumpDelay = 0; + private bool isGrounded = true; + private bool airControl = false; + private float jumpDirection; + private Rigidbody rb; + private void Start() { @@ -11,6 +22,50 @@ public class PlayerMovement : MonoBehaviour private void Update () { - rb.AddForce(new Vector3(Input.GetAxis("Horizontal") , 0, 0)); + if (!isGrounded) + { + if (jumpDirection > 0) + { + if (Input.GetAxis("Horizontal") > 0) + airControl = false; + else + airControl = true; + } + else + { + if (Input.GetAxis("Horizontal") > 0) + airControl = true; + else + airControl = false; + } + } + + rb.MovePosition(rb.position + new Vector3(Input.GetAxis("Horizontal"), 0, 0) * (airControl ? airSpeed : speed) * Time.deltaTime); + + if(jumpDelay == 0) + { + RaycastHit hit; + if (Physics.Raycast(rb.position, Vector3.down, out hit, 1)) + { + if(hit.collider.tag != "Player" || hit.collider.tag != "Projectile") + isGrounded = true; + } + } + else + { + jumpDelay--; + } + + + if (Input.GetAxis("Jump") > 0 && isGrounded) + { + print(Input.GetAxis("Jump")); + jumpDirection = Input.GetAxis("Horizontal"); + isGrounded = false; + jumpDelay = Mathf.Round(750 * Time.deltaTime); + rb.AddForce(new Vector3(0, jumpForce * Input.GetAxis("Jump"), 0), ForceMode.Acceleration); + } + + airControl = false; } } diff --git a/Assets/Test.unity b/Assets/Test.unity index 43d47e3..d4eebf3 100644 --- a/Assets/Test.unity +++ b/Assets/Test.unity @@ -328,11 +328,12 @@ GameObject: serializedVersion: 5 m_Component: - component: {fileID: 1180913641} + - component: {fileID: 1180913642} - component: {fileID: 1180913640} - component: {fileID: 1180913639} - component: {fileID: 1180913638} - component: {fileID: 1180913637} - m_Layer: 0 + m_Layer: 8 m_Name: Player m_TagString: Player m_Icon: {fileID: 0} @@ -364,7 +365,7 @@ Rigidbody: m_UseGravity: 1 m_IsKinematic: 0 m_Interpolate: 0 - m_Constraints: 0 + m_Constraints: 112 m_CollisionDetection: 0 --- !u!23 &1180913639 MeshRenderer: @@ -420,6 +421,22 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &1180913642 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1180913636} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 5d21adc90a3a0c5498d394337d4f81fb, type: 3} + m_Name: + m_EditorClassIdentifier: + speed: 10 + airSpeed: 7 + jumpForce: 400 + isGrounded: 1 + airControl: 0 --- !u!1 &1699708122 GameObject: m_ObjectHideFlags: 0 @@ -496,7 +513,6 @@ GameObject: - component: {fileID: 1936456414} - component: {fileID: 1936456413} - component: {fileID: 1936456412} - - component: {fileID: 1936456416} m_Layer: 0 m_Name: Cube m_TagString: Untagged @@ -570,14 +586,3 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!114 &1936456416 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_PrefabParentObject: {fileID: 0} - m_PrefabInternal: {fileID: 0} - m_GameObject: {fileID: 1936456411} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 5d21adc90a3a0c5498d394337d4f81fb, type: 3} - m_Name: - m_EditorClassIdentifier: diff --git a/ProjectSettings/InputManager.asset b/ProjectSettings/InputManager.asset index 17c8f53..fde8380 100644 --- a/ProjectSettings/InputManager.asset +++ b/ProjectSettings/InputManager.asset @@ -93,10 +93,10 @@ InputManager: positiveButton: space altNegativeButton: altPositiveButton: - gravity: 1000 + gravity: 3 dead: 0.001 - sensitivity: 1000 - snap: 0 + sensitivity: 100 + snap: 1 invert: 0 type: 0 axis: 0 diff --git a/ProjectSettings/TagManager.asset b/ProjectSettings/TagManager.asset index 1c92a78..0c7c8f5 100644 --- a/ProjectSettings/TagManager.asset +++ b/ProjectSettings/TagManager.asset @@ -13,7 +13,7 @@ TagManager: - UI - - - - + - Ground - - -