Guest User

Untitled

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