From 394422c5006093059fe3ff4817acd0f7f1f7db34 Mon Sep 17 00:00:00 2001 From: Trey Shaw Date: Tue, 30 Jun 2026 21:40:11 -0500 Subject: [PATCH] add jump and start bounce ability --- Assets/BouncePad.cs | 6 + Assets/BouncePad.cs.meta | 2 + Assets/Doug.prefab | 3 +- Assets/Scenes/World_Greaybox.unity | 133 +++++++++- Assets/Scripts/Management/PlayerController.cs | 228 +++++++++++------- 5 files changed, 283 insertions(+), 89 deletions(-) create mode 100644 Assets/BouncePad.cs create mode 100644 Assets/BouncePad.cs.meta diff --git a/Assets/BouncePad.cs b/Assets/BouncePad.cs new file mode 100644 index 0000000..1832bf1 --- /dev/null +++ b/Assets/BouncePad.cs @@ -0,0 +1,6 @@ +using UnityEngine; + +public class BouncePad : MonoBehaviour +{ + public float bounciness = 1; +} diff --git a/Assets/BouncePad.cs.meta b/Assets/BouncePad.cs.meta new file mode 100644 index 0000000..9f2b1f1 --- /dev/null +++ b/Assets/BouncePad.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 4fa1a488b56d8274fbeadd3c4fa613c5 \ No newline at end of file diff --git a/Assets/Doug.prefab b/Assets/Doug.prefab index 63397d3..9dfa5b1 100644 --- a/Assets/Doug.prefab +++ b/Assets/Doug.prefab @@ -3224,8 +3224,9 @@ MonoBehaviour: - {fileID: 6038027350876930962} walkSpeed: 2 sprintMultiplier: 1.5 - gravity: 5 + gravity: 0.03 rotationSpeed: 40 + jumpHeight: 250 cameraController: {fileID: 5478143440182735446} --- !u!114 &5478143440182735446 MonoBehaviour: diff --git a/Assets/Scenes/World_Greaybox.unity b/Assets/Scenes/World_Greaybox.unity index 2ad7060..22f3456 100644 --- a/Assets/Scenes/World_Greaybox.unity +++ b/Assets/Scenes/World_Greaybox.unity @@ -1447,6 +1447,132 @@ Transform: m_Children: [] m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &613495044 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 613495049} + - component: {fileID: 613495048} + - component: {fileID: 613495047} + - component: {fileID: 613495046} + - component: {fileID: 613495045} + m_Layer: 0 + m_Name: TestBounce + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &613495045 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 613495044} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 4fa1a488b56d8274fbeadd3c4fa613c5, type: 3} + m_Name: + m_EditorClassIdentifier: Assembly-CSharp::BouncePad + bounciness: 1 +--- !u!65 &613495046 +BoxCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 613495044} + m_Material: {fileID: 0} + m_IncludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_ExcludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_LayerOverridePriority: 0 + m_IsTrigger: 0 + m_ProvidesContacts: 0 + m_Enabled: 1 + serializedVersion: 3 + m_Size: {x: 1, y: 1, z: 1} + m_Center: {x: 0, y: 0, z: 0} +--- !u!23 &613495047 +MeshRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 613495044} + m_Enabled: 1 + m_CastShadows: 1 + m_ReceiveShadows: 1 + m_DynamicOccludee: 1 + m_StaticShadowCaster: 0 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RayTracingMode: 2 + m_RayTraceProcedural: 0 + m_RayTracingAccelStructBuildFlagsOverride: 0 + m_RayTracingAccelStructBuildFlags: 1 + m_SmallMeshCulling: 1 + m_ForceMeshLod: -1 + m_MeshLodSelectionBias: 0 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 2100000, guid: 31321ba15b8f8eb4c954353edc038b1d, type: 2} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_ReceiveGI: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 1 + m_SelectedEditorRenderState: 3 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_GlobalIlluminationMeshLod: 0 + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 + m_MaskInteraction: 0 + m_AdditionalVertexStreams: {fileID: 0} +--- !u!33 &613495048 +MeshFilter: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 613495044} + m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0} +--- !u!4 &613495049 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 613495044} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: -3.43873, y: -0.01, z: 1.693} + m_LocalScale: {x: 1, y: 0.19284, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!64 &627590510 MeshCollider: m_ObjectHideFlags: 0 @@ -2075,7 +2201,11 @@ PrefabInstance: m_Modifications: - target: {fileID: 1480974628599415149, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3} propertyPath: gravity - value: 5 + value: 0.03 + objectReference: {fileID: 0} + - target: {fileID: 1480974628599415149, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3} + propertyPath: jumpHeight + value: 250 objectReference: {fileID: 0} - target: {fileID: 1480974628599415149, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3} propertyPath: rotationSpeed @@ -3088,3 +3218,4 @@ SceneRoots: - {fileID: 1404838790} - {fileID: 1474562312} - {fileID: 578608798} + - {fileID: 613495049} diff --git a/Assets/Scripts/Management/PlayerController.cs b/Assets/Scripts/Management/PlayerController.cs index f9d051f..a47379a 100644 --- a/Assets/Scripts/Management/PlayerController.cs +++ b/Assets/Scripts/Management/PlayerController.cs @@ -1,4 +1,5 @@ using System.Collections; +using TMPro; using Unity.VisualScripting.Antlr3.Runtime; using UnityEngine; using static UnityEngine.GraphicsBuffer; @@ -10,15 +11,17 @@ public class PlayerController : MonoBehaviour public Tool[] tools; float toolUseTimestamp = 0; - public float walkSpeed; + public float walkSpeed = 2; public float sprintMultiplier = 1.5f; - public float gravity = 10; - public float rotationSpeed = 15; + public float gravity = 0.05f; + public float rotationSpeed = 40; + public float jumpHeight = 200f; Interactable nearestInteractable; public CameraController cameraController; private CharacterController characterController; private Vector3 moveDir; + private float verticalVelocity; private bool hasControl = true; // set this to false if we want to stop reading player inputs private bool canUseTools = true; // set this to false if we want to stop reading player tool use private bool isSprinting = false; @@ -69,6 +72,7 @@ void Update() CheckWeaponChange(); ApplyWalk(); + TryJump(); } if (canUseTools) @@ -77,10 +81,23 @@ void Update() } CheckSprint(); + ApplySpeed(); ApplyGravity(); DoMovement(); } + #region Movement Calculation/Input Processing + /// + /// If player presses space, jump + /// + private void TryJump() + { + if (characterController.isGrounded && Input.GetKeyDown(KeyCode.Space)) + { + verticalVelocity = Mathf.Sqrt(jumpHeight * -2f * -gravity); + } + } + /// /// Checks if shift is being held, isSprinting is true if it is /// @@ -111,7 +128,7 @@ private void CheckWeaponChange() { SwitchTools(false); } - } + } } /// @@ -141,47 +158,28 @@ void ApplyWalk() } /// - /// Apply gravity and speed to moveDir + /// Apply speed to moveDir /// - void ApplyGravity() + void ApplySpeed() { if (isSprinting) moveDir *= (walkSpeed * sprintMultiplier); else moveDir *= walkSpeed; - - moveDir.y += -gravity; } /// - /// Move character with moveDir + /// Apply gravity to moveDir /// - void DoMovement() + void ApplyGravity() { - characterController.Move(moveDir * Time.deltaTime); - } + // Makes sure we're grounded (built-in character controller is wonky) + if (characterController.isGrounded && verticalVelocity < 0) + { + verticalVelocity = -2f; + } - /// - /// Pass movement into character controller, lets us expose function without exposing - /// whole char controller - /// - public void CharacterControllerMove(Vector3 movement) - { - characterController.Move(movement); - } - - /// - /// Auto-walk the character in the specified direction. Uses character's walk - /// speed and camera rotation (per-frame) - /// - public void WalkInDirection(Vector3 direction) - { - Vector3 forwardDir = direction; - forwardDir.y = 0; // don't move on the y axis - - forwardDir *= walkSpeed; - - characterController.Move(forwardDir * Time.deltaTime); + verticalVelocity += -gravity; } /// @@ -292,61 +290,13 @@ void TryInteract() } } - private void OnTriggerEnter(Collider other) - { - Interactable interactable = other.GetComponent(); - - // store nearest interactable if it exists - if (interactable != null) - { - nearestInteractable = interactable; - interactable.MoveInsideRange(); - } - } - - private void OnTriggerExit(Collider other) - { - // just null out interactables when we leave an interactable trigger (we cant and shouldn't overlap interactables) - Interactable interactable = other.GetComponent(); - - // store nearest interactable if it exists - if (interactable != null && nearestInteractable != null) - { - nearestInteractable.MoveOutsideRange(); - nearestInteractable = null; - } - } - /// - /// Sets the character's "hasControl" bool. All input is ignored when false + /// Move character with moveDir /// - public void SetCharacterControl(bool hasControl) + void DoMovement() { - this.hasControl = hasControl; - } - - /// - /// Sets the character's "canUseTools" bool. Tool use input is ignored when false - /// - public void SetCharacterCanUseTools(bool canUseTools) - { - this.canUseTools = canUseTools; - } - - /// - /// Gets the character's "hasControl" bool. All input is ignored when false - /// - public bool GetCharacterHasControl() - { - return hasControl; - } - - /// - /// Expose the character controller's velocity - /// - public Vector3 GetVelocity() - { - return characterController.velocity; + moveDir.y = verticalVelocity; + characterController.Move(moveDir * Time.deltaTime); } /// @@ -358,7 +308,7 @@ public Vector3 GetVelocity() /// try the previous tool if the tool at toolIndex is not unlocked. /// /// - private void SwitchTools(int toolIndex, bool defaultToUp=true) + private void SwitchTools(int toolIndex, bool defaultToUp = true) { if (toolIndex < 0 || toolIndex >= tools.Length) { @@ -370,7 +320,7 @@ private void SwitchTools(int toolIndex, bool defaultToUp=true) { tool.gameObject.SetActive(false); } - + if (GameManager.Instance.PlayerManager.IsToolUnlocked(tools[toolIndex].ToolType)) { // load new tool @@ -422,5 +372,109 @@ private void SwitchTools(bool up) else SwitchTools(newIndex - 1, false); } } + #endregion + + #region External Controls + /// + /// Pass movement into character controller, lets us expose function without exposing + /// whole char controller + /// + public void CharacterControllerMove(Vector3 movement) + { + characterController.Move(movement); + } + + /// + /// Auto-walk the character in the specified direction. Uses character's walk + /// speed and camera rotation (per-frame) + /// + public void WalkInDirection(Vector3 direction) + { + Vector3 forwardDir = direction; + forwardDir.y = 0; // don't move on the y axis + + forwardDir *= walkSpeed; + + characterController.Move(forwardDir * Time.deltaTime); + } + + /// + /// Sets the character's "hasControl" bool. All input is ignored when false + /// + public void SetCharacterControl(bool hasControl) + { + this.hasControl = hasControl; + } + + /// + /// Sets the character's "canUseTools" bool. Tool use input is ignored when false + /// + public void SetCharacterCanUseTools(bool canUseTools) + { + this.canUseTools = canUseTools; + } + + /// + /// Gets the character's "hasControl" bool. All input is ignored when false + /// + public bool GetCharacterHasControl() + { + return hasControl; + } + + /// + /// Expose the character controller's velocity + /// + public Vector3 GetVelocity() + { + return characterController.velocity; + } + + /// + /// Bounce off of object (upward), reverses downward velocity and multiplies it by + /// the bounciness + /// How bouncy the surface is + /// + public void Bounce(float bounciness) + { + float bounceForce = -verticalVelocity * bounciness; + moveDir.y = bounceForce; + } + #endregion + + private void OnTriggerEnter(Collider other) + { + Interactable interactable = other.GetComponent(); + + // store nearest interactable if it exists + if (interactable != null) + { + nearestInteractable = interactable; + interactable.MoveInsideRange(); + } + } + + private void OnTriggerExit(Collider other) + { + // just null out interactables when we leave an interactable trigger (we cant and shouldn't overlap interactables) + Interactable interactable = other.GetComponent(); + + // store nearest interactable if it exists + if (interactable != null && nearestInteractable != null) + { + nearestInteractable.MoveOutsideRange(); + nearestInteractable = null; + } + } + + private void OnControllerColliderHit(ControllerColliderHit hit) + { + BouncePad bouncePad = hit.gameObject.GetComponent(); + if (bouncePad != null) + { + Bounce(bouncePad.bounciness); + Debug.Log("Bounce! " + verticalVelocity); + } + } }