add jump and start bounce ability

This commit is contained in:
2026-06-30 21:40:11 -05:00
parent fc0bcf0201
commit 394422c500
5 changed files with 283 additions and 89 deletions
+6
View File
@@ -0,0 +1,6 @@
using UnityEngine;
public class BouncePad : MonoBehaviour
{
public float bounciness = 1;
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 4fa1a488b56d8274fbeadd3c4fa613c5
+2 -1
View File
@@ -3224,8 +3224,9 @@ MonoBehaviour:
- {fileID: 6038027350876930962} - {fileID: 6038027350876930962}
walkSpeed: 2 walkSpeed: 2
sprintMultiplier: 1.5 sprintMultiplier: 1.5
gravity: 5 gravity: 0.03
rotationSpeed: 40 rotationSpeed: 40
jumpHeight: 250
cameraController: {fileID: 5478143440182735446} cameraController: {fileID: 5478143440182735446}
--- !u!114 &5478143440182735446 --- !u!114 &5478143440182735446
MonoBehaviour: MonoBehaviour:
+132 -1
View File
@@ -1447,6 +1447,132 @@ Transform:
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 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 --- !u!64 &627590510
MeshCollider: MeshCollider:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@@ -2075,7 +2201,11 @@ PrefabInstance:
m_Modifications: m_Modifications:
- target: {fileID: 1480974628599415149, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3} - target: {fileID: 1480974628599415149, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3}
propertyPath: gravity propertyPath: gravity
value: 5 value: 0.03
objectReference: {fileID: 0}
- target: {fileID: 1480974628599415149, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3}
propertyPath: jumpHeight
value: 250
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 1480974628599415149, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3} - target: {fileID: 1480974628599415149, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3}
propertyPath: rotationSpeed propertyPath: rotationSpeed
@@ -3088,3 +3218,4 @@ SceneRoots:
- {fileID: 1404838790} - {fileID: 1404838790}
- {fileID: 1474562312} - {fileID: 1474562312}
- {fileID: 578608798} - {fileID: 578608798}
- {fileID: 613495049}
+141 -87
View File
@@ -1,4 +1,5 @@
using System.Collections; using System.Collections;
using TMPro;
using Unity.VisualScripting.Antlr3.Runtime; using Unity.VisualScripting.Antlr3.Runtime;
using UnityEngine; using UnityEngine;
using static UnityEngine.GraphicsBuffer; using static UnityEngine.GraphicsBuffer;
@@ -10,15 +11,17 @@ public class PlayerController : MonoBehaviour
public Tool[] tools; public Tool[] tools;
float toolUseTimestamp = 0; float toolUseTimestamp = 0;
public float walkSpeed; public float walkSpeed = 2;
public float sprintMultiplier = 1.5f; public float sprintMultiplier = 1.5f;
public float gravity = 10; public float gravity = 0.05f;
public float rotationSpeed = 15; public float rotationSpeed = 40;
public float jumpHeight = 200f;
Interactable nearestInteractable; Interactable nearestInteractable;
public CameraController cameraController; public CameraController cameraController;
private CharacterController characterController; private CharacterController characterController;
private Vector3 moveDir; private Vector3 moveDir;
private float verticalVelocity;
private bool hasControl = true; // set this to false if we want to stop reading player inputs 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 canUseTools = true; // set this to false if we want to stop reading player tool use
private bool isSprinting = false; private bool isSprinting = false;
@@ -69,6 +72,7 @@ void Update()
CheckWeaponChange(); CheckWeaponChange();
ApplyWalk(); ApplyWalk();
TryJump();
} }
if (canUseTools) if (canUseTools)
@@ -77,10 +81,23 @@ void Update()
} }
CheckSprint(); CheckSprint();
ApplySpeed();
ApplyGravity(); ApplyGravity();
DoMovement(); DoMovement();
} }
#region Movement Calculation/Input Processing
/// <summary>
/// If player presses space, jump
/// </summary>
private void TryJump()
{
if (characterController.isGrounded && Input.GetKeyDown(KeyCode.Space))
{
verticalVelocity = Mathf.Sqrt(jumpHeight * -2f * -gravity);
}
}
/// <summary> /// <summary>
/// Checks if shift is being held, isSprinting is true if it is /// Checks if shift is being held, isSprinting is true if it is
/// </summary> /// </summary>
@@ -111,7 +128,7 @@ private void CheckWeaponChange()
{ {
SwitchTools(false); SwitchTools(false);
} }
} }
} }
/// <summary> /// <summary>
@@ -141,47 +158,28 @@ void ApplyWalk()
} }
/// <summary> /// <summary>
/// Apply gravity and speed to moveDir /// Apply speed to moveDir
/// </summary> /// </summary>
void ApplyGravity() void ApplySpeed()
{ {
if (isSprinting) if (isSprinting)
moveDir *= (walkSpeed * sprintMultiplier); moveDir *= (walkSpeed * sprintMultiplier);
else else
moveDir *= walkSpeed; moveDir *= walkSpeed;
moveDir.y += -gravity;
} }
/// <summary> /// <summary>
/// Move character with moveDir /// Apply gravity to moveDir
/// </summary> /// </summary>
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;
}
/// <summary> verticalVelocity += -gravity;
/// Pass movement into character controller, lets us expose function without exposing
/// whole char controller
/// </summary>
public void CharacterControllerMove(Vector3 movement)
{
characterController.Move(movement);
}
/// <summary>
/// Auto-walk the character in the specified direction. Uses character's walk
/// speed and camera rotation (per-frame)
/// </summary>
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);
} }
/// <summary> /// <summary>
@@ -292,61 +290,13 @@ void TryInteract()
} }
} }
private void OnTriggerEnter(Collider other)
{
Interactable interactable = other.GetComponent<Interactable>();
// 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<Interactable>();
// store nearest interactable if it exists
if (interactable != null && nearestInteractable != null)
{
nearestInteractable.MoveOutsideRange();
nearestInteractable = null;
}
}
/// <summary> /// <summary>
/// Sets the character's "hasControl" bool. All input is ignored when false /// Move character with moveDir
/// </summary> /// </summary>
public void SetCharacterControl(bool hasControl) void DoMovement()
{ {
this.hasControl = hasControl; moveDir.y = verticalVelocity;
} characterController.Move(moveDir * Time.deltaTime);
/// <summary>
/// Sets the character's "canUseTools" bool. Tool use input is ignored when false
/// </summary>
public void SetCharacterCanUseTools(bool canUseTools)
{
this.canUseTools = canUseTools;
}
/// <summary>
/// Gets the character's "hasControl" bool. All input is ignored when false
/// </summary>
public bool GetCharacterHasControl()
{
return hasControl;
}
/// <summary>
/// Expose the character controller's velocity
/// </summary>
public Vector3 GetVelocity()
{
return characterController.velocity;
} }
/// <summary> /// <summary>
@@ -358,7 +308,7 @@ public Vector3 GetVelocity()
/// try the previous tool if the tool at toolIndex is not unlocked. /// try the previous tool if the tool at toolIndex is not unlocked.
/// </param> /// </param>
/// </summary> /// </summary>
private void SwitchTools(int toolIndex, bool defaultToUp=true) private void SwitchTools(int toolIndex, bool defaultToUp = true)
{ {
if (toolIndex < 0 || toolIndex >= tools.Length) if (toolIndex < 0 || toolIndex >= tools.Length)
{ {
@@ -370,7 +320,7 @@ private void SwitchTools(int toolIndex, bool defaultToUp=true)
{ {
tool.gameObject.SetActive(false); tool.gameObject.SetActive(false);
} }
if (GameManager.Instance.PlayerManager.IsToolUnlocked(tools[toolIndex].ToolType)) if (GameManager.Instance.PlayerManager.IsToolUnlocked(tools[toolIndex].ToolType))
{ {
// load new tool // load new tool
@@ -422,5 +372,109 @@ private void SwitchTools(bool up)
else SwitchTools(newIndex - 1, false); else SwitchTools(newIndex - 1, false);
} }
} }
#endregion
#region External Controls
/// <summary>
/// Pass movement into character controller, lets us expose function without exposing
/// whole char controller
/// </summary>
public void CharacterControllerMove(Vector3 movement)
{
characterController.Move(movement);
}
/// <summary>
/// Auto-walk the character in the specified direction. Uses character's walk
/// speed and camera rotation (per-frame)
/// </summary>
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);
}
/// <summary>
/// Sets the character's "hasControl" bool. All input is ignored when false
/// </summary>
public void SetCharacterControl(bool hasControl)
{
this.hasControl = hasControl;
}
/// <summary>
/// Sets the character's "canUseTools" bool. Tool use input is ignored when false
/// </summary>
public void SetCharacterCanUseTools(bool canUseTools)
{
this.canUseTools = canUseTools;
}
/// <summary>
/// Gets the character's "hasControl" bool. All input is ignored when false
/// </summary>
public bool GetCharacterHasControl()
{
return hasControl;
}
/// <summary>
/// Expose the character controller's velocity
/// </summary>
public Vector3 GetVelocity()
{
return characterController.velocity;
}
/// <summary>
/// Bounce off of object (upward), reverses downward velocity and multiplies it by
/// the bounciness
/// <param name="bounciness">How bouncy the surface is</param>
/// </summary>
public void Bounce(float bounciness)
{
float bounceForce = -verticalVelocity * bounciness;
moveDir.y = bounceForce;
}
#endregion
private void OnTriggerEnter(Collider other)
{
Interactable interactable = other.GetComponent<Interactable>();
// 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<Interactable>();
// 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<BouncePad>();
if (bouncePad != null)
{
Bounce(bouncePad.bounciness);
Debug.Log("Bounce! " + verticalVelocity);
}
}
} }