mirror of
https://github.com/Leahnaya/TheKingsRace.git
synced 2026-08-21 09:54:28 -05:00
Cleaned up code
Slide Works again Ragdoll works again Cam is stable New Debugging prefab
This commit is contained in:
8
Assets/Scripts/PlayerScripts/DebugPlayer/DebugItems.meta
Normal file
8
Assets/Scripts/PlayerScripts/DebugPlayer/DebugItems.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2d32b8429f4a3df48a3585d68287b81a
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
102
Assets/Scripts/PlayerScripts/DebugPlayer/DebugItems/dBlink.cs
Normal file
102
Assets/Scripts/PlayerScripts/DebugPlayer/DebugItems/dBlink.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using MLAPI;
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
|
||||
public class dBlink : NetworkBehaviour{
|
||||
|
||||
// Start is called before the first frame update
|
||||
|
||||
private Camera cam;
|
||||
public CharacterController controller;
|
||||
//Blink Variables
|
||||
private LineRenderer beam;
|
||||
private Vector3 origin;
|
||||
private Vector3 endPoint;
|
||||
private Vector3 mousePos;
|
||||
private RaycastHit hit;
|
||||
|
||||
LayerMask ignoreP;
|
||||
/////
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
beam = gameObject.AddComponent<LineRenderer>();
|
||||
beam.startWidth = 0.2f;
|
||||
beam.endWidth = 0.2f;
|
||||
beam.enabled = false;
|
||||
|
||||
ignoreP = LayerMask.GetMask("Player");
|
||||
|
||||
controller = GetComponent<CharacterController>();
|
||||
}
|
||||
void Start(){
|
||||
|
||||
// Grab the main camera.
|
||||
//camera transform
|
||||
cam = GetComponentInChildren<Camera>();
|
||||
}
|
||||
|
||||
//ADJUST SO DISTANCE IS DETERMINED BY SCROLL WHEEL
|
||||
//blinks the player forwards
|
||||
private void BlinkMove()
|
||||
{
|
||||
if (!IsLocalPlayer) { return; }
|
||||
|
||||
if (Input.GetMouseButton(1))
|
||||
{
|
||||
// Finding the origin and end point of laser.
|
||||
origin = transform.position + transform.forward * transform.lossyScale.z;
|
||||
|
||||
// Finding mouse pos in 3D space.
|
||||
mousePos = Input.mousePosition;
|
||||
mousePos.z = 20f;
|
||||
endPoint = cam.ScreenToWorldPoint(mousePos);
|
||||
|
||||
// Find direction of beam.
|
||||
Vector3 dir = endPoint - origin;
|
||||
dir.Normalize();
|
||||
|
||||
// Are we hitting any colliders?
|
||||
if (Physics.Raycast(origin, dir, out hit, 20f))
|
||||
{
|
||||
// If yes, then set endpoint to hit-point.
|
||||
endPoint = hit.point;
|
||||
}
|
||||
|
||||
// Set end point of laser.
|
||||
beam.SetPosition(0, origin);
|
||||
beam.SetPosition(1, endPoint);
|
||||
// Draw the laser!
|
||||
beam.enabled = true;
|
||||
/*Ray ray = camera.ScreenPointToRay(Input.mousePosition);
|
||||
RaycastHit raycastHit;
|
||||
|
||||
if (Physics.Raycast(ray, out raycastHit, 5.0f)){
|
||||
LineRenderer.SetPosition(1, raycastHit.point);
|
||||
}*/
|
||||
|
||||
}
|
||||
|
||||
else if (!Input.GetMouseButton(1) && beam.enabled == true)
|
||||
{
|
||||
beam.enabled = false;
|
||||
//if teleporting due to hit to object, bump them a bit outside normal
|
||||
if (hit.point != null)
|
||||
{
|
||||
transform.position = endPoint + hit.normal * 1.25f;
|
||||
|
||||
}
|
||||
//if teleporting in the air or something, just spawn at endpoint
|
||||
else
|
||||
{
|
||||
|
||||
transform.position = endPoint;
|
||||
}
|
||||
//reenable character controller
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a504b3f7be0e5254e815029049abbeac
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
42
Assets/Scripts/PlayerScripts/DebugPlayer/DebugItems/dDash.cs
Normal file
42
Assets/Scripts/PlayerScripts/DebugPlayer/DebugItems/dDash.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using MLAPI;
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
public class dDash : NetworkBehaviour{
|
||||
public Vector3 moveDirection;
|
||||
|
||||
public const float maxDashTime = 1.0f;
|
||||
public float dashDistance = 10;
|
||||
public float dashStoppingSpeed = 0.1f;
|
||||
float currentDashTime = maxDashTime;
|
||||
float dashSpeed = 12;
|
||||
|
||||
CharacterController characterController;
|
||||
|
||||
void Start(){
|
||||
characterController = this.gameObject.GetComponent<CharacterController>();
|
||||
}
|
||||
|
||||
//UPDATE CHECK FOR MOVEMENT ONLY WHEN DASHING
|
||||
void FixedUpdate(){
|
||||
if (!IsLocalPlayer) { return; }
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.E))
|
||||
{
|
||||
currentDashTime = 0;
|
||||
}
|
||||
if(currentDashTime < maxDashTime)
|
||||
{
|
||||
moveDirection = transform.forward * dashDistance;
|
||||
currentDashTime += dashStoppingSpeed;
|
||||
}
|
||||
else
|
||||
{
|
||||
moveDirection = Vector3.zero;
|
||||
}
|
||||
characterController.Move(moveDirection * Time.deltaTime * dashSpeed);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b8e55f866f601244e9d99bc8e1c82334
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,70 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using MLAPI;
|
||||
using UnityEngine;
|
||||
|
||||
public class dGrapplingHook : NetworkBehaviour
|
||||
{
|
||||
public float maxGrappleDistance = 25;
|
||||
|
||||
private bool isGrappled;
|
||||
private int hookPointIndex;
|
||||
private GameObject hookPoint;
|
||||
private GameObject[] hookPoints;
|
||||
private float distance;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
isGrappled = false;
|
||||
hookPoints = GameObject.FindGameObjectsWithTag("HookPoint");
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (!IsLocalPlayer) { return; }
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.E)) //If grapple button is hit
|
||||
{
|
||||
if (!isGrappled) //If we are not grappling
|
||||
{
|
||||
hookPointIndex = FindHookPoint(); //Find the nearest hook point within max distance
|
||||
if (hookPointIndex != -1) //If there is a hookpoint
|
||||
{
|
||||
hookPoint = hookPoints[hookPointIndex]; //The point we are grappling from
|
||||
//physics set up?
|
||||
isGrappled = true; //toggle grappling state
|
||||
}
|
||||
}
|
||||
else //Else we are grappling
|
||||
{
|
||||
//physics tear down?
|
||||
isGrappled = false; //toggle grappling state to release
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (isGrappled)
|
||||
{
|
||||
//Do grappling physics based on hookPoint
|
||||
}
|
||||
}
|
||||
|
||||
int FindHookPoint()
|
||||
{
|
||||
float least = maxGrappleDistance;
|
||||
int index = -1;
|
||||
for(int i = 0; i<hookPoints.Length; i++)
|
||||
{
|
||||
distance = Vector3.Distance(gameObject.transform.position, hookPoints[i].transform.position);
|
||||
if (distance <= least)
|
||||
{
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a1f672a4741230d4a963fb68f5f8a8aa
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,76 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using MLAPI;
|
||||
using UnityEngine;
|
||||
|
||||
public class dKickController : NetworkBehaviour
|
||||
{
|
||||
//Important, may want to change blink code to only work for trigger collider
|
||||
// so that the player doesn't teleport to their leg
|
||||
private GameObject leg;
|
||||
private bool isKicking = false;
|
||||
//slightly bad practice, when merging find a better work around
|
||||
private bool isDiveKicking = false;
|
||||
private CharacterController characterController;
|
||||
|
||||
public PlayerStats pStats;
|
||||
|
||||
void Start(){
|
||||
pStats = GetComponent<PlayerStats>();
|
||||
|
||||
characterController = this.gameObject.GetComponent<CharacterController>();
|
||||
leg = transform.GetChild(0).gameObject;
|
||||
leg.SetActive(false);
|
||||
}
|
||||
|
||||
void Update(){
|
||||
Kick();
|
||||
}
|
||||
|
||||
void Kick(){
|
||||
//Note: when we merge this into PlayerMovement, we may want to change isgrounded to our
|
||||
//custom is grounded
|
||||
if (Input.GetKeyDown(KeyCode.F) && isKicking == false && characterController.isGrounded == false)
|
||||
{
|
||||
Debug.Log("dive");
|
||||
// if kicking in air, kick until grounded (maybe add some foward momentum if needeD)
|
||||
isKicking = true;
|
||||
isDiveKicking = true;
|
||||
leg.SetActive(true);
|
||||
}
|
||||
//otherwise do ground kick for .3 seconds
|
||||
else if (Input.GetKeyDown(KeyCode.F) && isKicking == false){
|
||||
Debug.Log("kick");
|
||||
StartCoroutine(Kicking(.3f));
|
||||
}
|
||||
|
||||
//once dive kick touches ground, set back to normal state
|
||||
if(characterController.isGrounded == true && isDiveKicking == true){
|
||||
isDiveKicking = false;
|
||||
isKicking = false;
|
||||
leg.SetActive(false);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator Kicking(float waitTime){
|
||||
isKicking = true;
|
||||
leg.SetActive(true);
|
||||
yield return new WaitForSeconds(waitTime);
|
||||
isKicking = false;
|
||||
leg.SetActive(false);
|
||||
|
||||
}
|
||||
|
||||
private void OnCollisionEnter(Collision collision)
|
||||
{
|
||||
if (!IsLocalPlayer) { return; }
|
||||
Collider myCollider = collision.contacts[0].thisCollider;
|
||||
if (collision.transform.CompareTag("kickable") && myCollider == leg.GetComponent<Collider>()){
|
||||
Vector3 direction = this.transform.forward;
|
||||
Debug.Log(direction);
|
||||
collision.rigidbody.AddForce(direction * pStats.KickPow, ForceMode.Impulse);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 813241e4d3933be429c9295081a09142
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
193
Assets/Scripts/PlayerScripts/DebugPlayer/DebugItems/dWallRun.cs
Normal file
193
Assets/Scripts/PlayerScripts/DebugPlayer/DebugItems/dWallRun.cs
Normal file
@@ -0,0 +1,193 @@
|
||||
using UnityEngine;
|
||||
using System.Linq;
|
||||
using MLAPI;
|
||||
using UnityEngine.Rendering;
|
||||
|
||||
[RequireComponent (typeof(PlayerMovement))]
|
||||
public class dWallRun : NetworkBehaviour
|
||||
{
|
||||
|
||||
public float wallMaxDistance = 1;
|
||||
public float wallSpeedMultiplier = 1.2f;
|
||||
public float minimumHeight = 1.2f;
|
||||
public float maxAngleRoll = 20;
|
||||
[Range(0.0f, 1.0f)]
|
||||
public float normalizedAngleThreshold = 0.1f;
|
||||
|
||||
public float jumpDuration = 1;
|
||||
public float wallBouncing = 3;
|
||||
public float cameraTransitionDuration = 1;
|
||||
|
||||
public float wallGravityDownForce = 20f;
|
||||
|
||||
[Space]
|
||||
PlayerMovement playerMovementController;
|
||||
|
||||
Vector3[] directions;
|
||||
RaycastHit[] hits;
|
||||
|
||||
bool isWallRunning = false;
|
||||
Vector3 lastWallPosition;
|
||||
Vector3 lastWallNormal;
|
||||
float elapsedTimeSinceJump = 0;
|
||||
float elapsedTimeSinceWallAttach = 0;
|
||||
float elapsedTimeSinceWallDetatch = 0;
|
||||
bool jumping;
|
||||
|
||||
bool isPlayergrounded() => playerMovementController.isGrounded;
|
||||
|
||||
public bool IsWallRunning() => isWallRunning;
|
||||
|
||||
bool CanWallRun()
|
||||
{
|
||||
float verticalAxis = Input.GetAxisRaw("Vertical");
|
||||
|
||||
return !isPlayergrounded() && verticalAxis > 0 && VerticalCheck();
|
||||
}
|
||||
|
||||
bool VerticalCheck()
|
||||
{
|
||||
return !Physics.Raycast(transform.position, Vector3.down, minimumHeight);
|
||||
}
|
||||
|
||||
|
||||
void Start()
|
||||
{
|
||||
playerMovementController = GetComponent<PlayerMovement>();
|
||||
|
||||
directions = new Vector3[]{
|
||||
Vector3.right,
|
||||
Vector3.right + Vector3.forward,
|
||||
Vector3.forward,
|
||||
Vector3.left + Vector3.forward,
|
||||
Vector3.left
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public void WallRunRoutine()
|
||||
{
|
||||
|
||||
if (!IsLocalPlayer) { return; }
|
||||
|
||||
isWallRunning = false;
|
||||
|
||||
if(playerMovementController.GetJumpPressed())
|
||||
{
|
||||
jumping = true;
|
||||
}
|
||||
|
||||
if(CanAttach())
|
||||
{
|
||||
hits = new RaycastHit[directions.Length];
|
||||
|
||||
for(int i=0; i<directions.Length; i++)
|
||||
{
|
||||
Vector3 dir = transform.TransformDirection(directions[i]);
|
||||
Physics.Raycast(transform.position, dir, out hits[i], wallMaxDistance);
|
||||
//if(hits[i].collider != null)
|
||||
//{
|
||||
//Debug.DrawRay(transform.position, dir * hits[i].distance, Color.green);
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
//Debug.DrawRay(transform.position, dir * wallMaxDistance, Color.red);
|
||||
//}
|
||||
}
|
||||
|
||||
if(CanWallRun())
|
||||
{
|
||||
hits = hits.ToList().Where(h => h.collider != null).OrderBy(h => h.distance).ToArray();
|
||||
if(hits.Length > 0)
|
||||
{
|
||||
if(hits[0].collider.tag == "WallRun")
|
||||
{
|
||||
OnWall(hits[0]);
|
||||
lastWallPosition = hits[0].point;
|
||||
lastWallNormal = hits[0].normal;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(isWallRunning)
|
||||
{
|
||||
elapsedTimeSinceWallDetatch = 0;
|
||||
elapsedTimeSinceWallAttach += Time.deltaTime;
|
||||
playerMovementController.AddPlayerVelocity((Vector3.down * wallGravityDownForce * Time.deltaTime));
|
||||
}
|
||||
else
|
||||
{
|
||||
elapsedTimeSinceWallAttach = 0;
|
||||
elapsedTimeSinceWallDetatch += Time.deltaTime;
|
||||
}
|
||||
}
|
||||
|
||||
bool CanAttach()
|
||||
{
|
||||
if(jumping)
|
||||
{
|
||||
elapsedTimeSinceJump += Time.deltaTime;
|
||||
if(elapsedTimeSinceJump > jumpDuration)
|
||||
{
|
||||
elapsedTimeSinceJump = 0;
|
||||
jumping = false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void OnWall(RaycastHit hit){
|
||||
float d = Vector3.Dot(hit.normal, Vector3.up);
|
||||
if(d >= -normalizedAngleThreshold && d <= normalizedAngleThreshold)
|
||||
{
|
||||
// Vector3 alongWall = Vector3.Cross(hit.normal, Vector3.up);
|
||||
float vertical = Input.GetAxisRaw("Vertical");
|
||||
Vector3 alongWall = transform.TransformDirection(Vector3.forward);
|
||||
|
||||
//Debug.DrawRay(transform.position, alongWall.normalized * 10, Color.green);
|
||||
//Debug.DrawRay(transform.position, lastWallNormal * 10, Color.magenta);
|
||||
|
||||
Vector3 moveToSet = alongWall * vertical * playerMovementController.PlayerSpeed() *Time.deltaTime;// * wallSpeedMultiplier;
|
||||
moveToSet.y = 0;
|
||||
|
||||
playerMovementController.SetPlayerVelocity(moveToSet);
|
||||
//Debug.Log("On Wall");
|
||||
isWallRunning = true;
|
||||
}
|
||||
}
|
||||
|
||||
float CalculateSide()
|
||||
{
|
||||
if(isWallRunning)
|
||||
{
|
||||
Vector3 heading = lastWallPosition - transform.position;
|
||||
Vector3 perp = Vector3.Cross(transform.forward, heading);
|
||||
float dir = Vector3.Dot(perp, transform.up);
|
||||
return dir;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public float GetCameraRoll() //Cause camera to roll when wall running - call to this is player movement
|
||||
{
|
||||
float dir = CalculateSide();
|
||||
float cameraAngle = playerMovementController.GetPlayerCamera().transform.eulerAngles.z;
|
||||
float targetAngle = 0;
|
||||
if(dir != 0)
|
||||
{
|
||||
targetAngle = Mathf.Sign(dir) * maxAngleRoll;
|
||||
}
|
||||
return Mathf.LerpAngle(cameraAngle, targetAngle, Mathf.Max(elapsedTimeSinceWallAttach, elapsedTimeSinceWallDetatch) / cameraTransitionDuration);
|
||||
}
|
||||
|
||||
public Vector3 GetWallJumpDirection() //Add call in jump where if we are wallrunning and jump, the jump vector is multiplied by this
|
||||
{
|
||||
if(isWallRunning)
|
||||
{
|
||||
return lastWallNormal * wallBouncing + Vector3.up;
|
||||
}
|
||||
return Vector3.zero;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f4264f4f7bed1d7499a037436582ed17
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
26
Assets/Scripts/PlayerScripts/DebugPlayer/dPlayCam.cs
Normal file
26
Assets/Scripts/PlayerScripts/DebugPlayer/dPlayCam.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using MLAPI;
|
||||
using UnityEngine;
|
||||
|
||||
public class dPlayCam : NetworkBehaviour
|
||||
{
|
||||
public GameObject player;
|
||||
|
||||
private Vector3 offset;
|
||||
private Vector3 rad;
|
||||
|
||||
void Start ()
|
||||
{
|
||||
rad = (transform.position - player.transform.position);
|
||||
}
|
||||
|
||||
void Update ()
|
||||
{
|
||||
if (!IsLocalPlayer) { return; }
|
||||
|
||||
offset = transform.parent.forward * rad.magnitude;
|
||||
transform.position = new Vector3((player.transform.position.x - offset.x),((player.transform.position.y - offset.y)+2),(player.transform.position.z - offset.z));
|
||||
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/PlayerScripts/DebugPlayer/dPlayCam.cs.meta
Normal file
11
Assets/Scripts/PlayerScripts/DebugPlayer/dPlayCam.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f8178c747ab202f4bb17b33e02bf63a5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
460
Assets/Scripts/PlayerScripts/DebugPlayer/dPlayerMovement.cs
Normal file
460
Assets/Scripts/PlayerScripts/DebugPlayer/dPlayerMovement.cs
Normal file
@@ -0,0 +1,460 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using MLAPI;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class dPlayerMovement : NetworkBehaviour
|
||||
{
|
||||
|
||||
//Scripts
|
||||
public PlayerStats pStats;
|
||||
|
||||
//Variable Section
|
||||
/////
|
||||
//Speed Variables
|
||||
public Vector3 vel;
|
||||
|
||||
private Vector3 moveZ;
|
||||
private Vector3 moveX;
|
||||
private Vector3 driftVel;
|
||||
|
||||
//Player prefab
|
||||
private GameObject parentObj;
|
||||
|
||||
//Character Moving
|
||||
private CharacterController moveController;
|
||||
|
||||
//Jump value
|
||||
private int curJumpNum;
|
||||
private bool jumpPressed;
|
||||
bool tempSet = false;
|
||||
float tempTraction = 0.0f;
|
||||
|
||||
//Jump physics
|
||||
private float mass = 5.0F; // defines the character mass
|
||||
private Vector3 impact = Vector3.zero;
|
||||
private float distToGround;
|
||||
|
||||
//Wallrunning
|
||||
private WallRun wallRun;
|
||||
|
||||
//Ground Check
|
||||
public bool isGrounded; //Better custom is grounded
|
||||
public float groundCheckDistance = 0.05f; //how far away from the ground to not be considered grounded
|
||||
private float lastTimeJumped = 0f; //Last time the player jumped
|
||||
const float jumpGroundingPreventionTime = 0.2f; // delay in checking if we are grounded after a jump
|
||||
const float groundCheckDistanceInAir = 0.07f; //How close we have to get to ground to start checking for grounded again
|
||||
private Ray groundRay;
|
||||
private RaycastHit groundHit;
|
||||
|
||||
//Camera Variables
|
||||
private LayerMask ignoreP;
|
||||
private Vector3 camRotation;
|
||||
private Camera cam;
|
||||
|
||||
[Range(-45, -15)]
|
||||
public int minAngle = -30;
|
||||
[Range(30, 80)]
|
||||
public int maxAngle = 45;
|
||||
[Range(50, 500)]
|
||||
public int sensitivity = 200;
|
||||
|
||||
//Ragdoll variables
|
||||
private Vector3 hit;
|
||||
private Rigidbody rB;
|
||||
private CapsuleCollider capCol;
|
||||
private bool firstHit = false;
|
||||
private bool heldDown = false;
|
||||
private bool beginRagTimer = false;
|
||||
private float ragTime;
|
||||
private Vector3 prevRot;
|
||||
private Vector3 hitForce;
|
||||
|
||||
//Slide Variables
|
||||
private bool isSliding = false;
|
||||
private float originalTraction;
|
||||
private RaycastHit ray;
|
||||
private Vector3 up;
|
||||
private bool qDown;
|
||||
|
||||
//Kick Variables
|
||||
|
||||
void Awake()
|
||||
{
|
||||
//Initialize Components
|
||||
moveController = GetComponent<CharacterController>();
|
||||
rB = GetComponent<Rigidbody>();
|
||||
capCol = GetComponent<CapsuleCollider>();
|
||||
pStats = GetComponent<PlayerStats>();
|
||||
parentObj = transform.parent.gameObject;
|
||||
|
||||
//camera transform
|
||||
cam = parentObj.GetComponentInChildren<Camera>();
|
||||
|
||||
capCol.enabled = false;
|
||||
//Wallrun
|
||||
wallRun = gameObject.GetComponent<WallRun>();
|
||||
|
||||
up = this.gameObject.GetComponentInParent<Transform>().up;
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
distToGround = GetComponent<Collider>().bounds.extents.y;
|
||||
|
||||
// Don't do movement unless this is the local player controlling it
|
||||
// Otherwise we let the server handle moving us
|
||||
|
||||
if (!IsLocalPlayer) { return; }
|
||||
|
||||
// Don't lock the cursor multiple times if this isn't the local player
|
||||
// Also don't want to lock the cursor for the king
|
||||
// That is why this is after the LocalPlayer check
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void FixedUpdate()
|
||||
{
|
||||
// Don't do movement unless this is the local player controlling it
|
||||
// Otherwise we let the server handle moving us
|
||||
if (!IsLocalPlayer) { return; }
|
||||
|
||||
|
||||
//Controls for camera
|
||||
Rotation();
|
||||
|
||||
|
||||
//Allow Movement when moveController is enabled
|
||||
if(moveController.enabled == true){
|
||||
|
||||
//input controls for movement
|
||||
InputController();
|
||||
|
||||
//if suffiecient impact magnitude is applied then move player
|
||||
if (impact.magnitude > 0.2F) moveController.Move(impact * Time.deltaTime);
|
||||
|
||||
// consumes the impact energy each cycle:
|
||||
impact = Vector3.Lerp(impact, Vector3.zero, 5*Time.deltaTime);
|
||||
}
|
||||
else{
|
||||
|
||||
if (RagdollTimer() == 0){
|
||||
|
||||
firstHit = false;
|
||||
DisableRagdoll();
|
||||
}
|
||||
|
||||
//Gravity without moveController
|
||||
vel.y -= pStats.PlayerGrav * Time.deltaTime;
|
||||
rB.AddForce(new Vector3(0,vel.y,0));
|
||||
|
||||
//Debug.LogWarning("MoveController is either Disabled or wasn't retrieved correctly");
|
||||
}
|
||||
|
||||
//TEMP FOR TESTING RAGDOLL
|
||||
//Right Click to ragdoll the player
|
||||
if (Input.GetMouseButton(1) && heldDown == false){
|
||||
getHit(new Vector3(vel.x, 0, vel.z), 30);
|
||||
heldDown = true;
|
||||
}
|
||||
if(!Input.GetMouseButton(1)){
|
||||
heldDown = false;
|
||||
}
|
||||
|
||||
//TEMP FOR TESTING
|
||||
//Checks if player should respawn
|
||||
Respawn();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
//Reads inputs and moves player
|
||||
private void InputController()
|
||||
{
|
||||
//Check if player is grounded before each frame
|
||||
GroundCheck();
|
||||
|
||||
//Keyboard inputs
|
||||
|
||||
//Checks if movement keys have been pressed and calculates correct vector
|
||||
moveX = transform.right * Input.GetAxis("Horizontal") * Time.deltaTime * PlayerSpeed();
|
||||
moveZ = transform.forward * Input.GetAxis("Vertical") * Time.deltaTime * PlayerSpeed();
|
||||
|
||||
//Adds vectors based on movement keys and other conditions to check what the
|
||||
//player vector should be under the circumstances
|
||||
vel = moveX + moveZ;
|
||||
|
||||
//Gravity
|
||||
Gravity();
|
||||
|
||||
driftVel = Vector3.Lerp(driftVel, vel, pStats.Traction * Time.deltaTime);
|
||||
|
||||
//Moving outside basic wasd
|
||||
//Jump Function
|
||||
Jump();
|
||||
//Slide Function
|
||||
Slide();
|
||||
|
||||
//Move Player
|
||||
moveController.Move(driftVel);
|
||||
}
|
||||
|
||||
|
||||
|
||||
//Calculates speed current player needs to be going
|
||||
public float PlayerSpeed()
|
||||
{
|
||||
//If nothing is pressed speed is 0
|
||||
if ((Input.GetAxis("Vertical") == 0.0f && Input.GetAxis("Horizontal") == 0.0f) || isSliding)
|
||||
{
|
||||
pStats.CurVel = 0.0f;
|
||||
return pStats.CurVel;
|
||||
}
|
||||
//If current speed is below min when pressed set to minimum speed
|
||||
else if (pStats.CurVel < pStats.MinVel)
|
||||
{
|
||||
pStats.CurVel = pStats.MinVel;
|
||||
return pStats.MinVel;
|
||||
}
|
||||
// while the speed is below max speed slowly increase it
|
||||
else if ((pStats.CurVel >= pStats.MinVel) && (pStats.CurVel < pStats.MaxVel))
|
||||
{
|
||||
pStats.CurVel += pStats.Acc;
|
||||
return pStats.CurVel;
|
||||
}
|
||||
//If the players speed is above or equal to max speed set speed to max
|
||||
else
|
||||
{
|
||||
pStats.CurVel = pStats.MaxVel;
|
||||
return pStats.CurVel;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//Apply Impact for when force needs to be applied without ragdolling
|
||||
public void AddImpact(Vector3 dir, float force)
|
||||
{
|
||||
|
||||
if (!IsLocalPlayer) { return; }
|
||||
|
||||
dir.Normalize();
|
||||
if (dir.y < 0) dir.y = -dir.y; // reflect down force on the ground
|
||||
impact += dir.normalized * force / mass;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//Jump Function
|
||||
private void Jump()
|
||||
{
|
||||
//If space is pressed apply an upwards force to the player
|
||||
if (Input.GetAxis("Jump") != 0 && !jumpPressed && curJumpNum + 1 < pStats.JumpNum && !isSliding)
|
||||
{
|
||||
AddImpact(transform.up, pStats.JumpPow);
|
||||
curJumpNum++;
|
||||
jumpPressed = true;
|
||||
}
|
||||
|
||||
lastTimeJumped = Time.time;
|
||||
|
||||
//If grounded no jumps have been used
|
||||
if(isGrounded){
|
||||
curJumpNum = 0;
|
||||
}
|
||||
|
||||
//If space isn't being pressed then jump is false
|
||||
if (Input.GetAxis("Jump") == 0) jumpPressed = false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//PlayerScript
|
||||
public bool GetJumpPressed(){
|
||||
return jumpPressed;
|
||||
}
|
||||
|
||||
public Camera GetPlayerCamera()
|
||||
{
|
||||
return cam;
|
||||
}
|
||||
|
||||
public void AddPlayerVelocity(Vector3 additiveVelocity)
|
||||
{
|
||||
vel += additiveVelocity;
|
||||
}
|
||||
|
||||
public void SetPlayerVelocity(Vector3 newVelocity)
|
||||
{
|
||||
vel = newVelocity;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//Camera and Player Rotation
|
||||
private void Rotation()
|
||||
{
|
||||
Vector3 lastCamPos = new Vector3(0,0,0);
|
||||
Vector3 rotOffset = transform.localEulerAngles;
|
||||
if(moveController.enabled){
|
||||
transform.parent.Rotate(Vector3.up * sensitivity * Time.deltaTime * Input.GetAxis("Mouse X"));
|
||||
|
||||
|
||||
camRotation.x -= Input.GetAxis("Mouse Y") * sensitivity * Time.deltaTime;
|
||||
camRotation.x = Mathf.Clamp(camRotation.x, minAngle, maxAngle);
|
||||
|
||||
cam.transform.localEulerAngles = camRotation;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//REMOVE WHEN UNNECCESARY
|
||||
//Respawns player if they fall below a certain point
|
||||
private void Respawn()
|
||||
{
|
||||
if (transform.position.y < -1)
|
||||
{
|
||||
transform.position = new Vector3(1f, 3f, 1f);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//Gravity Function for adjusting y-vel due to wallrun/glide/etc
|
||||
private void Gravity(){
|
||||
|
||||
//Gliding
|
||||
if(jumpPressed && pStats.HasGlider){
|
||||
|
||||
vel.y -= (pStats.PlayerGrav-18) * Time.deltaTime;
|
||||
if(tempSet == false){
|
||||
tempTraction = pStats.Traction;
|
||||
pStats.Traction = 1.0f;
|
||||
tempSet = true;
|
||||
}
|
||||
}
|
||||
else{
|
||||
|
||||
if(tempSet == true){
|
||||
pStats.Traction = tempTraction;
|
||||
tempSet = false;
|
||||
}
|
||||
|
||||
//Normal Gravity
|
||||
vel.y -= pStats.PlayerGrav * Time.deltaTime;
|
||||
}
|
||||
|
||||
//Wallrunning
|
||||
if (pStats.HasWallrun) { wallRun.WallRunRoutine(); } //adjusted later if we are wallrunning
|
||||
//If gliding
|
||||
//Go down slowly
|
||||
}
|
||||
|
||||
|
||||
//Checks if player is grounded
|
||||
void GroundCheck()
|
||||
{
|
||||
// Make sure that the ground check distance while already in air is very small, to prevent suddenly snapping to ground
|
||||
float chosenGroundCheckDistance = isGrounded ? (moveController.skinWidth + groundCheckDistance) : groundCheckDistanceInAir;
|
||||
// reset values before the ground check
|
||||
isGrounded = false;
|
||||
groundRay = new Ray(moveController.transform.position, Vector3.down);
|
||||
if (Physics.Raycast(groundRay, out groundHit, moveController.height + groundCheckDistance)) //&& Time.time >= lastTimeJumped + jumpGroundingPreventionTime) // only try to detect ground if it's been a short amount of time since last jump; otherwise we may snap to the ground instantly after we try jumping
|
||||
{
|
||||
// Only consider this a valid ground hit if the ground normal goes in the same direction as the character up
|
||||
if (Vector3.Dot(groundHit.normal, transform.up) > 0f)
|
||||
{
|
||||
isGrounded = true;
|
||||
// handle snapping to the ground
|
||||
if (groundHit.distance > moveController.skinWidth)
|
||||
{
|
||||
moveController.Move(Vector3.down * groundHit.distance);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Ragdoll Functions
|
||||
private void getHit(Vector3 dir, float force){
|
||||
if(firstHit == false){
|
||||
EnableRagdoll();
|
||||
dir.Normalize();
|
||||
rB.AddForce(dir * force, ForceMode.Impulse);
|
||||
firstHit = true;
|
||||
}
|
||||
}
|
||||
private void EnableRagdoll(){
|
||||
ragTime = pStats.RecovTime;
|
||||
prevRot = transform.localEulerAngles;
|
||||
capCol.enabled = true;
|
||||
moveController.enabled = false;
|
||||
rB.isKinematic = false;
|
||||
rB.detectCollisions = true;
|
||||
}
|
||||
|
||||
private void DisableRagdoll(){
|
||||
capCol.enabled = false;
|
||||
moveController.enabled = true;
|
||||
rB.isKinematic = true;
|
||||
rB.detectCollisions = false;
|
||||
transform.localEulerAngles = prevRot;
|
||||
}
|
||||
|
||||
//When to begin the ragdoll timer
|
||||
private float RagdollTimer(){
|
||||
if(beginRagTimer == false){
|
||||
beginRagTimer = Physics.Raycast(transform.position, -Vector3.up, distToGround + 1f);
|
||||
}
|
||||
|
||||
else if(ragTime <= 0){
|
||||
ragTime = 0;
|
||||
beginRagTimer = false;
|
||||
}
|
||||
|
||||
if(beginRagTimer == true){
|
||||
ragTime -= Time.deltaTime;
|
||||
}
|
||||
|
||||
return ragTime;
|
||||
}
|
||||
|
||||
//Slide Function
|
||||
private void Slide(){
|
||||
if (Input.GetKey(KeyCode.Q)){
|
||||
qDown = true;
|
||||
if (isSliding == false){
|
||||
originalTraction = pStats.Traction;
|
||||
this.gameObject.transform.eulerAngles = new Vector3(this.transform.eulerAngles.x - 90, this.transform.eulerAngles.y, this.transform.eulerAngles.z);
|
||||
isSliding = true;
|
||||
moveController.height = 1.0f;
|
||||
pStats.Traction = 0.01f;
|
||||
|
||||
}
|
||||
pStats.Traction += .004f;
|
||||
}
|
||||
else{
|
||||
qDown = false;
|
||||
}
|
||||
//NOTE: potentialy change this to only allow player back up if there is nothing above them
|
||||
if (qDown == false && isSliding == true) {
|
||||
//if nothing is above the object, stop slidding
|
||||
if (Physics.Raycast(this.gameObject.transform.position, up, out ray, 5f) == false)
|
||||
{
|
||||
this.gameObject.transform.eulerAngles = new Vector3(this.transform.eulerAngles.x + 90, this.transform.eulerAngles.y, this.transform.eulerAngles.z);
|
||||
isSliding = false;
|
||||
moveController.height = 2.0f;
|
||||
pStats.Traction = originalTraction;
|
||||
|
||||
}
|
||||
else{
|
||||
Debug.Log("Object above you");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 77da7821cb08097418fbac702ac00f93
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user