public bool IsGrounded(float distanceToCheck, LayerMask groundMask) { for (var i = 0; i < floorCheckers.Length; i++) { groundInfo[i] = GroundChecking.GetGroundHitInfo(floorCheckers[i], distanceToCheck + groundCheckOffset, groundMask); } if (EnemyBounceHit() != null) { var enemyTransform = EnemyBounceHit().transform; currentEnemyAi = enemyTransform.GetComponent <EnemyAI>(); currentEnemyAi.BouncedOn(); dealDamage.Attack(enemyTransform.gameObject, 1, 0f, 0f); } slopeNormal = AverageContactNormal(); slopeAngle = Vector3.Angle(slopeNormal, Vector3.up); currentlyGrounded = PointsOfContact() != 0; if (currentlyGrounded && ResetCamOnGrounded) { Invoke(nameof(LockCamNow), camLockResetTime); } if (!currentlyGrounded) { CancelInvoke(nameof(LockCamNow)); } col.material = currentlyGrounded ? frictionPlayerMat : frictionlessPlayerMat; return(currentlyGrounded); }
//returns whether we are on the ground or not //also: bouncing on enemies, keeping player on moving platforms and slope checking private bool IsGrounded() { //get distance to ground, from centre of collider (where floorcheckers should be) float dist = GetComponent <Collider>().bounds.extents.y; //check whats at players feet, at each floorcheckers position foreach (Transform check in floorCheckers) { RaycastHit hit; if (Physics.Raycast(check.position, Vector3.down, out hit, dist + 0.05f)) { if (!hit.transform.GetComponent <Collider>().isTrigger) { //slope control slope = Vector3.Angle(hit.normal, Vector3.up); //slide down slopes if (slope > slopeLimit && hit.transform.tag != "Pushable") { Vector3 slide = new Vector3(0f, -slideAmount, 0f); rigid.AddForce(slide, ForceMode.Force); } //enemy bouncing if (hit.transform.tag == "Enemy" && rigid.velocity.y < 0) { enemyAI = hit.transform.GetComponent <EnemyAI>(); enemyAI.BouncedOn(); onEnemyBounce++; dealDamage.Attack(hit.transform.gameObject, 1, 0f, 0f); } else { onEnemyBounce = 0; } //moving platforms if (hit.transform.tag == "MovingPlatform" || hit.transform.tag == "Pushable") { //rigid.drag = 5.0f; movingObjSpeed = hit.transform.GetComponent <Rigidbody>().velocity; movingObjSpeed.y = 0f; //9.5f is a magic number, if youre not moving properly on platforms, experiment with this number rigid.AddForce(movingObjSpeed * 7.7f * Time.fixedDeltaTime, ForceMode.VelocityChange); animator.SetBool("M_Platform", true); ForceIdle(); } else { movingObjSpeed = Vector3.zero; animator.SetBool("M_Platform", false); } //yes our feet are on something return(true); } } } movingObjSpeed = Vector3.zero; //no none of the floorchecks hit anything, we must be in the air (or water) return(false); }
//returns whether we are on the ground or not //also: bouncing on enemies, keeping player on moving platforms and slope checking private bool IsGrounded() { //get distance to ground, from centre of collider (where floorcheckers should be) float dist = m_Collider.bounds.extents.y; //check whats at players feet, at each floorcheckers position foreach (Transform check in floorCheckers) { RaycastHit hit; if (Physics.Raycast(check.position, Vector3.down, out hit, dist + 0.05f)) { //Collider[] allColliders = hit.transform.GetComponents<Collider>(); //for (int i = 0; i < allColliders.Length; i++) //{ //if (!allColliders[i].isTrigger) if (!hit.collider.isTrigger) { //slope control slope = Vector3.Angle(hit.normal, Vector3.up); //slide down slopes if (slope > m_slopeLimit && !hit.transform.CompareTag(Tags.Pushable)) { Vector3 slide = new Vector3(0f, -m_slideAmount, 0f); PlayerController.RB.AddForce(slide, ForceMode.Force); } //enemy bouncing if (hit.transform.CompareTag(Tags.Enemy) && PlayerController.RB.velocity.y < 0) { enemyAI = hit.transform.GetComponent<EnemyAI>(); enemyAI.BouncedOn(); onEnemyBounce++; dealDamage.Attack(hit.transform.gameObject, 1, 0f, 0f); } if (hit.transform.CompareTag(Tags.Spring) && PlayerController.RB.velocity.y < 0) { JumpSpring jSpring = hit.transform.GetComponent<JumpSpring>(); jSpring.BouncedOn(); } else { onEnemyBounce = 0; } //moving platforms if (hit.transform.CompareTag(Tags.MovingPlatform) || hit.transform.CompareTag(Tags.Pushable)) { movingObjSpeed = hit.rigidbody.velocity; movingObjSpeed.y = 0f; //9.5f is a magic number, if youre not moving properly on platforms, experiment with this number Vector3 newForce = movingObjSpeed * m_movingPlatformFriction * Time.fixedDeltaTime; //Debug.Log(Time.time + " plat " + hit.rigidbody.velocity + " force " + newForce); PlayerController.RB.AddForce(newForce, ForceMode.VelocityChange); } else { movingObjSpeed = Vector3.zero; } // If the player Triggers world events or objects (button pushing, floor buttons, reactions to player etc) if (hit.transform.CompareTag(Tags.Triggerable)) { TriggerableObject triggerable = hit.transform.GetComponent<TriggerableObject>(); if (triggerable != null) { triggerable.StandingOn(transform.position); } } //yes our feet are on something return true; } } //} } movingObjSpeed = Vector3.zero; //no none of the floorchecks hit anything, we must be in the air (or water) return false; }
//returns whether we are on the ground or not //also: bouncing on enemies, keeping player on moving platforms and slope checking private bool IsGrounded() { //get distance to ground, from centre of collider (where floorcheckers should be) float dist = GetComponent <Collider>().bounds.extents.y; //check whats at players feet, at each floorcheckers position int connectingRays = 0; Vector3 totalNormal = Vector3.zero; int loopCount = 0; foreach (Transform check in floorCheckers) { RaycastHit hit; if (Physics.Raycast(check.position, Vector3.down, out hit, dist + 0.05f, mask)) { //Debug.DrawRay(check.position, Vector3.down, Color.green); if (!hit.transform.GetComponent <Collider>().isTrigger) { //slope control //slopeNormal = hit.normal.normalized; totalNormal += hit.normal.normalized;; slope = Vector3.Angle(hit.normal, Vector3.up); //slide down slopes if (slope > slopeLimit && hit.transform.tag != "Pushable") { Vector3 slide = new Vector3(0f, -slideAmount, 0f); rigid.AddForce(slide, ForceMode.Force); } //enemy bouncing if (hit.transform.tag == "Enemy" && rigid.velocity.y < 0) { enemyAI = hit.transform.GetComponent <EnemyAI>(); enemyAI.BouncedOn(); onEnemyBounce++; dealDamage.Attack(hit.transform.gameObject, 1, 0f, 0f); } else { onEnemyBounce = 0; } //moving platforms if (hit.transform.tag == "MovingPlatform" || hit.transform.tag == "Pushable") { movingObjSpeed = hit.transform.GetComponent <Rigidbody>().velocity; movingObjSpeed.y = 0f; //9.5f is a magic number, if youre not moving properly on platforms, experiment with this number rigid.AddForce(movingObjSpeed * movingPlatformFriction * Time.fixedDeltaTime, ForceMode.VelocityChange); } else { movingObjSpeed = Vector3.zero; } //yes our feet are on something //return true; connectingRays++; if (loopCount == 5) { specificSlopeNormal = hit.normal; } } } else { Debug.DrawRay(check.position, Vector3.down, Color.red); } loopCount++; } float nX = Mathf.Abs(totalNormal.x) > 0 ? totalNormal.x / connectingRays : 0; float nY = Mathf.Abs(totalNormal.y) > 0 ? totalNormal.y / connectingRays : 0; float nZ = Mathf.Abs(totalNormal.z) > 0 ? totalNormal.z / connectingRays : 0; slopeNormal = new Vector3(nX, nY, nZ); float sX = Mathf.Abs(specificSlopeNormal.x) > 0 ? specificSlopeNormal.x / 2 : 0; float sY = Mathf.Abs(specificSlopeNormal.y) > 0 ? specificSlopeNormal.y / 2 : 0; float sZ = Mathf.Abs(specificSlopeNormal.z) > 0 ? specificSlopeNormal.z / 2 : 0; // specificSlopeNormal = new Vector3(sX, sY, sZ); movingObjSpeed = Vector3.zero; //no none of the floorchecks hit anything, we must be in the air (or water) if (connectingRays == 0) { return(false); } else { return(true); } }
//returns whether we are on the ground or not //also: bouncing on enemies, keeping player on moving platforms and slope checking private bool IsGrounded() { //get distance to ground, from centre of collider (where floorcheckers should be) float dist = GetComponent<Collider>().bounds.extents.y; //check whats at players feet, at each floorcheckers position foreach (Transform check in floorCheckers) { RaycastHit hit; if(Physics.Raycast(check.position, Vector3.down, out hit, dist + 0.05f)) { //print (hit.transform.tag); if(!hit.transform.GetComponent<Collider>().isTrigger && hit.transform.tag != "Checkpoint"&& hit.transform.tag != "NPC") { //slope control slope = Vector3.Angle (hit.normal, Vector3.up); //slide down slopes if(slope > slopeLimit && hit.transform.tag != "Pushable") { Vector3 slide = new Vector3(0f, -slideAmount, 0f); GetComponent<Rigidbody>().AddForce (slide, ForceMode.Force); } //enemy bouncing if (hit.transform.tag == "Enemy" && GetComponent<Rigidbody>().velocity.y < 0) { enemyAI = hit.transform.GetComponent<EnemyAI>(); enemyAI.BouncedOn(); onEnemyBounce ++; dealDamage.Attack(hit.transform.gameObject, 1, 0f, 0f); } else onEnemyBounce = 0; //moving platforms if (hit.transform.tag == "Pushable" || hit.transform.tag == "Pushable") { movingObjSpeed = hit.transform.GetComponent<Rigidbody>().velocity; print ("moving object speed is " + movingObjSpeed); movingObjSpeed.y = 0f; //9.5f is a magic number, if youre not moving properly on platforms, experiment with this number GetComponent<Rigidbody>().AddForce(movingObjSpeed * movingPlatformFriction * Time.fixedDeltaTime, ForceMode.VelocityChange); } else { movingObjSpeed = Vector3.zero; } if (hit.transform.tag == "LargeFlower" && haveCameraFocus){ camChange.Change(hit.transform.root.GetChild (0).position, false); haveCameraFocus = false; } if (!haveCameraFocus && hit.transform.tag != "LargeFlower"){ camChange.Change (camFocusLocation, true); haveCameraFocus = true; print ("switching back"); } if (hit.transform.tag != "Resetter"){ focusControls.StandingOn(hit.point.y); } hitPoint = hit.normal; //transform.position = new Vector3(transform.position.x, hitPoint.y + hitOffset, transform.position.z); if (standingOnTransform != hit.transform){ standingOnTransform = hit.transform; } //yes our feet are on something return true; } } } movingObjSpeed = Vector3.zero; //no none of the floorchecks hit anything, we must be in the air (or water) return false; }
//returns whether we are on the ground or not //also: bouncing on enemies, keeping player on moving platforms and slope checking private bool IsGrounded() { //get distance to ground, from centre of collider (where floorcheckers should be) float dist = GetComponent<Collider>().bounds.extents.y; //check whats at players feet, at each floorcheckers position foreach (Transform check in floorCheckers) { RaycastHit hit; if(Physics.Raycast(check.position, Vector3.down, out hit, dist + 0.05f)) { if(!hit.transform.GetComponent<Collider>().isTrigger) { //slope control slope = Vector3.Angle (hit.normal, Vector3.up); //slide down slopes if(slope > slopeLimit && hit.transform.tag != "Pushable") { Vector3 slide = new Vector3(0f, -slideAmount, 0f); GetComponent<Rigidbody>().AddForce (slide, ForceMode.Force); } //enemy bouncing if (hit.transform.tag == "Enemy" && GetComponent<Rigidbody>().velocity.y < 0) { enemyAI = hit.transform.GetComponent<EnemyAI>(); enemyAI.BouncedOn(); onEnemyBounce ++; dealDamage.Attack(hit.transform.gameObject, 1, 0f, 0f); } else onEnemyBounce = 0; //moving platforms if (hit.transform.tag == "MovingPlatform" || hit.transform.tag == "Pushable") { movingObjSpeed = hit.transform.GetComponent<Rigidbody>().velocity; movingObjSpeed.y = 0f; //9.5f is a magic number, if youre not moving properly on platforms, experiment with this number GetComponent<Rigidbody>().AddForce(movingObjSpeed * movingPlatformFriction * Time.fixedDeltaTime, ForceMode.VelocityChange); } else { movingObjSpeed = Vector3.zero; } //yes our feet are on something return true; } } } movingObjSpeed = Vector3.zero; //no none of the floorchecks hit anything, we must be in the air (or water) return false; }