Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.88 KB | None | 0 0
  1. public class PlayerControler : MonoBehaviour {
  2.  
  3.     public GameObject _AnimationBody;
  4.  
  5.  
  6.    
  7.  
  8.     float ForwardVelocity = 0;
  9.     float HorizontalVelocity = 0;
  10.     Vector3 AirVelocity = new Vector3();
  11.  
  12.     Vector3 Velocity
  13.     {
  14.         get
  15.         {
  16.             return transform.TransformDirection(HorizontalVelocity, 0, HorizontalVelocity) + AirVelocity;
  17.         }
  18.         set
  19.         {
  20.             Vector3 V = value;
  21.             V = Vector3.ProjectOnPlane(V, transform.up);
  22.             ForwardVelocity = transform.InverseTransformDirection(V).z;
  23.             HorizontalVelocity = transform.InverseTransformDirection(V).x;
  24.             AirVelocity = value - GroundVelocity;
  25.         }
  26.     }
  27.  
  28.     Vector3 LocalVelocity
  29.     {
  30.         get
  31.         {
  32.             return transform.InverseTransformDirection(AirVelocity) + new Vector3(HorizontalVelocity, 0, ForwardVelocity);
  33.         }
  34.         set
  35.         {
  36.             Vector3 V = transform.TransformDirection(value);
  37.             V = Vector3.ProjectOnPlane(V, transform.up);
  38.             ForwardVelocity = transform.InverseTransformDirection(V).z;
  39.             HorizontalVelocity = transform.InverseTransformDirection(V).x;
  40.             AirVelocity = value - GroundVelocity;
  41.         }
  42.     }
  43.  
  44.     Vector3 GroundVelocity
  45.     {
  46.         get
  47.         {
  48.             return transform.TransformDirection(HorizontalVelocity, 0, HorizontalVelocity);
  49.         }
  50.         set
  51.         {
  52.  
  53.             Vector3 V = Vector3.ProjectOnPlane(value, transform.up);
  54.             ForwardVelocity = transform.InverseTransformDirection(V).z;
  55.             HorizontalVelocity = transform.InverseTransformDirection(V).x;
  56.         }
  57.     }
  58.  
  59.     Vector3 LocalGroundVelocity
  60.     {
  61.         get
  62.         {
  63.             return new Vector3(HorizontalVelocity, 0, HorizontalVelocity);
  64.         }
  65.         set
  66.         {
  67.             Vector3 V = transform.TransformDirection(value);
  68.             ForwardVelocity = transform.InverseTransformDirection(Vector3.Project(V, transform.forward)).z;
  69.             HorizontalVelocity = transform.InverseTransformDirection(Vector3.Project(V, transform.right)).x;
  70.         }
  71.     }
  72.  
  73.     Trans _CameraTransformDuplicate;
  74.  
  75.     Vector3 _MoveInput = new Vector3();
  76.     Vector3 _RawInput = new Vector3();
  77.  
  78.     public Vector3 _Gravity = new Vector3(0, -9.81f, 0);
  79.  
  80.     float InitialInputMag;
  81.     float InitialLerpedInput;
  82.  
  83.     public float GroundRaycastLengthMin = 0.5f;
  84.     public float GroundRaycastLengthMax = 2f;
  85.  
  86.     public float Height = 0.25f;
  87.  
  88.     public float MaximumSpeed = 40;
  89.     public float Acell = 0.6f;
  90.     public float AirAcell = 1.2f;
  91.     public float Decel = 0.4f;
  92.  
  93.     public bool UtopiaTurning = false;
  94.     public bool _Grounded;
  95.  
  96.     Rigidbody RB;
  97.  
  98.  
  99.  
  100.     public enum PlayerState
  101.     {
  102.         Grounded,
  103.         OnWall,
  104.         InAir,
  105.         WallRun
  106.     }
  107.  
  108.     public PlayerState _PlayerState = PlayerState.Grounded;
  109.  
  110.     private void Start()
  111.     {
  112.         RB = GetComponent<Rigidbody>();
  113.        
  114.         _CameraTransformDuplicate = new Trans(Camera.main.transform);
  115.     }
  116.  
  117.     private void Update()
  118.     {
  119.         _CameraTransformDuplicate = new Trans(Camera.main.transform);
  120.  
  121.         //CameraControl(Time.deltaTime);
  122.  
  123.         // Get curve position
  124.  
  125.  
  126.         Debug.DrawRay(transform.position, Velocity, Color.red, Velocity.magnitude);
  127.  
  128.         // Get the axis and jump input.
  129.  
  130.         float h = InputManager.LeftStickX; //Input.GetAxis("Horizontal");
  131.         float v = InputManager.LeftStickY; //Input.GetAxis("Vertical");
  132.  
  133.         if (InputManager.IsPressed("Up"))
  134.         {
  135.             v = 1;
  136.         }
  137.  
  138.         if (InputManager.IsPressed("Down"))
  139.         {
  140.             v = -1;
  141.         }
  142.  
  143.         if (InputManager.IsPressed("Right"))
  144.         {
  145.             h = 1;
  146.         }
  147.  
  148.         if (InputManager.IsPressed("Left"))
  149.         {
  150.             h = -1;
  151.         }
  152.  
  153.         // calculate move direction
  154.         Vector3 moveInp = new Vector3(h, 0, v);
  155.         InitialInputMag = moveInp.sqrMagnitude;
  156.         InitialLerpedInput = Mathf.Lerp(InitialLerpedInput, InitialInputMag, Time.deltaTime);
  157.        
  158.         Vector3 transformedInput = Quaternion.FromToRotation(_CameraTransformDuplicate.Up, transform.up) * (_CameraTransformDuplicate.rotation * moveInp);
  159.         transformedInput = transform.InverseTransformDirection(transformedInput);
  160.         transformedInput.y = 0.0f;
  161.         _RawInput = transformedInput;
  162.         moveInp = transformedInput;
  163.        
  164.         _MoveInput = moveInp;
  165.        
  166.         if (InputManager.IsJustPressed("Jump")  &&  _Grounded)
  167.         {
  168.  
  169.            
  170.         }else if (InputManager.PressedElapsedTime("Jump") < 1)
  171.         {
  172.            
  173.            
  174.            
  175.         }
  176.         if (InputManager.IsJustReleased("Jump"))
  177.         {
  178.            
  179.         }
  180.     }
  181.  
  182.  
  183.  
  184.     private void FixedUpdate()
  185.     {
  186.         Velocity = RB.velocity;
  187.         GeneralPhysics();
  188.         RB.velocity = Velocity;
  189.         DebugText._DebugText._Velocity = Velocity;
  190.     }
  191.  
  192.     void GeneralPhysics()
  193.     {
  194.         Velocity = Velocity + _Gravity * Time.fixedDeltaTime;
  195.         RaycastHit _Hit;
  196.         if (_Grounded)
  197.         {
  198.             if (Physics.Raycast(transform.position, -transform.up, out _Hit, Mathf.Lerp(GroundRaycastLengthMin, GroundRaycastLengthMax, GroundVelocity.magnitude/MaximumSpeed)))
  199.             {
  200.                 _Grounded = true;
  201.                 transform.position = _Hit.point + _Hit.normal * Height;
  202.  
  203.                 Vector3 F1 = Vector3.Cross(Vector3.up, -Vector3.Cross(Vector3.up, _CameraTransformDuplicate.Forward));
  204.                 Vector3 F2 = Vector3.Cross(_Hit.normal, -Vector3.Cross(_Hit.normal, _CameraTransformDuplicate.Forward));
  205.  
  206.                 Quaternion Q1 = Quaternion.LookRotation(F1, Vector3.up);
  207.                 Quaternion Q2 = Quaternion.LookRotation(F2, _Hit.normal);
  208.  
  209.                 Rotate(Quaternion.Lerp(Q1, Q2, GroundVelocity.magnitude / MaximumSpeed), 0);
  210.             }
  211.             else
  212.             {
  213.                 _Grounded = false;
  214.                 Vector3 F1 = Vector3.Cross(Vector3.up, -Vector3.Cross(Vector3.up, _CameraTransformDuplicate.Forward));
  215.                 Quaternion Q1 = Quaternion.LookRotation(F1, Vector3.up);
  216.                 Rotate(Q1, 0);
  217.             }
  218.         }
  219.         else
  220.         {
  221.             if (Physics.Raycast(transform.position, -transform.up, out _Hit, GroundRaycastLengthMin))
  222.             {
  223.                 _Grounded = true;
  224.                 transform.position = _Hit.point + _Hit.normal * Height;
  225.  
  226.                 Vector3 F1 = Vector3.Cross(Vector3.up, -Vector3.Cross(Vector3.up, _CameraTransformDuplicate.Forward));
  227.                 Vector3 F2 = Vector3.Cross(_Hit.normal, -Vector3.Cross(_Hit.normal, _CameraTransformDuplicate.Forward));
  228.  
  229.                 Quaternion Q1 = Quaternion.LookRotation(F1, Vector3.up);
  230.                 Quaternion Q2 = Quaternion.LookRotation(F2, _Hit.normal);
  231.  
  232.                 Rotate(Quaternion.Lerp(Q1, Q2, GroundVelocity.magnitude / MaximumSpeed), 0);
  233.             }
  234.             else
  235.             {
  236.                 _Grounded = false;
  237.                 Vector3 F1 = Vector3.Cross(Vector3.up, -Vector3.Cross(Vector3.up, _CameraTransformDuplicate.Forward));
  238.                 Quaternion Q1 = Quaternion.LookRotation(F1, Vector3.up);
  239.                 Rotate(Q1, 0);
  240.             }
  241.         }
  242.  
  243.         Vector3 GV = GroundVelocity;
  244.  
  245.         if (_MoveInput == Vector3.zero)
  246.         {
  247.             if (_Grounded)
  248.             {
  249.                 float Mag = GV.magnitude;
  250.                 Mag = Mathf.Clamp(Mag - Decel * Time.fixedDeltaTime, 0, MaximumSpeed);
  251.             }
  252.         }
  253.         else
  254.         {
  255.             if (_Grounded)
  256.             {
  257.                 float Mag = GV.magnitude;
  258.                 Mag = Mathf.Clamp(Mag + Acell * Time.fixedDeltaTime, 0, MaximumSpeed);
  259.                 GV = _MoveInput * Mag;
  260.             }
  261.             else
  262.             {
  263.                 float Mag = GV.magnitude;
  264.                 Mag = Mathf.Clamp(Mag + AirAcell * Time.fixedDeltaTime, 0, MaximumSpeed);
  265.                 GV = _MoveInput * Mag;
  266.             }
  267.         }
  268.         LocalGroundVelocity = GV;
  269.        
  270.         Vector3 NextPoint = transform.position + Velocity * Time.fixedDeltaTime;
  271.  
  272.         if (_Grounded)
  273.         {
  274.             if (Physics.Raycast(NextPoint, -transform.up, out _Hit, Mathf.Lerp(GroundRaycastLengthMin, GroundRaycastLengthMax, GroundVelocity.magnitude / MaximumSpeed)))
  275.             {
  276.                 Velocity += DownForce(_Hit);
  277.             }
  278.         }
  279.     }
  280.    
  281.     void Rotate(Quaternion Rotation, float PreserveVAmount)
  282.     {
  283.         Vector3 V = Velocity;
  284.         transform.rotation = Rotation;
  285.         Vector3 V2 = Velocity;
  286.  
  287.         Velocity = Vector3.Lerp(V, V2, PreserveVAmount);
  288.     }
  289.    
  290.     Vector3 DownForce(RaycastHit _Hit)
  291.     {
  292.         Vector3 DF = -_Gravity;
  293.  
  294.         DF = _Hit.distance * -_Hit.normal;
  295.  
  296.         return DF;
  297.     }
  298.    
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement