Advertisement
NeoGriever

Untitled

Jul 12th, 2020
1,183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.55 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class playerController : MonoBehaviour {
  6.   private Rigidbody2D rb;
  7.   private float moveInput;
  8.   private bool jumpInput;
  9.   private bool sneakInput;
  10.   private float lastJump = -10f;
  11.   private float jumpTreshold = 1.5f;
  12.   private float lastFallSpeed = 0f;
  13.  
  14.   [HideInInspector]
  15.   public bool isFacingRight = true;
  16.  
  17.   [HideInInspector]
  18.   public additionalForcesHandler AdditionalForces = new additionalForcesHandler(0f, 0f);
  19.   private Vector2 raisedForce = new Vector2(0f, 0f);
  20.  
  21.   private bool isGrounded = true;
  22.   private bool isWalled = false;
  23.  
  24.   private int extraJumpsValue;
  25.   private float coyoteTime = 0f;
  26.   private float wallCoyoteTime = 0f;
  27.  
  28.   [Header("Movement")]
  29.   [Range(0.0f, 50.0f)]
  30.   public float speed = 18.0f;
  31.  
  32.   [Range(0.000f, 1.000f)]
  33.   public float sneakSpeedModifer = 0.225f;
  34.  
  35.   [Header("Coyote-Time")]
  36.   [Range(0.0001f, 1.0000f)]
  37.   public float coyoteTimeTreshold = 0.15f;
  38.   [Range(0.0001f, 1.0000f)]
  39.   public float wallCoyoteTimeTreshold = 0.25f;
  40.  
  41.   [Header("Sprung-Einstellungen")]
  42.   public GameObject dblJumpPlatform;
  43.   [Range(0.000f, 1.000f)]
  44.   public float groundCheckRadius = 0.5f;
  45.   [Range(0.0f, 50.0f)]
  46.   public float jumpForce = 20.0f;
  47.   [Range(0.000f, 1.000f)]
  48.   public float groundFriction = 0.15f;
  49.   [Range(0.000f, 1.000f)]
  50.   public float groundStopFriction = 0.02f;
  51.   [Range(0.000f, 1.000f)]
  52.   public float airFriction = 0.1f;
  53.  
  54.   [Header("Wall-Jump/Slide")]
  55.   [Range(0.000f, 1.000f)]
  56.   public float wallCheckRadius = 0.5f;
  57.   [Range(0.00f, 10.00f)]
  58.   public float maxSlideSpeed = 0.2f;
  59.   [Range(0.00f, 1.00f)]
  60.   public float wallJumpAspect = 0.8f;
  61.  
  62.   [Header("Ebenen")]
  63.   public LayerMask whatIsGround;
  64.   public LayerMask whatIsWall;
  65.  
  66.   [Header("Sound")]
  67.   public AudioClip[] sfxHitGround;
  68.  
  69.   [Range(0.00f, 0.35f)]
  70.   public float hitGroundTreshold = 0.05f;
  71.   private float lastHitGroundTreshold = -0.05f;
  72.  
  73.   public AudioClip[] sfxMoveStart;
  74.  
  75.   [Range(0.00f, 1.50f)]
  76.   public float sfxMoveStartTreshold = 0.85f;
  77.   private float lastSfxMoveStartTreshold = -0.85f;
  78.  
  79.   public AudioClip sfxJumpGround;
  80.   public AudioClip sfxJumpDbl;
  81.   public AudioClip sfxJumpWall;
  82.  
  83.   private Vector3 initialPosition;
  84.  
  85.   void Start() {
  86.     initialPosition = transform.position;
  87.     extraJumpsValue = gameStatics.extraJumps;
  88.     rb = GetComponent<Rigidbody2D> ();
  89.   }
  90.  
  91.   private float lastMoveInput;
  92.   void Update() {
  93.     if (gameStatics.allowPlayerInput) {
  94.       lastMoveInput = (lastMoveInput != moveInput)?moveInput:lastMoveInput;
  95.       moveInput = Input.GetAxisRaw("Horizontal");
  96.       jumpInput = Input.GetButton("Jump");
  97.       sneakInput = Input.GetButton("Fire3");
  98.  
  99.       if (jumpInput && gameStatics.selectedSkin != null && gameStatics.playerObject.transform.Find("PlayerVisual").GetComponent<SpriteRenderer> ().sprite != gameStatics.selectedSkin) {
  100.         gameStatics.playerObject.transform.Find("PlayerVisual").GetComponent<SpriteRenderer> ().sprite = gameStatics.selectedSkin;
  101.         gameStatics.selectedSkin = null;
  102.         lastJump = Time.fixedTime;
  103.       }
  104.     } else {
  105.       lastMoveInput = (lastMoveInput != moveInput)?moveInput:lastMoveInput;
  106.       moveInput = 0;
  107.       jumpInput = false;
  108.     }
  109.  
  110.     bool beforeGrounded = isGrounded;
  111.     bool beforeWalled = isWalled;
  112.  
  113.     if (!jumpInput) {
  114.       lastJump = -1f;
  115.     }
  116.  
  117.     if (moveInput < 0) {
  118.       isFacingRight = false;
  119.     } else if (moveInput > 0) {
  120.       isFacingRight = true;
  121.     }
  122.  
  123.     bool wasGrounded = isGrounded;
  124.  
  125.     RaycastHit2D hit1 = Physics2D.Raycast(new Vector2(transform.position.x + (-transform.localScale.x * 0.48f), transform.position.y + (-transform.localScale.y * 0.48f)), -Vector2.up, 10f, whatIsGround);
  126.     RaycastHit2D hit2 = Physics2D.Raycast(new Vector2(transform.position.x + ( transform.localScale.x * 0.48f), transform.position.y + (-transform.localScale.y * 0.48f)), -Vector2.up, 10f, whatIsGround);
  127.     isGrounded = (Mathf.Min((hit1.collider != null)?hit1.distance:Mathf.Infinity, (hit2.collider != null)?hit2.distance:Mathf.Infinity) <= groundCheckRadius);
  128.  
  129.     RaycastHit2D hit_left_1 = Physics2D.Raycast(new Vector2(transform.position.x + (-transform.localScale.x * 0.48f), transform.position.y + (-transform.localScale.y * 0.25f)), Vector2.left , 10f, whatIsWall);
  130.     RaycastHit2D hit_right1 = Physics2D.Raycast(new Vector2(transform.position.x + ( transform.localScale.x * 0.48f), transform.position.y + (-transform.localScale.y * 0.25f)), Vector2.right, 10f, whatIsWall);
  131.     isWalled = gameStatics.canDoWallJump && (
  132.       Mathf.Min(
  133.         (hit_left_1.collider != null && !isFacingRight)?hit_left_1.distance:Mathf.Infinity,
  134.         (hit_right1.collider != null && isFacingRight)?hit_right1.distance:Mathf.Infinity
  135.       ) <= wallCheckRadius
  136.     );
  137.  
  138.     if (isGrounded) {
  139.       isWalled = false;
  140.       coyoteTime = -(coyoteTimeTreshold + 1f);
  141.       wallCoyoteTime = -(wallCoyoteTimeTreshold + 1f);
  142.       extraJumpsValue = gameStatics.extraJumps;
  143.     } else if (beforeGrounded && !jumpInput) {
  144.       coyoteTime = Time.fixedTime;
  145.     } else if (beforeGrounded && jumpInput) {
  146.       coyoteTime = -(coyoteTimeTreshold + 1f);
  147.     }
  148.  
  149.     if (!isGrounded && !isWalled && beforeWalled && !jumpInput) {
  150.       wallCoyoteTime = Time.fixedTime;
  151.     } else if (isWalled || (beforeWalled && jumpInput)) {
  152.       wallCoyoteTime = -(wallCoyoteTimeTreshold + 1f);
  153.     }
  154.  
  155.     if ((isGrounded && !beforeGrounded) || (isWalled && !beforeWalled && !beforeGrounded && gameStatics.canDoWallJump)) {
  156.       if (Time.unscaledTime - lastHitGroundTreshold > hitGroundTreshold) {
  157.         lastHitGroundTreshold = Time.unscaledTime;
  158.         AudioClip groundSelectedSound = sfxHitGround[Random.Range(0, sfxHitGround.Length)];
  159.         gameStatics.Sfx(groundSelectedSound, Random.Range(0.12f, 0.20f));
  160.       }
  161.       lastFallSpeed = 0f;
  162.     }
  163.     if(isGrounded && moveInput != 0) {
  164.       if (Time.unscaledTime - lastSfxMoveStartTreshold > sfxMoveStartTreshold) {
  165.         lastSfxMoveStartTreshold = Time.unscaledTime;
  166.         AudioClip sfxMoveStartSound = sfxMoveStart[Random.Range(0, sfxMoveStart.Length)];
  167.         gameStatics.Sfx(sfxMoveStartSound, Random.Range(0.08f, 0.25f));
  168.       }
  169.     }
  170.   }
  171.  
  172.   void FixedUpdate() {
  173.     Vector2 desiredMovement = rb.velocity;
  174.  
  175.     float triggeredSpeed = (sneakInput)?sneakSpeedModifer:1f;
  176.  
  177.     float calculatedXSpeed = Mathf.Lerp(rb.velocity.x, (speed*triggeredSpeed) * moveInput, (isGrounded)?((moveInput==0)?groundStopFriction:groundFriction):airFriction);
  178.     float calculatedYSpeed = (isWalled && gameStatics.canDoWallJump)?Mathf.Max(-maxSlideSpeed, rb.velocity.y) : rb.velocity.y;
  179.  
  180.     bool coyoteTimeN = (Time.fixedTime - coyoteTime < coyoteTimeTreshold);
  181.     bool coyoteTimeW = (Time.fixedTime - wallCoyoteTime < wallCoyoteTimeTreshold);
  182.     bool canNJump = coyoteTimeN || (isGrounded || (extraJumpsValue > 0 && !isWalled && !coyoteTimeW));
  183.     bool canWJump = (coyoteTimeW || isWalled) && gameStatics.canDoWallJump;
  184.  
  185.     if (jumpInput && Time.fixedTime - lastJump > jumpTreshold) {
  186.       if (canNJump && !canWJump) {
  187.         bool dblJump = false;
  188.         if (!isGrounded && extraJumpsValue > 0) {
  189.           extraJumpsValue--;
  190.           dblJump = true;
  191.         }
  192.         if (canNJump || dblJump) {
  193.           lastJump = Time.fixedTime;
  194.           calculatedYSpeed = (jumpForce*triggeredSpeed);
  195.           coyoteTime = -(coyoteTimeTreshold + 1f);
  196.           wallCoyoteTime = -(wallCoyoteTimeTreshold + 1f);
  197.           canNJump = (extraJumpsValue > 0);
  198.           if (!dblJump) {
  199.             gameStatics.Sfx(sfxJumpGround);
  200.           } else {
  201.             Instantiate(dblJumpPlatform, transform.position, transform.rotation);
  202.             gameStatics.Sfx(sfxJumpDbl);
  203.           }
  204.         }
  205.       } else if (!canNJump && canWJump) {
  206.         lastJump = Time.fixedTime;
  207.         calculatedYSpeed = (jumpForce*triggeredSpeed);
  208.         calculatedXSpeed = (isFacingRight)?-(jumpForce*triggeredSpeed)*wallJumpAspect:(jumpForce*triggeredSpeed)*wallJumpAspect;
  209.         calculatedXSpeed = (coyoteTimeW)?-calculatedXSpeed:calculatedXSpeed;
  210.         isFacingRight = (coyoteTimeW)?isFacingRight:!isFacingRight;
  211.         coyoteTime = -(coyoteTimeTreshold + 1f);
  212.         wallCoyoteTime = -(wallCoyoteTimeTreshold + 1f);
  213.         gameStatics.Sfx(sfxJumpWall);
  214.       }
  215.     }
  216.     Vector2 taf = AdditionalForces.toVector2(sneakInput);
  217.     raisedForce = Vector2.Lerp(raisedForce, taf, (taf == new Vector2(0f, 0f))?0.65f:0.35f);
  218.     desiredMovement = new Vector2(calculatedXSpeed, calculatedYSpeed) + (raisedForce);
  219.     rb.velocity = desiredMovement;
  220.     gameStatics.currentPlayerSpeed = Mathf.Abs(rb.velocity.x);
  221.  
  222.     lastFallSpeed = Mathf.Min(rb.velocity.y, lastFallSpeed);
  223.   }
  224. }
  225. public sealed class additionalForcesHandler {
  226.   private List<playerAppendForce> forceSources = new List<playerAppendForce> ();
  227.   private float _x = 0f;
  228.   private float _y = 0f;
  229.   public additionalForcesHandler (float x, float y) {
  230.     _x = x;
  231.     _y = y;
  232.   }
  233.   public void append (playerAppendForce obj) {
  234.     if (forceSources.IndexOf(obj) == -1) {
  235.       forceSources.Add(obj);
  236.     }
  237.   }
  238.   public void remove (playerAppendForce obj) {
  239.     if (forceSources.IndexOf(obj) > -1) {
  240.       forceSources.Remove(obj);
  241.     }
  242.   }
  243.   public Vector2 toVector2(bool sneaking) {
  244.     Vector2 result = new Vector2(_x, _y);
  245.     foreach (playerAppendForce i in forceSources) {
  246.       if (i.activated && (!sneaking || i.ignoreSneak)) {
  247.         Vector2 calcForce = i.transform.rotation * new Vector2(0f, i.force);
  248.         result = result + calcForce;
  249.       }
  250.     }
  251.     return(result);
  252.   }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement