diff --git a/Assets/Prefabs/Player.prefab b/Assets/Prefabs/Player.prefab index c480e1b..dd57dc4 100644 --- a/Assets/Prefabs/Player.prefab +++ b/Assets/Prefabs/Player.prefab @@ -76,9 +76,9 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 7e392b35ca7959444b61a3e29a242f2b, type: 3} m_Name: m_EditorClassIdentifier: + jumpHeight: 3.5 + jumpDuration: 0.4 moveSpeed: 6 - moveController: {fileID: 0} - velocity: {x: 0, y: 0, z: 0} --- !u!1 &6383496741659177281 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 5084589..8776b45 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -436,18 +436,7 @@ PrefabInstance: propertyPath: m_LocalEulerAnglesHint.z value: 0 objectReference: {fileID: 0} - - target: {fileID: 6891912346901077427, guid: cd445451907d7274daea79c67e13a202, - type: 3} - propertyPath: collisionMask.m_Bits - value: 2048 - objectReference: {fileID: 0} - - target: {fileID: 6383496741659177281, guid: cd445451907d7274daea79c67e13a202, - type: 3} - propertyPath: m_IsActive - value: 1 - objectReference: {fileID: 0} - m_RemovedComponents: - - {fileID: 528243339991412957, guid: cd445451907d7274daea79c67e13a202, type: 3} + m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: cd445451907d7274daea79c67e13a202, type: 3} --- !u!1001 &8206000050524585146 PrefabInstance: diff --git a/Assets/Scripts/MoveController.cs b/Assets/Scripts/MoveController.cs index b0e6d3e..1a9f596 100644 --- a/Assets/Scripts/MoveController.cs +++ b/Assets/Scripts/MoveController.cs @@ -130,6 +130,8 @@ public class MoveController : MonoBehaviour verticalRaySpacing = bounds.size.x / (verticalRayCount - 1); } + public bool Grounded() { return collisions.below; } + struct RaycastOrigins { public Vector3 topBackLeft; public Vector3 topBackRight; diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index 2176fbd..cb08cd6 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -4,15 +4,23 @@ using UnityEngine; [RequireComponent(typeof(MoveController))] public class PlayerController : MonoBehaviour { - + public float jumpHeight = 4f; + public float jumpDuration = 0.4f; public float moveSpeed = 6f; - public MoveController moveController; + public float accelerationTimeAirborne = 0.2f; + public float accelerationTimeGrounded = 0.05f; - private float gravity = -20f; - public Vector3 velocity; + private Vector3 velocity; + private float jumpVelocity; + private MoveController moveController; + private float gravity; + private float moveXSmoothing; void Start() { moveController = GetComponent(); + + gravity = -(2*jumpHeight)/Mathf.Pow(jumpDuration, 2f); + jumpVelocity = Mathf.Abs(gravity) * jumpDuration; } void Update() { @@ -24,7 +32,12 @@ public class PlayerController : MonoBehaviour { } Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); - velocity.x = input.x * moveSpeed; + if (Input.GetKeyDown(KeyCode.Space) && moveController.collisions.below) { + velocity.y = jumpVelocity; + } + + float targetx = input.x * moveSpeed; + velocity.x = Mathf.SmoothDamp(velocity.x, targetx, ref moveXSmoothing, moveController.Grounded() ? accelerationTimeGrounded : accelerationTimeAirborne); velocity.y += gravity * Time.deltaTime; moveController.Move(velocity * Time.deltaTime); }