Merge branch 'rigidBodySetup'

This commit is contained in:
Melbyj1125
2021-11-01 12:21:31 -05:00
17 changed files with 484 additions and 266 deletions

View File

@@ -19,6 +19,9 @@ public class PlayerMovement : NetworkBehaviour
private Vector3 moveX;
private Vector3 driftVel;
//Player prefab
private GameObject parentObj;
//Character Moving
private CharacterController moveController;
@@ -63,9 +66,20 @@ public class PlayerMovement : NetworkBehaviour
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
@@ -73,13 +87,16 @@ public class PlayerMovement : NetworkBehaviour
rB = GetComponent<Rigidbody>();
capCol = GetComponent<CapsuleCollider>();
pStats = GetComponent<PlayerStats>();
parentObj = transform.parent.gameObject;
//camera transform
cam = GetComponentInChildren<Camera>();
cam = parentObj.GetComponentInChildren<Camera>();
capCol.enabled = false;
//Wallrun
//wallRun = gameObject.GetComponent<WallRun>();
wallRun = gameObject.GetComponent<WallRun>();
up = this.gameObject.GetComponentInParent<Transform>().up;
}
void Start()
@@ -103,6 +120,7 @@ public class PlayerMovement : NetworkBehaviour
// Otherwise we let the server handle moving us
if (!IsLocalPlayer) { return; }
//Controls for camera
Rotation();
@@ -119,7 +137,7 @@ public class PlayerMovement : NetworkBehaviour
}
else{
if (rB.velocity.magnitude < 0.2f){
if (RagdollTimer() == 0){
firstHit = false;
DisableRagdoll();
@@ -147,8 +165,6 @@ public class PlayerMovement : NetworkBehaviour
//Checks if player should respawn
Respawn();
}
@@ -172,9 +188,13 @@ public class PlayerMovement : NetworkBehaviour
Gravity();
driftVel = Vector3.Lerp(driftVel, vel, pStats.Traction * Time.deltaTime);
//Moving outside basic wasd
//Jump Function
Jump();
//Slide Function
Slide();
moveController.Move(driftVel);
}
@@ -184,7 +204,7 @@ public class PlayerMovement : NetworkBehaviour
public float PlayerSpeed()
{
//If nothing is pressed speed is 0
if (Input.GetAxis("Vertical") == 0.0f && Input.GetAxis("Horizontal") == 0.0f)
if ((Input.GetAxis("Vertical") == 0.0f && Input.GetAxis("Horizontal") == 0.0f) || isSliding)
{
pStats.CurVel = 0.0f;
return pStats.CurVel;
@@ -225,7 +245,7 @@ public class PlayerMovement : NetworkBehaviour
private void Jump()
{
//If space is pressed apply an upwards force to the player
if (Input.GetAxis("Jump") != 0 && !jumpPressed && curJumpNum + 1 < pStats.JumpNum)
if (Input.GetAxis("Jump") != 0 && !jumpPressed && curJumpNum + 1 < pStats.JumpNum && !isSliding)
{
AddImpact(transform.up, pStats.JumpPow);
curJumpNum++;
@@ -270,12 +290,22 @@ public class PlayerMovement : NetworkBehaviour
//Camera and Player Rotation
private void Rotation()
{
transform.Rotate(Vector3.up * sensitivity * Time.deltaTime * Input.GetAxis("Mouse X"));
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;
}
/*
else{
cam.transform.localEulerAngles = cam.transform.localEulerAngles - rotOffset;
}
*/
}
@@ -356,20 +386,91 @@ public class PlayerMovement : NetworkBehaviour
}
}
private void EnableRagdoll(){
prevRot = transform.localEulerAngles;
capCol.enabled = true;
moveController.enabled = false;
rB.isKinematic = false;
rB.detectCollisions = true;
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;
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 += .02f;
}
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");
}
}
//if button is not held down, and still slidding (if they let go but something was above them) check to see if something is still above them, if not
else if (Input.GetKey(KeyCode.Q) == 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");
}
}
}
}