Advertisement
NeoGriever

Untitled

Jul 28th, 2021
943
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.05 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(Rigidbody2D))]
  6. [RequireComponent(typeof(BoxCollider2D))]
  7. public class SpielerController : MonoBehaviour {
  8.     [Range(  -50f,   50f)]  public float gravity                                            = 9.81f * 3f;
  9.     [Range(    0f,   20f)]  public float movementSpeed                              = 8.0f;
  10.     [Range(    0f,  100f)]  public float maxSpeed                                           = 10.0f;
  11.     [Range(    0f, 1000f)]  public float fallSpeed                                      = 50.0f;
  12.  
  13.     [Range(    0f,   30f)]  public float jumpForceMultiplier                    = 2.5f;
  14.     [Range( 0.01f, 2.00f)]  public float jumpTimeout                                    = 0.35f;
  15.                                                     private float lastJumpStart                             = -2.0f;
  16.  
  17.     [Range(    0f,    1f)]  public float groundFriction                             = 0.7f;
  18.     [Range(    0f,    1f)]  public float groundCheckRadius                      = 0.15f;
  19.     [Range(     2,    20)]  public int groundRays                                       = 5;
  20.                                                     public LayerMask groundCheckMask                    = new LayerMask();
  21.  
  22.     [Range(    0f,    1f)]  public float airFriction                                    = 0.1f;
  23.     [Range(    0f,    1f)]  public float wallCheckRadius                            = 0.15f;
  24.     [Range(     2,    20)]  public int wallRays                                             = 5;
  25.                                                     public LayerMask wallCheckMask                      = new LayerMask();
  26.  
  27.     [Range(  0.1f,  9.0f)]  public float forceRotationMultiplicator     = 1.5f;
  28.  
  29.     [Range(0.001f,0.100f)]  public float deathZone                                      = 0.002f;
  30.  
  31.     [Range(    0f,    1f)]  public float waterFriction                              = 0.5f;
  32.  
  33.                                                     public SpielerStatus state                              = new SpielerStatus();
  34.  
  35.     public void Update() {
  36.         Rigidbody2D rb = GetComponent<Rigidbody2D>();
  37.         BoxCollider2D bc = GetComponent<BoxCollider2D>();
  38.         startDetections(rb, bc);
  39.         addMovingForce(rb);
  40.         addRotationForce(rb);
  41.         calculateFacingDirection(rb);
  42.     }
  43.  
  44.     public void startDetections(Rigidbody2D rb, BoxCollider2D bc) {
  45.         groundCheck(rb, bc);
  46.         wallCheck(rb, bc);
  47.     }
  48.  
  49.     public void wallCheck(Rigidbody2D rb, BoxCollider2D bc) {
  50.         float h = (bc.size.y * bc.transform.localScale.y) * 0.955f;
  51.         float checkStep = h / (float)(wallRays - 1);
  52.         List<RaycastHit2D> raysLeft = new List<RaycastHit2D>();
  53.         List<RaycastHit2D> raysRight = new List<RaycastHit2D>();
  54.         for(int i = 0;i < wallRays;i++) {
  55.             Vector2 startPointL = (transform.rotation * new Vector2(0f - (bc.size.x * bc.transform.localScale.x) / 2f, (checkStep * (float)i) - (h / 2f))) + new Vector3(bc.transform.position.x - 0.01f, bc.transform.position.y, 0f);
  56.             Vector2 startPointR = (transform.rotation * new Vector2(0f + (bc.size.x * bc.transform.localScale.x) / 2f, (checkStep * (float)i) - (h / 2f))) + new Vector3(bc.transform.position.x + 0.01f, bc.transform.position.y, 0f);
  57.             raysLeft.Add(Physics2D.Raycast(startPointL, transform.rotation * -Vector2.right, wallCheckRadius, wallCheckMask));
  58.             raysRight.Add(Physics2D.Raycast(startPointR, transform.rotation *  Vector2.right, wallCheckRadius, wallCheckMask));
  59.             Debug.DrawRay(startPointL, (transform.rotation * -Vector2.right) * wallCheckRadius, Color.blue);
  60.             Debug.DrawRay(startPointR, (transform.rotation *  Vector2.right) * wallCheckRadius, Color.blue);
  61.         }
  62.         int hitCountL = 0;
  63.         int hitCountR = 0;
  64.         foreach(RaycastHit2D hit in raysLeft) {
  65.             if(hit.collider != null) {
  66.                 hitCountL += 1;
  67.             }
  68.         }
  69.         foreach(RaycastHit2D hit in raysRight) {
  70.             if(hit.collider != null) {
  71.                 hitCountR += 1;
  72.             }
  73.         }
  74.  
  75.         state.wallLeft = (hitCountL > 0);
  76.         state.wallRight = (hitCountR > 0);
  77.         if(state.wallLeft) {
  78.             if(!state.wasWallLeft) {
  79.                 // HitWallLeft
  80.                 // HitWall
  81.             }
  82.         } else {
  83.             if(state.wasWallLeft) {
  84.                 // LeaveWallLeft
  85.                 // LeaveWall
  86.             }
  87.         }
  88.         if(state.wallRight) {
  89.             if(!state.wasWallRight) {
  90.                 // HitWallRight
  91.                 // HitWall
  92.             }
  93.         } else {
  94.             if(state.wasWallRight) {
  95.                 // LeaveWallRight
  96.                 // LeaveWall
  97.             }
  98.         }
  99.         state.wasWallLeft = state.wallLeft;
  100.         state.wasWallRight = state.wallRight;
  101.     }
  102.  
  103.     public void groundCheck(Rigidbody2D rb, BoxCollider2D bc) {
  104.         float w = (bc.size.x * bc.transform.localScale.x) * 0.9985f;
  105.         float checkStep = w / (float)(groundRays - 1);
  106.         List<RaycastHit2D> rays = new List<RaycastHit2D>();
  107.         for(int i = 0;i < groundRays;i++) {
  108.             Vector2 startPoint = (transform.rotation * new Vector2((checkStep * (float)i) - (w / 2f), 0f - (bc.size.y * bc.transform.localScale.y) / 2f)) + new Vector3(bc.transform.position.x, bc.transform.position.y - 0.01f, 0f);
  109.             rays.Add(Physics2D.Raycast(startPoint, transform.rotation * -Vector2.up, groundCheckRadius, groundCheckMask));
  110.             Debug.DrawRay(startPoint, (transform.rotation * -Vector2.up) * groundCheckRadius, Color.red);
  111.         }
  112.         int hitCount = 0;
  113.         foreach(RaycastHit2D hit in rays) {
  114.             if(hit.collider != null) {
  115.                 hitCount += 1;
  116.             }
  117.         }
  118.  
  119.         state.grounded = (hitCount > 0);
  120.         if(state.grounded) {
  121.             if(!state.wasGrounded) {
  122.                 state.jumped = false;
  123.             }
  124.         } else {
  125.             if(state.wasGrounded) {
  126.                 // LeaveGround
  127.             }
  128.         }
  129.         state.wasGrounded = (hitCount > 0);
  130.     }
  131.  
  132.     private Vector2 injectInputs(Rigidbody2D rb, Vector2 direction) {
  133.         float vl = rb.velocity.x;
  134.         float vlAbs = Mathf.Abs(vl);
  135.  
  136.         int hRaw = (int)Mathf.Round(Input.GetAxisRaw("Horizontal"));
  137.         int vRaw = (int)Mathf.Round(Input.GetAxisRaw("Vertical"));
  138.  
  139.         direction.x += hRaw * movementSpeed;
  140.  
  141.         if(vRaw > 0 && state.grounded && !state.jumped && Time.time - lastJumpStart >= jumpTimeout) {
  142.             lastJumpStart = Time.time;
  143.             direction.y = (jumpForceMultiplier * 5f) * gravity;
  144.         }
  145.         return(direction);
  146.     }
  147.  
  148.     private void clampVelocity(Rigidbody2D rb, Vector2 direction) {
  149.         if(state.grounded) {
  150.             if(Mathf.Abs(direction.x) == 0f) {
  151.                 rb.velocity = new Vector2(rb.velocity.x * (1f - groundFriction), rb.velocity.y);
  152.             }
  153.         }
  154.         rb.velocity = new Vector2(Mathf.Clamp(rb.velocity.x, -movementSpeed, movementSpeed), Mathf.Min(fallSpeed, rb.velocity.y));
  155.     }
  156.  
  157.     private void addMovingForce(Rigidbody2D rb) {
  158.         Vector2 direction = new Vector2(0f, 0f);
  159.         direction = injectInputs(rb, direction);
  160.         rb.AddForce(direction);
  161.         rb.AddForce(new Vector2(0f, -gravity));
  162.         clampVelocity(rb, direction);
  163.     }
  164.  
  165.     private void addRotationForce(Rigidbody2D rb) {
  166.         Vector3 ea = rb.transform.rotation.eulerAngles;
  167.         float posRot = Mathf.Round(ea.z * 100f) / 100f;
  168.         float negRot = (0f - (180f + (360f - posRot))) + 180f;
  169.         float rot = posRot;
  170.         if(posRot > 180f) {
  171.             rot = negRot;
  172.         }
  173.         float rotAbs = Mathf.Abs(rot) / 180f;
  174.         float rotForce = -360f * (rot / 360f) * Time.deltaTime * (forceRotationMultiplicator * (0.2f + (rotAbs * 0.8f)));
  175.         rb.AddTorque(rotForce, ForceMode2D.Force);
  176.     }
  177.  
  178.     private void calculateFacingDirection(Rigidbody2D rb) {
  179.         float vl = rb.velocity.x;
  180.         int movingDirection = 0;
  181.         int velocityDirection = 0;
  182.         int hRaw = (int)Mathf.Round(Input.GetAxisRaw("Horizontal"));
  183.         if(hRaw < 0) {
  184.             movingDirection -= 1;
  185.         }
  186.         if(hRaw > 0) {
  187.             movingDirection += 1;
  188.         }
  189.         if(Mathf.Abs(rb.velocity.x) > deathZone) {
  190.             velocityDirection = 1;
  191.             if(rb.velocity.x < 0) {
  192.                 velocityDirection = -1;
  193.             }
  194.         }
  195.         state.facingLeft = movingDirection == -1;
  196.         state.facingRight = movingDirection == 1;
  197.         state.movingLeft = velocityDirection == -1;
  198.         state.movingRight = velocityDirection == 1;
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement