slimecing

a fighting game featuring slimes and swords
Log | Files | Refs | README

SlimeMovementController.cs (1998B)


      1 using UnityEngine;
      2 
      3 namespace Slimecing.Characters
      4 {
      5     public class SlimeMovementController : CharacterMovementController
      6     {
      7         [SerializeField] private float speedOfMovementSmoothing;
      8         [SerializeField] private float airControl;
      9         [SerializeField] private float groundControl;
     10         protected override void Move(float deltaTime)
     11         {
     12             if (move == Vector2.zero && rb.velocity == Vector3.zero) return;
     13             
     14             bool grounded = IsGrounded();
     15             
     16             Vector3 moveInputVector = new Vector3(move.x, 0, move.y).normalized;
     17             Vector3 up = PlayerUpVector();
     18             
     19             Vector3 velocity = rb.velocity;
     20             
     21             Vector3 inputRight = Vector3.Cross(moveInputVector, up);
     22             Vector3 reorientedInput = Vector3.Cross(up, inputRight).normalized * moveInputVector.magnitude;
     23             Vector3 targetMovementVelocity = reorientedInput * movementSpeed;
     24             Vector3 smoothedTargetVelocity = Vector3.Lerp(velocity, targetMovementVelocity, 1 - Mathf.Exp(-speedOfMovementSmoothing * deltaTime));
     25             Vector3 localVelocity = transform.InverseTransformDirection(velocity);
     26             Vector3 velocityChange = transform.InverseTransformDirection(smoothedTargetVelocity) - localVelocity;
     27 
     28             velocityChange = Vector3.ClampMagnitude(velocityChange, grounded ? groundControl : airControl);
     29 		    velocityChange = transform.TransformDirection(velocityChange);
     30             rb.AddForce(velocityChange, ForceMode.VelocityChange);
     31             internalTransformPosition += velocityChange * deltaTime;
     32 
     33         }
     34 
     35         protected override void Rotate(float deltaTime)
     36         {
     37             rotatable.RotateToVector(transform, new Vector3(move.x, 0 , move.y), deltaTime);
     38         }
     39 
     40         public override void GetMoveInputH(float h)
     41         {
     42             move.x = h;
     43         }
     44 
     45         public override void GetMoveInputV(float v)
     46         {
     47             move.y = v;
     48         }
     49     }
     50 }