/// <summary>─Get jumping on a cliff</summary> void Can_Jump_on_Cliff(float normalizedTime) { if (normalizedTime >= Cliff.minValue && normalizedTime <= Cliff.maxValue) { if (Physics.Raycast(animal.Main_Pivot_Point, -transform.up, out JumpRay, CliffRay * animal.ScaleFactor, animal.GroundLayer)) { if (Vector3.Angle(JumpRay.normal, Vector3.up) < animal.maxAngleSlope) //Jump to a jumpable cliff not an inclined one { if (animal.debug) { Debug.DrawLine(animal.Main_Pivot_Point, JumpRay.point, Color.black); MalbersTools.DebugTriangle(JumpRay.point, 0.1f, Color.black); } animal.SetIntID(110); } } else { if (animal.debug) { Debug.DrawRay(animal.Main_Pivot_Point, -transform.up * CliffRay * animal.ScaleFactor, Color.black); MalbersTools.DebugPlane(animal.Main_Pivot_Point - (transform.up * CliffRay * animal.ScaleFactor), 0.1f, Color.black); } } } }
private void Can_Jump_on_Cliff(float normalizedTime) { if (normalizedTime >= activeJump.CliffTime.minValue && normalizedTime <= activeJump.CliffTime.maxValue) { if (debug) { Debug.DrawRay(animal.Main_Pivot_Point, animal.GravityDirection * activeJump.CliffLandDistance * animal.ScaleFactor, Color.black); } if (Physics.Raycast(animal.Main_Pivot_Point, animal.GravityDirection, out JumpRay, activeJump.CliffLandDistance * animal.ScaleFactor, animal.GroundLayer, QueryTriggerInteraction.Ignore)) { if (debug) { MalbersTools.DebugTriangle(JumpRay.point, 0.1f, Color.black); } var TerrainSlope = Vector3.Angle(JumpRay.normal, animal.UpVector); var DeepSlope = TerrainSlope > animal.maxAngleSlope; if (!DeepSlope) //Jump to a jumpable cliff not an inclined one { if (debug) { Debug.Log("<B>Jump</b> State: Exit on a Cliff "); } animal.Grounded = true; //Force the animal to be grounded so it can find go to Locomotion or IDLE IgnoreLowerStates = false; IsPersistent = false; } } } }
/// <summary>Check if the animal can change to fall state if there's no future ground to land on</summary> private void Check_for_Falling() { IgnoreLowerStates = false; //Means that it will directly to fall OneCastingFall_Ray = true; IsPersistent = false; float RayLength = animal.ScaleFactor * activeJump.JumpLandDistance; //Ray Distance with the Scale Factor var MainPivot = animal.Main_Pivot_Point; var Direction = animal.GravityDirection; if (debug) { Debug.Log("Doing: <b>" + activeJump.name + "</b>. RayLegth:" + RayLength); } if (activeJump.JumpLandDistance > 0) //greater than 0 it can complete the Jump on an even Ground { if (debug) { Debug.DrawRay(MainPivot, Direction * RayLength, Color.red, 0.25f); } if (Physics.Raycast(MainPivot, Direction, out JumpRay, RayLength, animal.GroundLayer, QueryTriggerInteraction.Ignore)) { if (debug) { Debug.Log("Min Distance to complete " + activeJump.name + " " + JumpRay.distance); MalbersTools.DebugTriangle(JumpRay.point, 0.1f, Color.yellow); } var TerrainSlope = Vector3.Angle(JumpRay.normal, animal.UpVector); var DeepSlope = TerrainSlope > animal.maxAngleSlope; if (DeepSlope) //if wefound something but there's a deep slope { if (debug) { Debug.Log("Jump State: Try to Land but the Sloope is too Deep. Exiting Jump State " + TerrainSlope); } IgnoreLowerStates = false; return; } IgnoreLowerStates = true; //Means that it can complete the Jump if (debug) { Debug.Log("Can finish the Jump. Going to Jump End"); } } else { if (debug) { Debug.Log(activeJump.name + ": Go to Fall.. No ground was found"); } IgnoreLowerStates = false; } } }
/// <summary> /// Check if the animal can change to fall state if there's no future ground to land on /// </summary> /// <param name="normalizedTime"></param> void Can_Fall(float normalizedTime) { Debug.DrawRay(animal.Pivot_fall, -animal.transform.up * animal.Pivot_Multiplier * fallRay, Color.red); if (Physics.Raycast(animal.Pivot_fall, -animal.transform.up, out JumpRay, animal.Pivot_Multiplier * fallRay, animal.GroundLayer)) { if ((jumpPoint - JumpRay.point.y) <= (stepHeight * animal.ScaleFactor) && (Vector3.Angle(JumpRay.normal, Vector3.up) < animal.maxAngleSlope)) //If if finding a lower jump point; { animal.SetIntID(0); //Keep the INTID in 0 MalbersTools.DebugTriangle(JumpRay.point, 0.1f, Color.red); } else { if (normalizedTime > willFall) { animal.SetIntID(111); //Set INTID to 111 to activate the FALL transition } MalbersTools.DebugTriangle(JumpRay.point, 0.1f, Color.yellow); } } else { if (normalizedTime > willFall) { animal.SetIntID(111); //Set INTID to 111 to activate the FALL transition } MalbersTools.DebugPlane(animal.Pivot_fall - (animal.transform.up * animal.Pivot_Multiplier * fallRay), 0.1f, Color.red); } }
//private void BlendFallDistance() //{ // if (BlendFall == FallBlending.Distance) // { // var CurrentUpImpulse = MalbersTools.CleanUpVector(animal.Inertia, animal.Forward, animal.Right); //Remove Forward // var UpSpeed = CurrentUpImpulse.magnitude * (Vector3.Dot(CurrentUpImpulse, animal.UpVector) < 0 ? 1f : -1f) / animal.GravityForce; // FallBlend = UpSpeed; // animal.SetFloatID(FallBlend); //Blend between High and Low Fall // } //} public override void TryExitState(float DeltaTime) { // Debug.Log("Try Exit Fall"); FallPoint = animal.Main_Pivot_Point + animal.AdditivePosition; if (animal.debugGizmos && debug) { // MalbersTools.DebugTriangle(animal.Main_Pivot_Point, animal.RayCastRadius * animal.ScaleFactor, Color.magenta); MalbersTools.DebugTriangle(FallPoint, animal.RayCastRadius * animal.ScaleFactor, Color.magenta); Debug.DrawRay(FallPoint, animal.GravityDirection * 100f, Color.magenta); } //int hits = Physics.SphereCastNonAlloc(FallPoint, animal.RayCastRadius * animal.ScaleFactor,animal.GravityDirection, FallHits, 100f, animal.GroundLayer, QueryTriggerInteraction.Ignore); //if (hits > 0) if (Physics.SphereCast(FallPoint, animal.RayCastRadius, animal.GravityDirection, out FallRayCast, 100f, animal.GroundLayer, QueryTriggerInteraction.Ignore)) { DistanceToGround = FallRayCast.distance - (/*animal.AdditivePosition.magnitude +*/ animal.DeltaPos.magnitude); if (animal.debugGizmos && debug) { MalbersTools.DebugTriangle(FallRayCast.point, animal.RayCastRadius * animal.ScaleFactor, Color.magenta); } // if (BlendFall == FallBlending.Normalized) { if (MaxHeight < DistanceToGround) { MaxHeight = DistanceToGround; //get the Highest Distance the first time you touch the ground } FallBlend = Mathf.Lerp(FallBlend, (DistanceToGround - LowerBlendDistance) / (MaxHeight - LowerBlendDistance), DeltaTime * 20); //Small blend in case there's a new ground found animal.SetFloatID(FallBlend); //Blend between High and Low Fall } //Debug.Log(DistanceToGround); if (animal.Height >= DistanceToGround) { var TerrainSlope = Vector3.Angle(FallRayCast.normal, animal.UpVector); var DeepSlope = TerrainSlope > animal.maxAngleSlope; if (!DeepSlope) { animal.AlingRayCasting(); //Check one time the Align Rays to calculate the Angle Slope used on the CanFallOnSlope AllowExit(); animal.Grounded = true; //This Allow Locomotion and Idle to Try Activate themselves animal.UseGravity = false; } } } }
/// <summary>Check if the animal can change to fall state if there's no future ground to land on</summary> private void Check_for_Falling() { IgnoreLowerStates = false; //Means that it will directly to fall OneCastingFall_Ray = true; IsPersistent = false; var scaleFactor = animal.ScaleFactor; if (activeJump.JumpLandDistance > 0) //greater than 0 it can complete the Jump on an even Ground { if (Physics.Raycast(animal.Main_Pivot_Point, animal.GravityDirection, out JumpRay, scaleFactor * activeJump.fallRay, animal.GroundLayer, QueryTriggerInteraction.Ignore)) { if (debug) { Debug.Log("Min Distance to complete " + activeJump.name + " " + JumpRay.distance); // Debug.Log("Current Jump Distance " + activeJump.name +" " + activeJump.JumpLandDistance * scaleFactor); MalbersTools.DebugTriangle(JumpRay.point, 0.1f, Color.yellow); Debug.DrawRay(animal.Main_Pivot_Point, animal.GravityDirection * animal.Pivot_Multiplier * activeJump.fallRay, Color.red); } if ((JumpRay.distance < activeJump.JumpLandDistance * scaleFactor) /* && Angle < animal.maxAngleSlope*/) { var TerrainSlope = Vector3.Angle(JumpRay.normal, animal.UpVector); var DeepSlope = TerrainSlope > animal.maxAngleSlope; if (DeepSlope) //if wefound something but there's a deep slope { if (debug) { Debug.Log("Jump State: Try to Land but the Sloope is too Deep. Exiting Jump State " + TerrainSlope); } IgnoreLowerStates = false; return; } IgnoreLowerStates = true; //Means that it can complete the Jump if (debug) { Debug.Log("Can make the Jump to Jump End"); } return; } } } // Debug.Log("JUMP: Go to the Next Fall State"); }
/// <summary>Check if the animal can change to fall state if there's no future ground to land on</summary> void Can_Fall(float normalizedTime) { Debug.DrawRay(animal.Pivot_fall, -transform.up * animal.Pivot_Multiplier * fallRay, Color.red); if (MinJumpLand > 0) { if (Physics.Raycast(animal.Pivot_fall, -transform.up, out JumpRay, animal.Pivot_Multiplier * fallRay, animal.GroundLayer)) { float distance = Vector3.Distance(animal.Pivot_fall, JumpRay.point); float Angle = Vector3.Angle(Vector3.up, JumpRay.normal); if (animal.debug) { Debug.Log("Min Distance to complete the Jump: " + distance); } if ((MinJumpLand * animal.ScaleFactor) < distance || Angle > animal.maxAngleSlope) { animal.SetIntID(111); MalbersTools.DebugTriangle(JumpRay.point, 0.1f, Color.yellow); } } else { animal.SetIntID(111); } } else if (Physics.Raycast(animal.Pivot_fall, -transform.up, out JumpRay, animal.Pivot_Multiplier * fallRay, animal.GroundLayer)) { if ((jumpPoint - JumpRay.point.y) <= (stepHeight * animal.ScaleFactor) && (Vector3.Angle(JumpRay.normal, Vector3.up) < animal.maxAngleSlope)) //If if finding a lower jump point; { MalbersTools.DebugTriangle(JumpRay.point, 0.1f, Color.red); } else { animal.SetIntID(111); //Set INTID to 111 to activate the FALL transition MalbersTools.DebugTriangle(JumpRay.point, 0.1f, Color.yellow); } } else { animal.SetIntID(111); //Set INTID to 111 to activate the FALL transition MalbersTools.DebugPlane(animal.Pivot_fall - (transform.up * animal.Pivot_Multiplier * fallRay), 0.1f, Color.red); } }
public override void TryExitState(float DeltaTime) { // Debug.Log("Try Exit Fall"); bool GoingDown = Vector3.Dot(animal.DeltaPos, animal.GravityDirection) > 0; //Check if is falling down if (GoingDown) { FallCurrentDistance += Vector3.Project(animal.DeltaPos, animal.GravityDirection).magnitude; } FallPoint = animal.Main_Pivot_Point + animal.AdditivePosition; if (animal.debugGizmos && debug) { // MalbersTools.DebugTriangle(animal.Main_Pivot_Point, animal.RayCastRadius * animal.ScaleFactor, Color.magenta); MalbersTools.DebugTriangle(FallPoint, animal.RayCastRadius * animal.ScaleFactor, Color.magenta); Debug.DrawRay(FallPoint, animal.GravityDirection * 100f, Color.magenta); } //int hits = Physics.SphereCastNonAlloc(FallPoint, animal.RayCastRadius * animal.ScaleFactor,animal.GravityDirection, FallHits, 100f, animal.GroundLayer, QueryTriggerInteraction.Ignore); //if (hits > 0) if (Physics.SphereCast(FallPoint, animal.RayCastRadius, animal.GravityDirection, out FallRayCast, 100f, animal.GroundLayer, QueryTriggerInteraction.Ignore)) { DistanceToGround = FallRayCast.distance - (/*animal.AdditivePosition.magnitude +*/ animal.DeltaPos.magnitude); if (animal.debugGizmos && debug) { MalbersTools.DebugTriangle(FallRayCast.point, animal.RayCastRadius * animal.ScaleFactor, Color.magenta); } switch (BlendFall) { case FallBlending.DistanceNormalized: { if (MaxHeight < DistanceToGround) { MaxHeight = DistanceToGround; //get the Highest Distance the first time you touch the ground } FallBlend = Mathf.Lerp(FallBlend, (DistanceToGround - LowerBlendDistance) / (MaxHeight - LowerBlendDistance), DeltaTime * 20); //Small blend in case there's a new ground found animal.SetFloatID(FallBlend); //Blend between High and Low Fall } break; case FallBlending.Distance: animal.SetFloatID(FallCurrentDistance); break; case FallBlending.VerticalVelocity: var UpInertia = MalbersTools.CleanUpVector(animal.DeltaPos, animal.Forward, animal.Right).magnitude; //Clean the Vector from Forward and Horizontal Influence animal.SetFloatID(UpInertia / animal.DeltaTime * (GoingDown?1:-1)); break; default: break; } //Debug.Log(DistanceToGround); if (animal.Height >= DistanceToGround) { var TerrainSlope = Vector3.Angle(FallRayCast.normal, animal.UpVector); var DeepSlope = TerrainSlope > animal.maxAngleSlope; if (!DeepSlope) { animal.AlingRayCasting(); //Check one time the Align Rays to calculate the Angle Slope used on the CanFallOnSlope AllowExit(); animal.Grounded = true; //This Allow Locomotion and Idle to Try Activate themselves animal.UseGravity = false; } } } }
// OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { bool isInTransition = animator.IsInTransition(layerIndex); if (!isInTransition && JumpMultiplier > 0 && animal.JumpHeightMultiplier > 0) //Add More Height to the Jump { rb.AddForce((Vector3.up * JumpMultiplier * 10) + (animator.transform.forward * ForwardMultiplier * 10), ForceMode.Acceleration); } Debug.DrawRay(animal.Pivot_fall, -animal.transform.up * animal.Chest_Pivot_Multiplier * fallRay, Color.red); //This code is execute when the animal can change to fall state if there's no future ground to land on if (Physics.Raycast(animal.Pivot_fall, -animal.transform.up, out JumpRay, animal.Chest_Pivot_Multiplier * fallRay, animal.GroundLayer)) { if ((jumpPoint - JumpRay.point.y) <= (threshold * animal.ScaleFactor) && (Vector3.Angle(JumpRay.normal, Vector3.up) < animal.maxAngleSlope)) //If if finding a lower jump point; { animal.SetIntID(0); //Keep the INTID in 0 MalbersTools.DebugTriangle(JumpRay.point, 0.1f, Color.red); } else { if (stateInfo.normalizedTime > willFall) { animal.SetIntID(111); //Set INTID to 111 to activate the FALL transition } MalbersTools.DebugTriangle(JumpRay.point, 0.1f, Color.yellow); } } else { if (stateInfo.normalizedTime > willFall) { animal.SetIntID(111); //Set INTID to 111 to activate the FALL transition } MalbersTools.DebugPlane(animal.Pivot_fall - (animal.transform.up * animal.Chest_Pivot_Multiplier * fallRay), 0.1f, Color.red); } //───────────────────────────────────────────────Get jumping on a cliff ──────────────────────────────────────────────────────────────────────────────────────────────────── if (stateInfo.normalizedTime >= startEdge && stateInfo.normalizedTime <= finishEdge) { if (Physics.Raycast(animal.Chest_Pivot_Point, -animal.transform.up, out JumpRay, CliffRay * animal.ScaleFactor, animal.GroundLayer)) { if (Vector3.Angle(JumpRay.normal, Vector3.up) < animal.maxAngleSlope) //Jump to a jumpable cliff not an inclined one { if (animal.debug) { Debug.DrawLine(animal.Chest_Pivot_Point, JumpRay.point, Color.black); MalbersTools.DebugTriangle(JumpRay.point, 0.1f, Color.black); } animal.SetIntID(110); } } else { if (animal.debug) { Debug.DrawRay(animal.Chest_Pivot_Point, -animator.transform.up * CliffRay * animal.ScaleFactor, Color.black); MalbersTools.DebugPlane(animal.Chest_Pivot_Point - (animal.transform.up * CliffRay * animal.ScaleFactor), 0.1f, Color.black); } } } if (animal.forwardJumpControl) //If the jump can be controlled on air { Vector3 pos = animator.transform.position; JumpControl = Vector3.Lerp(pos, pos - new Vector3(animator.velocity.x, 0, animator.velocity.z), Time.deltaTime); if (animal.MovementReleased) { animator.transform.position = Vector3.Lerp(animator.transform.position, JumpControl, smooth); } smooth += (animal.MovementReleased ? Time.deltaTime : -Time.deltaTime) * animal.smoothJumpForward; smooth = Mathf.Clamp01(smooth); } #region if is transitioning to flying //If the next animation is FLY smoothly remove the Y rigidbody speed if (rb && isInTransition && animator.GetNextAnimatorStateInfo(layerIndex).tagHash == Hash.Fly) { float transitionTime = animator.GetAnimatorTransitionInfo(layerIndex).normalizedTime; Vector3 cleanY = rb.velocity; if (Rb_Y_Speed < cleanY.y) { Rb_Y_Speed = cleanY.y; //Get the max Y SPEED } cleanY.y = Mathf.Lerp(Rb_Y_Speed, 0, transitionTime); rb.velocity = cleanY; } #endregion }