/// <summary>Raycasting stuff to align and calculate the ground from the animal ****IMPORTANT***</summary> internal virtual void AlignRayCasting() { MainRay = FrontRay = false; hit_Chest = new RaycastHit(); //Clean the Raycasts every time hit_Hip = new RaycastHit(); //Clean the Raycasts every time hit_Chest.distance = hit_Hip.distance = Height; //Reset the Distances to the Heigth of the animal //var Direction = Gravity; var Direction = -transform.up; if (Physics.Raycast(Main_Pivot_Point, Direction, out hit_Chest, Pivot_Multiplier, GroundLayer, QueryTriggerInteraction.Ignore)) { FrontRay = true; var MainPivotSlope = Vector3.SignedAngle(hit_Chest.normal, UpVector, Right); if (MainPivotSlope > maxAngleSlope) //Means that the Slope is higher thanthe Max slope so stop the animal from Going forward AND ONLY ON LOCOMOTION { if (MovementAxisRaw.z > 0 && !hit_Chest.transform.gameObject.CompareTag("Stair")) { AdditivePosition = Vector3.ProjectOnPlane(AdditivePosition, Forward); //Remove Forward Movement MovementAxis.z = 0; } } // else { if (debugGizmos) { Debug.DrawRay(hit_Chest.point, hit_Chest.normal * ScaleFactor * 0.2f, Color.green); MTools.DrawWireSphere(Main_Pivot_Point + Direction * (hit_Chest.distance - RayCastRadius), Color.green, RayCastRadius * ScaleFactor); } if (platform == null || platform != hit_Chest.transform) //Platforming logic { platform = hit_Chest.transform; platform_Pos = platform.position; platform_Rot = platform.rotation; } hit_Chest.collider.attachedRigidbody?.AddForceAtPosition(Gravity * (RB.mass / 2), hit_Chest.point, ForceMode.Force); } } else { platform = null; } if (Has_Pivot_Hip && Has_Pivot_Chest) //Ray From the Hip to the ground { var hipPoint = Pivot_Hip.World(transform) + DeltaVelocity; if (Physics.Raycast(hipPoint, Direction, out hit_Hip, ScaleFactor * Pivot_Hip.multiplier, GroundLayer, QueryTriggerInteraction.Ignore)) { MainRay = true; if (debugGizmos) { Debug.DrawRay(hit_Hip.point, hit_Hip.normal * ScaleFactor * 0.2f, Color.green); MTools.DrawWireSphere(hipPoint + Direction * (hit_Hip.distance - RayCastRadius), Color.green, RayCastRadius * ScaleFactor); } if (platform == null || platform != hit_Chest.transform) //Platforming logic { platform = hit_Hip.transform; platform_Pos = platform.position; platform_Rot = platform.rotation; } hit_Hip.collider.attachedRigidbody?.AddForceAtPosition(Gravity * (RB.mass / 2), hit_Hip.point, ForceMode.Force); if (!FrontRay) { hit_Chest = hit_Hip; //If there's no Front Ray } } else { platform = null; if (FrontRay) { InertiaPositionSpeed = Forward * ScaleFactor * DeltaTime * 2; //Force going forward in case there's no front Ray (HACK) hit_Hip = hit_Chest; //In case there's no Hip Ray } } } else { MainRay = FrontRay; //Just in case you dont have HIP RAY IMPORTANT FOR HUMANOID CHARACTERSSSSSSSSSSSS hit_Hip = hit_Chest; //In case there's no Hip Ray } if (ground_Changes_Gravity) { Gravity = -hit_Hip.normal; } CalculateSurfaceNormal(); }
/// <summary>Raycasting stuff to align and calculate the ground from the animal ****IMPORTANT***</summary> internal virtual void AlingRayCasting() { Height = (height - RayCastRadius) * ScaleFactor; //multiply the Height by the scale //MainRay = FrontRay = false; hit_Chest = new RaycastHit(); //Clean the Raycasts every time hit_Hip = new RaycastHit(); //Clean the Raycasts every time hit_Chest.distance = hit_Hip.distance = Height; //Reset the Distances to the Heigth of the animal if (Has_Pivot_Hip) //Ray From the Hip to the ground { var hipPoint = Pivot_Hip.World(_transform) + AdditivePosition; // if (Physics.Raycast(Pivot_Hip.World(transform), -_transform.up, out hit_Hip, ScaleFactor * Pivot_Hip.multiplier, GroundLayer, QueryTriggerInteraction.Ignore)) if (Physics.SphereCast(hipPoint, RayCastRadius * ScaleFactor, -_transform.up, out hit_Hip, ScaleFactor * Pivot_Hip.multiplier, GroundLayer, QueryTriggerInteraction.Ignore)) { MainRay = true; if (platform == null || platform != hit_Hip.transform) //Platforming logic { platform = hit_Hip.transform; platform_Pos = platform.position; platform_Rot = platform.rotation; } } else { platform = null; MainRay = false; } } if (Physics.SphereCast(Main_Pivot_Point, RayCastRadius * ScaleFactor, -_transform.up, out hit_Chest, Pivot_Multiplier, GroundLayer, QueryTriggerInteraction.Ignore)) //if (Physics.Raycast(Main_Pivot_Point, -_transform.up, out hit_Chest, Pivot_Multiplier, GroundLayer, QueryTriggerInteraction.Ignore)) { FrontRay = true; if (platform == null || platform != hit_Chest.transform) //Platforming logic { platform = hit_Chest.transform; platform_Pos = platform.position; platform_Rot = platform.rotation; } } else { platform = null; FrontRay = false; hit_Chest = hit_Hip; } if (!Has_Pivot_Hip || !MainRay) { // MainRay = FrontRay; //In case there's no Hip Ray hit_Hip = hit_Chest; } if (ground_Changes_Gravity) { GravityDirection = -hit_Hip.normal; } CalculateSurfaceNormal(); // else if (!Has_Pivot_Chest) FrontRay = MainRay; //In case there's no frontRay }
/// <summary>Raycasting stuff to align and calculate the ground from the animal ****IMPORTANT***</summary> internal virtual void AlingRayCasting() { Height = (height) * ScaleFactor; //multiply the Height by the scale TO properly ALign with the terrain we need to remove the Radius // var LastHitChest = hit_Chest; //Save the last Hit chest hit_Chest = new RaycastHit(); //Clean the Raycasts every time hit_Hip = new RaycastHit(); //Clean the Raycasts every time hit_Chest.distance = hit_Hip.distance = Height; //Reset the Distances to the Heigth of the animal if (Physics.SphereCast(Main_Pivot_Point, RayCastRadius * ScaleFactor, -transform.up, out hit_Chest, Pivot_Multiplier, GroundLayer, QueryTriggerInteraction.Ignore)) //if (Physics.Raycast(Main_Pivot_Point, -_transform.up, out hit_Chest, Pivot_Multiplier, GroundLayer, QueryTriggerInteraction.Ignore)) { FrontRay = true; MainPivotSlope = Vector3.Angle(hit_Chest.normal, UpVector); MainPivotSlope *= Vector3.Dot(Forward_no_Y, hit_Chest.normal) > 0 ? -1 : 1; //if (MainPivotSlope > maxAngleSlope && ActiveStateID.ID == 1 ) //Means that the Slope is higher thanthe Max slope so stop the animal from Going forward AND ONLY ON LOCOMOTION //{ // if (!hit_Chest.transform.CompareTag("Stair")) // { // AdditivePosition = Vector3.ProjectOnPlane(AdditivePosition, Forward); // MovementAxis.z = Mathf.MoveTowards(MovementAxis.z, 0, DeltaTime); // } //} if (debugGizmos) { Debug.DrawRay(hit_Chest.point + AdditivePosition, hit_Chest.normal * ScaleFactor * 0.2f, Color.green); MalbersTools.DebugWireSphere(Main_Pivot_Point + AdditivePosition + -transform.up * hit_Chest.distance, Color.green, RayCastRadius * ScaleFactor); } if (platform == null || platform != hit_Chest.transform) //Platforming logic { platform = hit_Chest.transform; platform_Pos = platform.position; platform_Rot = platform.rotation; } } else { platform = null; FrontRay = false; hit_Chest = hit_Hip; } if (Has_Pivot_Hip && Has_Pivot_Chest) //Ray From the Hip to the ground { var hipPoint = Pivot_Hip.World(transform); // if (Physics.Raycast(hipPoint, -_transform.up, out hit_Hip, ScaleFactor * Pivot_Hip.multiplier, GroundLayer, QueryTriggerInteraction.Ignore)) if (Physics.SphereCast(hipPoint, RayCastRadius * ScaleFactor, -transform.up, out hit_Hip, ScaleFactor * Pivot_Hip.multiplier, GroundLayer, QueryTriggerInteraction.Ignore)) { MainRay = true; if (debugGizmos) { Debug.DrawRay(hit_Hip.point + AdditivePosition, hit_Hip.normal * ScaleFactor * 0.2f, Color.green); MalbersTools.DebugWireSphere(hipPoint + AdditivePosition + -transform.up * hit_Hip.distance, Color.green, RayCastRadius * ScaleFactor); } if (platform == null || platform != hit_Hip.transform) //Platforming logic { platform = hit_Hip.transform; platform_Pos = platform.position; platform_Rot = platform.rotation; } } else { platform = null; MainRay = false; } } if (!Has_Pivot_Hip || !MainRay) { hit_Hip = hit_Chest; //In case there's no Hip Ray } if (ground_Changes_Gravity) { GravityDirection = -hit_Hip.normal; } CalculateSurfaceNormal(); }