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
+141 -87
View File
@@ -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
/// <summary>
/// If player presses space, jump
/// </summary>
private void TryJump()
{
if (characterController.isGrounded && Input.GetKeyDown(KeyCode.Space))
{
verticalVelocity = Mathf.Sqrt(jumpHeight * -2f * -gravity);
}
}
/// <summary>
/// Checks if shift is being held, isSprinting is true if it is
/// </summary>
@@ -111,7 +128,7 @@ private void CheckWeaponChange()
{
SwitchTools(false);
}
}
}
}
/// <summary>
@@ -141,47 +158,28 @@ void ApplyWalk()
}
/// <summary>
/// Apply gravity and speed to moveDir
/// Apply speed to moveDir
/// </summary>
void ApplyGravity()
void ApplySpeed()
{
if (isSprinting)
moveDir *= (walkSpeed * sprintMultiplier);
else
moveDir *= walkSpeed;
moveDir.y += -gravity;
}
/// <summary>
/// Move character with moveDir
/// Apply gravity to moveDir
/// </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>
/// 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);
verticalVelocity += -gravity;
}
/// <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>
/// Sets the character's "hasControl" bool. All input is ignored when false
/// Move character with moveDir
/// </summary>
public void SetCharacterControl(bool hasControl)
void DoMovement()
{
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;
moveDir.y = verticalVelocity;
characterController.Move(moveDir * Time.deltaTime);
}
/// <summary>
@@ -358,7 +308,7 @@ public Vector3 GetVelocity()
/// try the previous tool if the tool at toolIndex is not unlocked.
/// </param>
/// </summary>
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
/// <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);
}
}
}