Ground Check Fixes

-Simplified and re calibrated ground check
-Ground Check now functions well, test with inclines
-Temporarily commented out  player inventory lines that produced errors
-Commented out debug statements
This commit is contained in:
wheatv3015
2021-10-13 21:32:25 -05:00
parent 69be815a58
commit 114b567445
4 changed files with 93 additions and 93 deletions

View File

@@ -37,8 +37,8 @@ public class PlayerInventory : MonoBehaviour
}
void Start(){
AddItem(invMan.ItemList[0]);
AddItem(invMan.ItemList[1]);
//AddItem(invMan.ItemList[0]);
// AddItem(invMan.ItemList[1]);
foreach (Item item in items){
Debug.Log(item.name);
item.Equip(pStats);

View File

@@ -17,7 +17,7 @@ public class PlayerMovement : MonoBehaviour
private Vector3 moveZ;
private Vector3 moveX;
private Vector3 driftVel;
//Character Moving
private CharacterController moveController;
@@ -35,12 +35,13 @@ public class PlayerMovement : MonoBehaviour
private WallRun wallRun;
//Ground Check
public bool isGrounded { get; private set; } //Better custom is grounded
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
public LayerMask groundCheckLayers = -1; //Physics layers checked to consider the player grounded
private Ray groundRay;
private RaycastHit groundHit;
//Camera Variables
@@ -63,7 +64,8 @@ public class PlayerMovement : MonoBehaviour
private RaycastHit hit;
/////
void Awake(){
void Awake()
{
//Initialize Components
moveController = GetComponent<CharacterController>();
pStats = GetComponent<PlayerStats>();
@@ -83,13 +85,15 @@ public class PlayerMovement : MonoBehaviour
void Start(){
void Start()
{
distToGround = GetComponent<Collider>().bounds.extents.y;
}
// Update is called once per frame
void FixedUpdate(){
void FixedUpdate()
{
//input controls for movement
InputController();
@@ -101,7 +105,7 @@ public class PlayerMovement : MonoBehaviour
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);
impact = Vector3.Lerp(impact, Vector3.zero, 5 * Time.deltaTime);
//Checks if player should respawn
Respawn();
@@ -112,7 +116,8 @@ public class PlayerMovement : MonoBehaviour
//Reads inputs and moves player
private void InputController(){
private void InputController()
{
//Check if player is grounded before each frame
GroundCheck();
//Keyboard inputs
@@ -120,7 +125,7 @@ public class PlayerMovement : MonoBehaviour
//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;
@@ -128,34 +133,39 @@ public class PlayerMovement : MonoBehaviour
//Gravity
Gravity();
driftVel = Vector3.Lerp(driftVel, vel, pStats.Traction*Time.deltaTime);
driftVel = Vector3.Lerp(driftVel, vel, pStats.Traction * Time.deltaTime);
//Jump Function
Jump();
moveController.Move(driftVel);
}
}
//Calculates speed current player needs to be going
public float PlayerSpeed(){
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)
{
pStats.CurVel = 0.0f;
return pStats.CurVel;
return pStats.CurVel;
}
//If current speed is below min when pressed set to minimum speed
else if(pStats.CurVel < pStats.MinVel){
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)){
else if ((pStats.CurVel >= pStats.MinVel) && (pStats.CurVel < pStats.MaxVel))
{
pStats.CurVel += pStats.Acc;
return pStats.CurVel;
return pStats.CurVel;
}
//If the players speed is above or equal to max speed set speed to max
else{
else
{
pStats.CurVel = pStats.MaxVel;
return pStats.CurVel;
}
@@ -164,7 +174,8 @@ public class PlayerMovement : MonoBehaviour
//Applies impact in a direction with the given force
public void AddImpact(Vector3 dir, float force){
public void AddImpact(Vector3 dir, float force)
{
dir.Normalize();
if (dir.y < 0) dir.y = -dir.y; // reflect down force on the ground
impact += dir.normalized * force / mass;
@@ -173,9 +184,11 @@ public class PlayerMovement : MonoBehaviour
//Jump Function
private void Jump(){
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)
{
AddImpact(transform.up, pStats.JumpPow);
curJumpNum++;
jumpPressed = true;
@@ -186,23 +199,26 @@ public class PlayerMovement : MonoBehaviour
//NEEDS TO BE MASSIVELY CHANGE LIKELY USE RAYCAST TO CHECK IF ACTUALLY ON GROUND
//CANNOT USE CHARACTERCONTROLLER.ISGROUNDED IT IS UNRELIABLE
//If grounded no jumps have been used
// if(IsGrounded()){
// curJumpNum = 0;
// }
if(isGrounded){
curJumpNum = 0;
}
//If space isn't being pressed then jump is false
if(Input.GetAxis("Jump")==0) jumpPressed = false;
if (Input.GetAxis("Jump") == 0) jumpPressed = false;
}
public bool GetJumpPressed() {
public bool GetJumpPressed()
{
return jumpPressed;
}
public Camera GetPlayerCamera() {
return cam;
public Camera GetPlayerCamera()
{
return cam;
}
public void AddPlayerVelocity(Vector3 additiveVelocity) {
public void AddPlayerVelocity(Vector3 additiveVelocity)
{
vel += additiveVelocity;
}
@@ -226,73 +242,53 @@ public class PlayerMovement : MonoBehaviour
//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, 1f, 1f);
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(){
private void Gravity()
{
//Normal Gravity
vel.y -= pStats.PlayerGrav * Time.deltaTime;
vel.y -= pStats.PlayerGrav * Time.deltaTime;
//Wallrunning
if (pStats.HasWallrun) { wallRun.WallRunRoutine(); } //adjusted later if we are wallrunning
//If gliding
//Go down slowly
//If gliding
//Go down slowly
}
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;
Vector3 groundNormal = Vector3.up;
// 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
if (Time.time >= lastTimeJumped + jumpGroundingPreventionTime)
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
{
// if we're grounded, collect info about the ground normal with a downward capsule cast representing our character capsule
if (Physics.CapsuleCast(GetCapsuleBottomHemisphere(), GetCapsuleTopHemisphere(moveController.height), moveController.radius, Vector3.down, out RaycastHit hit, chosenGroundCheckDistance, groundCheckLayers, QueryTriggerInteraction.Ignore))
// 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)
{
// storing the upward direction for the surface found
groundNormal = hit.normal;
// Only consider this a valid ground hit if the ground normal goes in the same direction as the character up
// and if the slope angle is lower than the character controller's limit
if (Vector3.Dot(hit.normal, transform.up) > 0f && IsNormalUnderSlopeLimit(groundNormal))
isGrounded = true;
// handle snapping to the ground
if (groundHit.distance > moveController.skinWidth)
{
isGrounded = true;
// handle snapping to the ground
if (hit.distance > moveController.skinWidth)
{
moveController.Move(Vector3.down * hit.distance);
}
moveController.Move(Vector3.down * groundHit.distance);
}
}
}
}
private bool IsNormalUnderSlopeLimit(Vector3 normal){ // Returns true if the slope angle represented by the given normal is under the slope angle limit of the character controller
return Vector3.Angle(transform.up, normal) <= moveController.slopeLimit;
}
private Vector3 GetCapsuleBottomHemisphere(){ // Gets the center point of the bottom hemisphere of the character controller capsule
return transform.position + (transform.up * moveController.radius);
}
private Vector3 GetCapsuleTopHemisphere(float atHeight){ // Gets the center point of the top hemisphere of the character controller capsule
return transform.position + (transform.up * (atHeight - moveController.radius));
}
//ADJUST SO DISTANCE IS DETERMINED BY SCROLL WHEEL
//blinks the player forwards
private void Blink(){
if (Input.GetMouseButton(1)){
private void Blink()
{
if (Input.GetMouseButton(1))
{
// Finding the origin and end point of laser.
origin = transform.position + transform.forward * transform.lossyScale.z;
@@ -306,7 +302,8 @@ public class PlayerMovement : MonoBehaviour
dir.Normalize();
// Are we hitting any colliders?
if (Physics.Raycast(origin, dir, out hit, 20f)){
if (Physics.Raycast(origin, dir, out hit, 20f))
{
// If yes, then set endpoint to hit-point.
endPoint = hit.point;
}
@@ -325,19 +322,22 @@ public class PlayerMovement : MonoBehaviour
}
else if(!Input.GetMouseButton(1) && beam.enabled == true){
else if (!Input.GetMouseButton(1) && beam.enabled == true)
{
beam.enabled = false;
//disable character controller for a brief second for teleportation
//gameObject.GetComponent<CharacterController>().enabled = false;
//get
Vector3 bump = new Vector3(0, .5f, 0);
//if teleporting due to hit to object, bump them a bit outside normal
if(hit.point != null) {
if (hit.point != null)
{
transform.position = endPoint + hit.normal * 1.25f;
}
//if teleporting in the air or something, just spawn at endpoint
else{
else
{
transform.position = endPoint;
}
@@ -345,5 +345,5 @@ public class PlayerMovement : MonoBehaviour
}
}
}

View File

@@ -81,14 +81,14 @@ public class WallRun : MonoBehaviour
{
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(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())
@@ -142,14 +142,14 @@ public class WallRun : MonoBehaviour
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);
//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");
//Debug.Log("On Wall");
isWallRunning = true;
}
}