Advertisement
apieceoffruit

Untitled

Oct 15th, 2023
737
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.13 KB | None | 0 0
  1.     public class DefaultCharacterState : CharacterControllerState
  2.     {
  3.         int counter;
  4.         public bool _jumpHeld;
  5.         Vector3 _look;
  6.         public float RotateSharpness = 10f;
  7.        
  8.         public int _usedJumps;
  9.         bool _jumpRequested;
  10.         Vector3 _playerInput;
  11.         Vector3 _cameraForward;
  12.        
  13.         public override void SetInputs(ref CharacterInputs inputs)
  14.         {
  15.             _look = inputs.Look;
  16.             _jumpHeld = inputs.JumpHeld;
  17.             CalculateCameraProjections(ref inputs,out _playerInput,out _cameraForward);
  18.             if(inputs.JumpDown)
  19.                 _jumpRequested = true;
  20.             if (inputs.GrindDown)
  21.                 SetState(GRINDING);
  22.         }
  23.  
  24.         public override void UpdateRotation(ref Quaternion current, float delta) =>
  25.             current = Quaternion.Slerp(current, Quaternion.LookRotation(_cameraForward, Motor.CharacterUp), delta * RotateSharpness);
  26.  
  27.  
  28.  
  29.        
  30.         public override void UpdateVelocity(ref Vector3 currentVelocity, float deltaTime)
  31.         {
  32.             ApplyMovementInput(ref currentVelocity, deltaTime);
  33.             TryToJump(ref currentVelocity);
  34.  
  35.             if (OnTheGround)
  36.                 _usedJumps = 0;
  37.             else
  38.                 ApplyGravity(ref currentVelocity, deltaTime);
  39.         }
  40.        
  41.                 void ApplyGravity(ref Vector3 currentVelocity, float deltaTime)
  42.         {
  43.             var gravityToApply = Settings.Gravity;
  44.             if(OutOfJumps && !_jumpHeld || IsFalling)
  45.                 gravityToApply *= Settings.FallMultiplier;
  46.            
  47.             currentVelocity -= gravityToApply * deltaTime * Motor.CharacterUp;
  48.             currentVelocity *= 1f / (1f + Settings.Drag * deltaTime);
  49.         }
  50.  
  51.  
  52.         Vector3 _inputRelativeToCamera;
  53.         void ApplyMovementInput(ref Vector3 currentVelocity, float deltaTime)
  54.         {
  55.             var moveInputVector =
  56.                 Quaternion.LookRotation(_cameraForward, Motor.CharacterUp) * _playerInput;
  57.  
  58.             _inputRelativeToCamera = moveInputVector;
  59.             Vector3 inputRight = Vector3.Cross(moveInputVector, Motor.CharacterUp);
  60.             Vector3 reorientedInput = Vector3.Cross(Motor.GroundingStatus.GroundNormal, inputRight).normalized *
  61.                                       moveInputVector.magnitude;
  62.             Vector3 targetMovementVelocity = reorientedInput * Settings.Speed;
  63.  
  64.             if (!OnTheGround)
  65.             {
  66.                 Vector3 velocityDiff = Vector3.ProjectOnPlane(targetMovementVelocity - currentVelocity,
  67.                     Motor.CharacterUp);
  68.                 currentVelocity += velocityDiff * (1f - Mathf.Exp(-Settings.MovementSharpness * deltaTime));
  69.                 return;
  70.             }
  71.  
  72.             currentVelocity =
  73.                 Vector3.Lerp(currentVelocity, targetMovementVelocity,
  74.                     1f - Mathf.Exp(-Settings.MovementSharpness * deltaTime));
  75.         }
  76.  
  77.        
  78.         public float AmountOfForwardToAddToJump = 10;
  79.        
  80.         void TryToJump(ref Vector3 currentVelocity)
  81.         {
  82.           if (!_jumpRequested || OutOfJumps) return;
  83.           _jumpRequested = false;
  84.           _usedJumps++;
  85.           Motor.ForceUnground();
  86.             currentVelocity += (Motor.CharacterUp * Settings.JumpHeight) -
  87.                                Vector3.Project(currentVelocity, Motor.CharacterUp);
  88.        
  89.             var currentVelocityIsInOppositeDirectionOfInput =
  90.                 Vector3.Dot(currentVelocity, _inputRelativeToCamera) < 0f;
  91.  
  92.             if (currentVelocityIsInOppositeDirectionOfInput)
  93.             {
  94.                 var yAmount = currentVelocity.y;
  95.                 currentVelocity = -(currentVelocity * 0.5f);
  96.                 currentVelocity.y = yAmount;
  97.             }
  98.  
  99.             currentVelocity += _inputRelativeToCamera * AmountOfForwardToAddToJump;
  100.            
  101.            
  102.            
  103.         }
  104.  
  105.         bool IsFalling => Motor.Velocity.y < -0.001f;
  106.         bool OnTheGround => Motor.GroundingStatus is { IsStableOnGround: true, GroundNormal: { y: > 0.01f } };
  107.         bool OutOfJumps => _usedJumps >= Settings.AllowedJumps-1;
  108.  
  109.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement