daily pastebin goal
90%
SHARE
TWEET

Untitled

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