Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Controller3 : MonoBehaviour
- {
- public Animator animator;
- bool death = false;
- public class GroundState
- {
- private GameObject player;
- private float width;
- private float height;
- private bool leftWall;
- private bool rightWall;
- //GroundState constructor. Sets offsets for raycasting.
- public GroundState(GameObject playerRef)
- {
- player = playerRef;
- width = player.GetComponent<Collider2D>().bounds.extents.x;
- height = player.GetComponent<Collider2D>().bounds.extents.y;
- }
- //Returns whether or not player is touching wall.
- public bool isWall()
- {
- int count;
- Collider2D[] results;
- count = Physics2D.OverlapArea(new Vector2(player.transform.position.x - width , player.transform.position.y - height),
- new Vector2(player.transform.position.x - width - 0.15f, player.transform.position.y + height),
- ContactFilter2D.NoFilter(),
- results);
- leftWall = count > 1;
- count = Physics2D.OverlapArea(new Vector2(player.transform.position.x + width, player.transform.position.y - height),
- new Vector2(player.transform.position.x + width + 0.15f, player.transform.position.y + height),
- ContactFilter2D.NoFilter(),
- results);
- rightWall = count > 1;
- return leftWall || rightWall;
- }
- //Returns whether or not player is touching ground.
- public bool isGround()
- {
- Collider2D[] results;
- int count = Physics2D.OverlapArea(new Vector2(player.transform.position.x - width, player.transform.position.y - height),
- new Vector2(player.transform.position.x + width, player.transform.position.y - height - 0.15f),
- ContactFilter2D.NoFilter(),
- results;
- );
- return count > 1;
- }
- //Returns whether or not player is touching wall or ground.
- public bool isTouching()
- {
- if (isGround() || isWall())
- return true;
- else
- return false;
- }
- public int wallDirection()
- {
- if (leftWall)
- return -1;
- else if (rightWall)
- return 1;
- else
- return 0;
- }
- }
- IEnumerator Delay()
- {
- powerJump = false;
- GetComponent<Animator>().Play("Player_Death", 0, 0f);
- death = true;
- gameObject.GetComponent<Rigidbody2D>().isKinematic = true;
- gameObject.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezePositionY;
- GetComponent<Rigidbody2D>().velocity = new Vector2(0, 0);
- GetComponent<Rigidbody2D>().gravityScale = 0.0f;
- yield return new WaitForSeconds(0.5f);
- GameManager.instance.ResetLevel();
- }
- void OnTriggerEnter2D(Collider2D other)
- {
- //When touching the trap.
- if (other.CompareTag("Trap") && !death)
- {
- StartCoroutine(Delay());
- }
- //When touching the finish.
- else if (other.CompareTag("Finish"))
- {
- //Go to next level.
- GameManager.instance.IncreaseLevel();
- }
- }
- public float speed = 14f;
- public float accel = 6f;
- public float airAccel = 3f;
- public float jump = 14f;
- public float powerJumpStopBrakeMultiplier = 0.5f;
- private GroundState groundState;
- private bool powerJump;
- void Start()
- {
- //Create an object to check if player is grounded or touching wall
- groundState = new GroundState(transform.gameObject);
- }
- private Vector2 input;
- void Update()
- {
- if (death == true)
- {
- return;
- }
- //Handle input
- if (Input.GetKey(KeyCode.LeftArrow))
- input.x = -1;
- else if (Input.GetKey(KeyCode.RightArrow))
- input.x = 1;
- else
- input.x = 0;
- if (Input.GetButtonDown("Jump"))
- input.y = 1;
- //Stopped powerjumping
- if (powerJump && Input.GetButtonUp("Jump"))
- {
- powerJump = false;
- GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, GetComponent<Rigidbody2D>().velocity.y * powerJumpStopBrakeMultiplier);
- }
- //Reverse player if going different direction
- transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, (input.x == 0) ? transform.localEulerAngles.y : (input.x + 1) * 90, transform.localEulerAngles.z);
- //Check if run into obstacle.
- }
- void FixedUpdate()
- {
- bool isTouching = groundState.isTouching();
- bool isWall = groundState.isWall();
- bool isGround = groundState.isGround();
- if (death == true)
- {
- return;
- }
- if (powerJump && (isTouching || GetComponent<Rigidbody2D>().velocity.y < 0))
- {
- powerJump = false;
- }
- if (input.y == 1 && isTouching)
- powerJump = true;
- GetComponent<Rigidbody2D>().AddForce(new Vector2(((input.x * speed) - GetComponent<Rigidbody2D>().velocity.x) * (isGround ? accel : airAccel), 0)); //Move player.
- //Stop player if input.x is 0 (and grounded) and jump if input.y is 1
- GetComponent<Rigidbody2D>().velocity = new Vector2((input.x == 0 && isGround ? 0 : GetComponent<Rigidbody2D>().velocity.x), (input.y == 1 && isTouching ? jump : GetComponent<Rigidbody2D>().velocity.y));
- if (groundState.wallDirection() != 0)
- {
- Debug.Log("AAAAAAAAA: " + groundState.wallDirection());
- }
- if (isWall && !isGround && input.y == 1)
- GetComponent<Rigidbody2D>().velocity = new Vector2(-groundState.wallDirection() * speed * 0.75f, GetComponent<Rigidbody2D>().velocity.y); //Add force negative to wall direction (with speed reduction)
- input.y = 0;
- animator.SetFloat("horizontal", Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x));
- animator.SetFloat("vertical", GetComponent<Rigidbody2D>().velocity.y);
- animator.SetBool("grounded", isGround);
- animator.SetBool("walled", isWall);
- //animator.SetBool("dead", death);
- }
- //Enables being carried by moving objects tagged as "platform"
- void OnCollisionEnter2D(Collision2D col)
- {
- if (col.gameObject.CompareTag("Platform"))
- this.transform.parent = col.transform;
- }
- void OnCollisionExit2D(Collision2D col)
- {
- if (col.gameObject.CompareTag("Platform"))
- this.transform.parent = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement