Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.08 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Controller3 : MonoBehaviour
  5. {
  6.     public Animator animator;
  7.     bool death = false;
  8.     public class GroundState
  9.     {
  10.         private GameObject player;
  11.         private float width;
  12.         private float height;
  13.         private bool leftWall;
  14.         private bool rightWall;
  15.  
  16.         //GroundState constructor.  Sets offsets for raycasting.
  17.         public GroundState(GameObject playerRef)
  18.         {
  19.             player = playerRef;
  20.             width = player.GetComponent<Collider2D>().bounds.extents.x;
  21.             height = player.GetComponent<Collider2D>().bounds.extents.y;
  22.         }
  23.  
  24.         //Returns whether or not player is touching wall.
  25.         public bool isWall()
  26.         {
  27.             int count;
  28.             Collider2D[] results;
  29.             count = Physics2D.OverlapArea(new Vector2(player.transform.position.x - width , player.transform.position.y - height),
  30.             new Vector2(player.transform.position.x - width - 0.15f, player.transform.position.y + height),
  31.                 ContactFilter2D.NoFilter(),
  32.                 results);
  33.             leftWall = count > 1;
  34.  
  35.             count = Physics2D.OverlapArea(new Vector2(player.transform.position.x + width, player.transform.position.y - height),
  36.                 new Vector2(player.transform.position.x + width + 0.15f, player.transform.position.y + height),
  37.                 ContactFilter2D.NoFilter(),
  38.                 results);
  39.             rightWall = count > 1;
  40.  
  41.             return leftWall || rightWall;
  42.         }
  43.  
  44.         //Returns whether or not player is touching ground.
  45.         public bool isGround()
  46.         {
  47.             Collider2D[] results;
  48.             int count = Physics2D.OverlapArea(new Vector2(player.transform.position.x - width, player.transform.position.y - height),
  49.                 new Vector2(player.transform.position.x + width, player.transform.position.y - height - 0.15f),
  50.                 ContactFilter2D.NoFilter(),
  51.                 results;
  52.                 );
  53.             return count > 1;
  54.         }
  55.  
  56.         //Returns whether or not player is touching wall or ground.
  57.         public bool isTouching()
  58.         {
  59.             if (isGround() || isWall())
  60.                 return true;
  61.             else
  62.                 return false;
  63.         }
  64.  
  65.         public int wallDirection()
  66.         {
  67.             if (leftWall)
  68.                 return -1;
  69.             else if (rightWall)
  70.                 return 1;
  71.             else
  72.                 return 0;
  73.         }
  74.     }
  75.     IEnumerator Delay()
  76.     {
  77.         powerJump = false;
  78.         GetComponent<Animator>().Play("Player_Death", 0, 0f);
  79.         death = true;
  80.         gameObject.GetComponent<Rigidbody2D>().isKinematic = true;
  81.         gameObject.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezePositionY;
  82.         GetComponent<Rigidbody2D>().velocity = new Vector2(0, 0);
  83.         GetComponent<Rigidbody2D>().gravityScale = 0.0f;
  84.         yield return new WaitForSeconds(0.5f);
  85.         GameManager.instance.ResetLevel();
  86.     }
  87.     void OnTriggerEnter2D(Collider2D other)
  88.     {
  89.         //When touching the trap.
  90.         if (other.CompareTag("Trap") && !death)
  91.         {
  92.             StartCoroutine(Delay());
  93.         }
  94.         //When touching the finish.
  95.         else if (other.CompareTag("Finish"))
  96.         {
  97.             //Go to next level.
  98.             GameManager.instance.IncreaseLevel();
  99.         }
  100.     }
  101.     public float speed = 14f;
  102.     public float accel = 6f;
  103.     public float airAccel = 3f;
  104.     public float jump = 14f;
  105.     public float powerJumpStopBrakeMultiplier = 0.5f;
  106.  
  107.     private GroundState groundState;
  108.     private bool powerJump;
  109.  
  110.     void Start()
  111.     {
  112.         //Create an object to check if player is grounded or touching wall
  113.         groundState = new GroundState(transform.gameObject);
  114.     }
  115.  
  116.     private Vector2 input;
  117.  
  118.  
  119.  
  120.     void Update()
  121.     {
  122.         if (death == true)
  123.         {
  124.             return;
  125.         }
  126.  
  127.         //Handle input
  128.         if (Input.GetKey(KeyCode.LeftArrow))
  129.             input.x = -1;
  130.         else if (Input.GetKey(KeyCode.RightArrow))
  131.             input.x = 1;
  132.         else
  133.             input.x = 0;
  134.  
  135.         if (Input.GetButtonDown("Jump"))
  136.             input.y = 1;
  137.  
  138.         //Stopped powerjumping
  139.         if (powerJump && Input.GetButtonUp("Jump"))
  140.         {
  141.             powerJump = false;
  142.             GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, GetComponent<Rigidbody2D>().velocity.y * powerJumpStopBrakeMultiplier);
  143.         }
  144.  
  145.         //Reverse player if going different direction
  146.         transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, (input.x == 0) ? transform.localEulerAngles.y : (input.x + 1) * 90, transform.localEulerAngles.z);
  147.         //Check if run into obstacle.
  148.  
  149.  
  150.     }
  151.  
  152.     void FixedUpdate()
  153.     {
  154.         bool isTouching = groundState.isTouching();
  155.         bool isWall = groundState.isWall();
  156.         bool isGround = groundState.isGround();
  157.  
  158.         if (death == true)
  159.         {
  160.             return;
  161.         }
  162.  
  163.         if (powerJump && (isTouching || GetComponent<Rigidbody2D>().velocity.y < 0))
  164.         {
  165.             powerJump = false;
  166.         }
  167.  
  168.         if (input.y == 1 && isTouching)
  169.             powerJump = true;
  170.  
  171.         GetComponent<Rigidbody2D>().AddForce(new Vector2(((input.x * speed) - GetComponent<Rigidbody2D>().velocity.x) * (isGround ? accel : airAccel), 0)); //Move player.
  172.                                                                                                                                                             //Stop player if input.x is 0 (and grounded) and jump if input.y is 1
  173.         GetComponent<Rigidbody2D>().velocity = new Vector2((input.x == 0 && isGround ? 0 : GetComponent<Rigidbody2D>().velocity.x), (input.y == 1 && isTouching ? jump : GetComponent<Rigidbody2D>().velocity.y));
  174.  
  175.         if (groundState.wallDirection() != 0)
  176.         {
  177.             Debug.Log("AAAAAAAAA: " + groundState.wallDirection());
  178.         }
  179.         if (isWall && !isGround && input.y == 1)
  180.             GetComponent<Rigidbody2D>().velocity = new Vector2(-groundState.wallDirection() * speed * 0.75f, GetComponent<Rigidbody2D>().velocity.y); //Add force negative to wall direction (with speed reduction)
  181.  
  182.         input.y = 0;
  183.  
  184.         animator.SetFloat("horizontal", Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x));
  185.         animator.SetFloat("vertical", GetComponent<Rigidbody2D>().velocity.y);
  186.         animator.SetBool("grounded", isGround);
  187.         animator.SetBool("walled", isWall);
  188.         //animator.SetBool("dead", death);
  189.  
  190.     }
  191.     //Enables being carried by moving objects tagged as "platform"
  192.     void OnCollisionEnter2D(Collision2D col)
  193.     {
  194.         if (col.gameObject.CompareTag("Platform"))
  195.             this.transform.parent = col.transform;
  196.     }
  197.     void OnCollisionExit2D(Collision2D col)
  198.     {
  199.         if (col.gameObject.CompareTag("Platform"))
  200.             this.transform.parent = null;
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement