The fucking 3rd time i had to upload this project to git

This commit is contained in:
Madhav Kapa
2023-10-06 20:18:29 -04:00
commit 5658acfe16
2689 changed files with 1259400 additions and 0 deletions

View File

@ -0,0 +1,47 @@
using UnityEngine;
namespace Player.Movement
{
public class MouseLook : MonoBehaviour
{
[SerializeField] public float mouseSensitivity = 100f;
public float xRotation;
private Transform playerBody;
// Start is called before the first frame update
private void Start()
{
// check playerprefs for sensitivity
// if not set, set to 100
// if set, set to playerprefs
if (!PlayerPrefs.HasKey("Sensitivity"))
{
PlayerPrefs.SetFloat("Sensitivity", 100f);
}
else
{
mouseSensitivity = PlayerPrefs.GetFloat("Sensitivity");
}
playerBody = transform.parent;
Cursor.lockState = CursorLockMode.Locked;
//mouseSensitivity = PlayerPrefs.GetFloat("Sensitivity");
}
// Update is called once per frame
private void Update()
{
var mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
var mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
playerBody.Rotate(Vector3.up * mouseX);
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 867f6d1560c5c4838bf513a400b50bec
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,67 @@
using UnityEngine;
using Player.Interactions;
public class PlayerMovement : MonoBehaviour
{
// Start is called before the first frame update
private void Start()
{
_controller = gameObject.GetComponent<CharacterController>();
_moveSpeed = walkSpeed;
_sprintBonus = sprintSpeed - walkSpeed;
grappleHook = gameObject.GetComponent<GrappleHook>();
}
// Update is called once per frame
private void Update()
{
// ground check
// isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
_isGrounded = _controller.isGrounded;
if (_isGrounded && playerVelocity.y < 0) playerVelocity.y = -2f;
// sprint mechanic
var sprintInput = Input.GetAxis("Sprint");
// wasd player movement
var xMove = Input.GetAxis("Horizontal");
var zMove = Input.GetAxis("Vertical");
var xzMove = (transform.right * xMove + transform.forward * zMove) * (_moveSpeed + sprintInput * _sprintBonus);
// jump
if (Input.GetButtonDown("Jump") && _isGrounded) playerVelocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
// gravity
playerVelocity.y += gravity * Time.deltaTime;
// force applied by grapple hook
//playerVelocity += grappleHook.PullForce(transform.position) * Time.deltaTime;
// final player move
_controller.Move((playerVelocity + xzMove) * Time.deltaTime);
//Debug.Log(controller.isGrounded);
}
#region Variables
[SerializeField] private float walkSpeed = 2f;
[SerializeField] private float sprintSpeed = 10f;
private float _moveSpeed;
private float _sprintBonus;
private CharacterController _controller;
public Vector3 playerVelocity;
[SerializeField] private float jumpHeight = 2f;
[SerializeField] private float gravity = -10f;
private bool _isGrounded;
private GrappleHook grappleHook;
#endregion
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b3ecedebba2564ed7ad0bca94c48c9a0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,149 @@
using UnityEngine;
public class PlayerMovement2 : MonoBehaviour
{
// Start is called before the first frame update
private void Start()
{
controller = gameObject.GetComponent<CharacterController>();
playerVelocity = Vector3.zero;
playerAcceleration = Vector3.zero;
moveSpeed = walkSpeed;
sprintBonus = sprintSpeed - walkSpeed;
}
private void Update()
{
// acceleration of an object by default is zero
playerAcceleration = Vector3.zero;
// determine if the player is grounded
isGrounded = controller.isGrounded;
playerAcceleration += new Vector3(0f, gravity, 0f);
// add acceleration due to normal force, assuming platforms are stationary and parallel to the ground
if (isGrounded) playerAcceleration += new Vector3(0f, -gravity, 0f);
// when the player jumps on the ground, apply an instant force to the player's velocity
if (isGrounded && Input.GetButtonDown("Jump")) playerAcceleration.y += jumpHeight;
// handle force applied by player movement
if (isGrounded)
if (playerVelocity.magnitude < maxWalkSpeed)
{
//Vector3 xzMove = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
var xzMove = Vector3.zero;
if (Input.GetKey(KeyCode.W)) xzMove.z += 1f;
if (Input.GetKey(KeyCode.S)) xzMove.z += -1f;
if (Input.GetKey(KeyCode.D)) xzMove.x += 1f;
if (Input.GetKey(KeyCode.A)) xzMove.x += -1f;
xzMove = xzMove.normalized * moveSpeed;
var transformedDisplacement = transform.right * xzMove.x + transform.forward * xzMove.z;
playerAcceleration += transformedDisplacement;
}
// add acceleration due to friction
if (isGrounded)
{
var forceByFriction = Vector3.ProjectOnPlane(playerVelocity.normalized, transform.up) * frictionDefault;
playerAcceleration -= forceByFriction;
}
// add to player's velocity based on acceleartion
playerVelocity += playerAcceleration * Time.deltaTime;
/*
// assume all platforms are stationary and parallel to the ground
if (isGrounded && playerVelocity.y < 0)
{
playerVelocity.y = 0f;
}
// when the player jumps on the ground, apply an instant force to the player's velocity
if (isGrounded && Input.GetButtonDown("Jump"))
{
playerVelocity.y += Mathf.Sqrt(jumpHeight * -2f * gravity);
}
// apply an instant force to the player's velocity when on the ground when they try to move
if (isGrounded)
{
if (playerVelocity.magnitude < maxWalkSpeed)
{
Vector3 xzMove = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
xzMove = xzMove.normalized * moveSpeed * Time.deltaTime;
playerVelocity += transform.right * xzMove.x + transform.forward * xzMove.z;
}
}
*/
// add to the player's position based on velocity
controller.Move(playerVelocity * Time.deltaTime);
Debug.Log(playerVelocity.magnitude);
}
#region Variables
[SerializeField] private float walkSpeed = 2f;
[SerializeField] private float maxWalkSpeed = 10f;
[SerializeField] private float sprintSpeed = 10f;
private float moveSpeed;
private float sprintBonus;
[SerializeField] private float jumpHeight = 2f;
[SerializeField] private float gravity = -10f;
[SerializeField] private float frictionDefault = 1f;
private CharacterController controller;
public Vector3 playerVelocity;
public Vector3 playerAcceleration;
private bool isGrounded;
#endregion
/*
// Update is called once per frame
void Update()
{
// ground check
// isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
isGrounded = controller.isGrounded;
if (isGrounded && playerVelocity.y < 0)
{
playerVelocity.y = -2f;
}
// sprint mechanic
float sprintInput = Input.GetAxis("Sprint");
// wasd player movement
float xMove = Input.GetAxis("Horizontal");
float zMove = Input.GetAxis("Vertical");
Vector3 xzMove = (transform.right * xMove + transform.forward * zMove) * (moveSpeed + sprintInput * sprintBonus);
// jump
if (Input.GetButtonDown("Jump") && isGrounded)
{
playerVelocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
// gravity
playerVelocity.y += gravity * Time.deltaTime;
// final player move
controller.Move((playerVelocity + xzMove) * Time.deltaTime);
//Debug.Log(controller.isGrounded);
}
*/
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e8682b16961bcd049923fff1bdc4126d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,78 @@
using UnityEngine;
using Player.Interactions;
public class PlayerMovement3 : MonoBehaviour
{
// Start is called before the first frame update
private void Start()
{
_controller = gameObject.GetComponent<CharacterController>();
_moveSpeed = walkSpeed;
_sprintBonus = sprintSpeed - walkSpeed;
grappleHook = gameObject.GetComponent<GrappleHook>();
}
// Update is called once per frame
private void Update()
{
// an object by default has no forces acting on it, and therefore, zero acceleration
playerAcceleration = Vector3.zero;
// acceleration due to gravity
playerAcceleration += new Vector3(0f, gravity, 0f);
// ground check
_isGrounded = _controller.isGrounded;
// assuming platforms are horizontal and stationary
if (_isGrounded && playerVelocity.y < 0) playerVelocity.y = -2f;
// instant force applied on jump
if (Input.GetButtonDown("Jump") && _isGrounded) playerVelocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
// sprint mechanic
var sprintInput = Input.GetAxis("Sprint");
// wasd player movement
var xMove = Input.GetAxis("Horizontal");
var zMove = Input.GetAxis("Vertical");
var transformedMove = transform.right * xMove + transform.forward * zMove;
if (transformedMove.magnitude > 1f) transformedMove = transformedMove.normalized;
var xzMove = transformedMove * (_moveSpeed + sprintInput * _sprintBonus);
// instance force applied when moving
_controller.Move(xzMove * Time.deltaTime);
// force applied by grapple hook
//playerVelocity += grappleHook.PullForce(transform.position) * Time.deltaTime;
// apply acceleration to player velocity
playerVelocity += playerAcceleration * Time.deltaTime;
// apply velocity to change position
_controller.Move(playerVelocity * Time.deltaTime);
//Debug.Log(controller.isGrounded);
}
#region Variables
[SerializeField] private float walkSpeed = 2f;
[SerializeField] private float sprintSpeed = 10f;
private float _moveSpeed;
private float _sprintBonus;
private CharacterController _controller;
public Vector3 playerAcceleration;
public Vector3 playerVelocity;
[SerializeField] private float jumpHeight = 2f;
[SerializeField] private float gravity = -10f;
private bool _isGrounded;
private GrappleHook grappleHook;
#endregion
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9c4fb8c78ea7749a3a9e88c9ae55e131
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,208 @@
using Player.Interactions;
using UnityEngine;
using Player.Information;
namespace Player.Movement
{
public class PlayerPhysics : MonoBehaviour
{
// measured in meters per second squared
[SerializeField] private Vector3 gravity;
[SerializeField] private float walkingForce;
[SerializeField] private float maxWalkSpeed;
[SerializeField] private float maxRunSpeed;
// measured in N applied when space is pressed
[SerializeField] private float jumpForce;
[SerializeField] private float defaultAirControl;
// the minimum change in speed which will result in damage being taken
[SerializeField] private float speedDamageThreshold;
// the ratio of speed change to damage taken
[SerializeField] private float speedToDamage;
// measured in seconds
[SerializeField] private float coyoteTime;
[SerializeField] private float jumpBuffer;
// current air control ratio experienced by the player
private float airControl;
private float coyoteTimeCounter;
// reference to GrappleHook script
private GrappleHook grappleHook;
// reference to JetPack script
private JetPack jetPack;
private float jumpBufferCounter;
// current maximum input speed of the player
private float maxSpeed;
// referendce to PlayerStats script
private PlayerStats playerStats;
// metrics for ground detection
//private float playerHeight;
//private Vector3 boxDim;
// speed of the player in the previous frame
private float prevFrameSpeed;
private Rigidbody rb;
public void Reset()
{
prevFrameSpeed = 0f;
rb.velocity = Vector3.zero;
grappleHook.ReleaseGrapple();
jetPack.ResetThrust();
}
// Start is called before the first frame update
private void Start()
{
rb = gameObject.GetComponent<Rigidbody>();
// ground detection constants
//playerHeight = (transform.localScale.y / 2f);
//Vector3 boxDim = transform.localScale;
//boxDim = new Vector3(boxDim.x - 0.5f, boxDim.y + 0.5f, boxDim.z - 0.5f);
maxSpeed = maxWalkSpeed;
airControl = defaultAirControl;
grappleHook = gameObject.GetComponent<GrappleHook>();
jetPack = gameObject.GetComponent<JetPack>();
playerStats = gameObject.GetComponent<PlayerStats>();
prevFrameSpeed = 0f;
}
// Update is called once per frame
private void Update()
{
var grounded = isGrounded();
// coyote time manager
if (grounded)
coyoteTimeCounter = coyoteTime;
else
coyoteTimeCounter -= Time.deltaTime;
// jump buffer manager
if (Input.GetButtonDown("Jump") )
jumpBufferCounter = jumpBuffer;
else
jumpBufferCounter -= Time.deltaTime;
//Debug.Log("Coyote: " + coyoteTimeCounter + ". Buffer: " + jumpBufferCounter);
// if jump key is pressed
if (coyoteTimeCounter > 0f && jumpBufferCounter > 0f)
{
Debug.Log("jump");
// apply an upward force to the player
rb.AddForce(new Vector3(0f, jumpForce, 0f));
coyoteTimeCounter = 0f;
jumpBufferCounter = 0f;
}
// change current max speed depending on sprinting or not
if (Input.GetKey(KeyCode.LeftShift) && grounded )
maxSpeed = maxRunSpeed;
else
maxSpeed = maxWalkSpeed;
}
private void FixedUpdate()
{
// get the input values for player movement
var movement = GetMoveInputs();
// calculate the velocity change of the player based on input
movement = movement.normalized * walkingForce;
// scale movement force if not on ground
if (!isGrounded()) movement *= airControl;
var relativeMove = transform.right * movement.x + transform.forward * movement.z;
var flatCurVelocity = Vector3.ProjectOnPlane(rb.velocity, transform.up);
// calculate the velocity the player would have when adding input velocity to it
var nextVelocity = flatCurVelocity + relativeMove * Time.deltaTime;
// check if the player's next velocity has a magnitude above walking speed
if (nextVelocity.magnitude > maxSpeed)
{
// if the player is currently moving below their maxSpeed and trying to accelerate up to it
if (flatCurVelocity.magnitude < maxSpeed)
// scale the player's next velocity to be equal to their maxSpeed
nextVelocity = nextVelocity.normalized * maxSpeed;
else
// else, scale the player's next velocity to equal their current speed
nextVelocity = nextVelocity.normalized * flatCurVelocity.magnitude;
}
// set the player's current velocity to their next velocity in the xz-plane
rb.velocity = new Vector3(nextVelocity.x, rb.velocity.y, nextVelocity.z);
// apply the force of a potential grapple hook to the player
rb.AddForce(grappleHook.PullForce(transform.position));
// apply the force of a potential jetpack to the player
rb.AddForce(jetPack.ThrustForce());
// apply the force of gravity to the player
rb.AddForce(gravity, ForceMode.Acceleration);
// calculate the change in player speed this frame
var deltaSpeed = Mathf.Abs(rb.velocity.magnitude - prevFrameSpeed);
// apply force damage to player depending on change in speed
if (deltaSpeed > speedDamageThreshold)
//Debug.Log(deltaSpeed - speedDamageThreshold);
playerStats.ChangeHealth(-speedToDamage * (deltaSpeed - speedDamageThreshold));
// update speed record for next frame
prevFrameSpeed = rb.velocity.magnitude;
//Debug.Log(isGrounded());
}
private void OnTriggerEnter(Collider other)
{
var tp = other.gameObject.GetComponent<TraversalProperties>();
if (tp != null && tp.lethalToEnter) playerStats.Respawn();
}
public Vector3 GetMoveInputs()
{
var xzMove = Vector3.zero;
if (Input.GetKey(KeyCode.W)) xzMove.z += 1f;
if (Input.GetKey(KeyCode.S)) xzMove.z += -1f;
if (Input.GetKey(KeyCode.D)) xzMove.x += 1f;
if (Input.GetKey(KeyCode.A)) xzMove.x += -1f;
return xzMove;
}
public bool isGrounded()
{
// Raycast using ray
//return Physics.Raycast(transform.position, -Vector3.up, playerHeight + 0.1f);
// Raycast using box
//Vector3 boxCenter = transform.position - new Vector3(0f, playerHeight, 0f);
//return Physics.CheckBox(boxCenter, boxDim);
// Raycast using box hardcoded
var boxCenter = transform.position - new Vector3(0f, 0.5f, 0f);
return Physics.CheckBox(boxCenter, new Vector3(0.25f, 0.52f, 0.25f));
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6371500714cc7409c92e2a14cced0fca
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: