Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class playerController : MonoBehaviour {
- private Rigidbody2D rb;
- private float moveInput;
- private bool jumpInput;
- private bool sneakInput;
- private float lastJump = -10f;
- private float jumpTreshold = 1.5f;
- private float lastFallSpeed = 0f;
- [HideInInspector]
- public bool isFacingRight = true;
- [HideInInspector]
- public additionalForcesHandler AdditionalForces = new additionalForcesHandler(0f, 0f);
- private Vector2 raisedForce = new Vector2(0f, 0f);
- private bool isGrounded = true;
- private bool isWalled = false;
- private int extraJumpsValue;
- private float coyoteTime = 0f;
- private float wallCoyoteTime = 0f;
- [Header("Movement")]
- [Range(0.0f, 50.0f)]
- public float speed = 18.0f;
- [Range(0.000f, 1.000f)]
- public float sneakSpeedModifer = 0.225f;
- [Header("Coyote-Time")]
- [Range(0.0001f, 1.0000f)]
- public float coyoteTimeTreshold = 0.15f;
- [Range(0.0001f, 1.0000f)]
- public float wallCoyoteTimeTreshold = 0.25f;
- [Header("Sprung-Einstellungen")]
- public GameObject dblJumpPlatform;
- [Range(0.000f, 1.000f)]
- public float groundCheckRadius = 0.5f;
- [Range(0.0f, 50.0f)]
- public float jumpForce = 20.0f;
- [Range(0.000f, 1.000f)]
- public float groundFriction = 0.15f;
- [Range(0.000f, 1.000f)]
- public float groundStopFriction = 0.02f;
- [Range(0.000f, 1.000f)]
- public float airFriction = 0.1f;
- [Header("Wall-Jump/Slide")]
- [Range(0.000f, 1.000f)]
- public float wallCheckRadius = 0.5f;
- [Range(0.00f, 10.00f)]
- public float maxSlideSpeed = 0.2f;
- [Range(0.00f, 1.00f)]
- public float wallJumpAspect = 0.8f;
- [Header("Ebenen")]
- public LayerMask whatIsGround;
- public LayerMask whatIsWall;
- [Header("Sound")]
- public AudioClip[] sfxHitGround;
- [Range(0.00f, 0.35f)]
- public float hitGroundTreshold = 0.05f;
- private float lastHitGroundTreshold = -0.05f;
- public AudioClip[] sfxMoveStart;
- [Range(0.00f, 1.50f)]
- public float sfxMoveStartTreshold = 0.85f;
- private float lastSfxMoveStartTreshold = -0.85f;
- public AudioClip sfxJumpGround;
- public AudioClip sfxJumpDbl;
- public AudioClip sfxJumpWall;
- private Vector3 initialPosition;
- void Start() {
- initialPosition = transform.position;
- extraJumpsValue = gameStatics.extraJumps;
- rb = GetComponent<Rigidbody2D> ();
- }
- private float lastMoveInput;
- void Update() {
- if (gameStatics.allowPlayerInput) {
- lastMoveInput = (lastMoveInput != moveInput)?moveInput:lastMoveInput;
- moveInput = Input.GetAxisRaw("Horizontal");
- jumpInput = Input.GetButton("Jump");
- sneakInput = Input.GetButton("Fire3");
- if (jumpInput && gameStatics.selectedSkin != null && gameStatics.playerObject.transform.Find("PlayerVisual").GetComponent<SpriteRenderer> ().sprite != gameStatics.selectedSkin) {
- gameStatics.playerObject.transform.Find("PlayerVisual").GetComponent<SpriteRenderer> ().sprite = gameStatics.selectedSkin;
- gameStatics.selectedSkin = null;
- lastJump = Time.fixedTime;
- }
- } else {
- lastMoveInput = (lastMoveInput != moveInput)?moveInput:lastMoveInput;
- moveInput = 0;
- jumpInput = false;
- }
- bool beforeGrounded = isGrounded;
- bool beforeWalled = isWalled;
- if (!jumpInput) {
- lastJump = -1f;
- }
- if (moveInput < 0) {
- isFacingRight = false;
- } else if (moveInput > 0) {
- isFacingRight = true;
- }
- bool wasGrounded = isGrounded;
- 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);
- 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);
- isGrounded = (Mathf.Min((hit1.collider != null)?hit1.distance:Mathf.Infinity, (hit2.collider != null)?hit2.distance:Mathf.Infinity) <= groundCheckRadius);
- 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);
- 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);
- isWalled = gameStatics.canDoWallJump && (
- Mathf.Min(
- (hit_left_1.collider != null && !isFacingRight)?hit_left_1.distance:Mathf.Infinity,
- (hit_right1.collider != null && isFacingRight)?hit_right1.distance:Mathf.Infinity
- ) <= wallCheckRadius
- );
- if (isGrounded) {
- isWalled = false;
- coyoteTime = -(coyoteTimeTreshold + 1f);
- wallCoyoteTime = -(wallCoyoteTimeTreshold + 1f);
- extraJumpsValue = gameStatics.extraJumps;
- } else if (beforeGrounded && !jumpInput) {
- coyoteTime = Time.fixedTime;
- } else if (beforeGrounded && jumpInput) {
- coyoteTime = -(coyoteTimeTreshold + 1f);
- }
- if (!isGrounded && !isWalled && beforeWalled && !jumpInput) {
- wallCoyoteTime = Time.fixedTime;
- } else if (isWalled || (beforeWalled && jumpInput)) {
- wallCoyoteTime = -(wallCoyoteTimeTreshold + 1f);
- }
- if ((isGrounded && !beforeGrounded) || (isWalled && !beforeWalled && !beforeGrounded && gameStatics.canDoWallJump)) {
- if (Time.unscaledTime - lastHitGroundTreshold > hitGroundTreshold) {
- lastHitGroundTreshold = Time.unscaledTime;
- AudioClip groundSelectedSound = sfxHitGround[Random.Range(0, sfxHitGround.Length)];
- gameStatics.Sfx(groundSelectedSound, Random.Range(0.12f, 0.20f));
- }
- lastFallSpeed = 0f;
- }
- if(isGrounded && moveInput != 0) {
- if (Time.unscaledTime - lastSfxMoveStartTreshold > sfxMoveStartTreshold) {
- lastSfxMoveStartTreshold = Time.unscaledTime;
- AudioClip sfxMoveStartSound = sfxMoveStart[Random.Range(0, sfxMoveStart.Length)];
- gameStatics.Sfx(sfxMoveStartSound, Random.Range(0.08f, 0.25f));
- }
- }
- }
- void FixedUpdate() {
- Vector2 desiredMovement = rb.velocity;
- float triggeredSpeed = (sneakInput)?sneakSpeedModifer:1f;
- float calculatedXSpeed = Mathf.Lerp(rb.velocity.x, (speed*triggeredSpeed) * moveInput, (isGrounded)?((moveInput==0)?groundStopFriction:groundFriction):airFriction);
- float calculatedYSpeed = (isWalled && gameStatics.canDoWallJump)?Mathf.Max(-maxSlideSpeed, rb.velocity.y) : rb.velocity.y;
- bool coyoteTimeN = (Time.fixedTime - coyoteTime < coyoteTimeTreshold);
- bool coyoteTimeW = (Time.fixedTime - wallCoyoteTime < wallCoyoteTimeTreshold);
- bool canNJump = coyoteTimeN || (isGrounded || (extraJumpsValue > 0 && !isWalled && !coyoteTimeW));
- bool canWJump = (coyoteTimeW || isWalled) && gameStatics.canDoWallJump;
- if (jumpInput && Time.fixedTime - lastJump > jumpTreshold) {
- if (canNJump && !canWJump) {
- bool dblJump = false;
- if (!isGrounded && extraJumpsValue > 0) {
- extraJumpsValue--;
- dblJump = true;
- }
- if (canNJump || dblJump) {
- lastJump = Time.fixedTime;
- calculatedYSpeed = (jumpForce*triggeredSpeed);
- coyoteTime = -(coyoteTimeTreshold + 1f);
- wallCoyoteTime = -(wallCoyoteTimeTreshold + 1f);
- canNJump = (extraJumpsValue > 0);
- if (!dblJump) {
- gameStatics.Sfx(sfxJumpGround);
- } else {
- Instantiate(dblJumpPlatform, transform.position, transform.rotation);
- gameStatics.Sfx(sfxJumpDbl);
- }
- }
- } else if (!canNJump && canWJump) {
- lastJump = Time.fixedTime;
- calculatedYSpeed = (jumpForce*triggeredSpeed);
- calculatedXSpeed = (isFacingRight)?-(jumpForce*triggeredSpeed)*wallJumpAspect:(jumpForce*triggeredSpeed)*wallJumpAspect;
- calculatedXSpeed = (coyoteTimeW)?-calculatedXSpeed:calculatedXSpeed;
- isFacingRight = (coyoteTimeW)?isFacingRight:!isFacingRight;
- coyoteTime = -(coyoteTimeTreshold + 1f);
- wallCoyoteTime = -(wallCoyoteTimeTreshold + 1f);
- gameStatics.Sfx(sfxJumpWall);
- }
- }
- Vector2 taf = AdditionalForces.toVector2(sneakInput);
- raisedForce = Vector2.Lerp(raisedForce, taf, (taf == new Vector2(0f, 0f))?0.65f:0.35f);
- desiredMovement = new Vector2(calculatedXSpeed, calculatedYSpeed) + (raisedForce);
- rb.velocity = desiredMovement;
- gameStatics.currentPlayerSpeed = Mathf.Abs(rb.velocity.x);
- lastFallSpeed = Mathf.Min(rb.velocity.y, lastFallSpeed);
- }
- }
- public sealed class additionalForcesHandler {
- private List<playerAppendForce> forceSources = new List<playerAppendForce> ();
- private float _x = 0f;
- private float _y = 0f;
- public additionalForcesHandler (float x, float y) {
- _x = x;
- _y = y;
- }
- public void append (playerAppendForce obj) {
- if (forceSources.IndexOf(obj) == -1) {
- forceSources.Add(obj);
- }
- }
- public void remove (playerAppendForce obj) {
- if (forceSources.IndexOf(obj) > -1) {
- forceSources.Remove(obj);
- }
- }
- public Vector2 toVector2(bool sneaking) {
- Vector2 result = new Vector2(_x, _y);
- foreach (playerAppendForce i in forceSources) {
- if (i.activated && (!sneaking || i.ignoreSneak)) {
- Vector2 calcForce = i.transform.rotation * new Vector2(0f, i.force);
- result = result + calcForce;
- }
- }
- return(result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement