public static bool IsJumpEllegible(this MovableEntity entity) { var config = entity.GetMotorConfigAs <TerrestrialMotorConfig>(false); if (config == null) { return(false); } if (entity.CollisionStatus.Down) { return(true); } var bounds = entity.Hitbox.bounds; var min = bounds.min; var max = bounds.max; max.y = min.y; min.x += config.JumpCheckPadding; max.x -= config.JumpCheckPadding; min.y -= config.JumpCheckLength; return(Physics2D.OverlapArea(min, max, GameResources.Instance.WorldMask)); }
public override void OnMove(MovableEntity entity, ref Vector2 velocity) { var animator = entity.AnimatorUpdater; var provider = entity.InputProvider; var hasProvider = provider != null; var xInput = hasProvider ? provider.GetAxis(InputActions.Horizontal) : 0; var inputDir = System.Math.Sign(xInput); var config = entity.GetMotorConfigAs <TerrestrialMotorConfig>(); //entity.Defending = hasProvider && provider.GetDefend(); if (hasProvider && provider.GetButtonDown(InputActions.Attack)) { animator.TriggerDefaultAttack(); } var shouldJump = hasProvider && provider.GetButtonDown(InputActions.Jump); if (shouldJump && entity.IsJumpEllegible()) { //Grounded and should jump velocity.y = config.JumpForce; } var velDir = System.Math.Sign(velocity.x); var curve = config.Acceleration; var speedMultiplier = entity.SpeedMultiplier.Value; var maxSpeed = config.MaxSpeed * speedMultiplier; var speedPercent = entity.SpeedPercentage; var rawAcceleration = curve.Evaluate(speedPercent) * speedMultiplier; var acceleration = rawAcceleration * inputDir; var speed = Mathf.Abs(velocity.x); //Check acceleration if (is stopped or (input is not empty and input is same dir as vel)) if (velDir == 0 || velDir == inputDir && inputDir != 0) { velocity.x += acceleration; } else { var deacceleration = curve.Evaluate(1 - speedPercent); if (Mathf.Abs(xInput) > 0) { //Changing direction, Double deacceleration velocity.x += deacceleration * inputDir * OposingDirectinalForceMultiplier; } else { //Not inputting if (speed < rawAcceleration) { velocity.x = 0; } else { velocity.x += deacceleration * -velDir; } } } velocity.x = Mathf.Clamp(velocity.x, -maxSpeed, maxSpeed); }