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 float length;
- //GroundState constructor. Sets offsets for raycasting.
- public GroundState(GameObject playerRef)
- {
- player = playerRef;
- width = player.GetComponent<Collider2D>().bounds.extents.x + 0.1f;
- height = player.GetComponent<Collider2D>().bounds.extents.y + 0.2f;
- length = 0.05f;
- }
- //Returns whether or not player is touching wall.
- public bool isWall()
- {
- bool left = Physics2D.Raycast(new Vector2(player.transform.position.x - (width), player.transform.position.y), -Vector2.right, length);
- bool right = Physics2D.Raycast(new Vector2(player.transform.position.x + (width), player.transform.position.y), Vector2.right, length);
- if (left || right)
- return true;
- else
- return false;
- }
- //Returns whether or not player is touching ground.
- public bool isGround()
- {
- bool bottom1 = Physics2D.Raycast(new Vector2(player.transform.position.x, player.transform.position.y - height), -Vector2.up, length);
- bool bottom2 = Physics2D.Raycast(new Vector2(player.transform.position.x + (width - 0.1f), player.transform.position.y - height), -Vector2.up, length);
- bool bottom3 = Physics2D.Raycast(new Vector2(player.transform.position.x - (width - 0.1f), player.transform.position.y - height), -Vector2.up, length);
- if (bottom1 || bottom2 || bottom3)
- return true;
- else
- return false;
- }
- //Returns whether or not player is touching wall or ground.
- public bool isTouching()
- {
- if (isGround() || isWall())
- return true;
- else
- return false;
- }
- //Returns direction of wall.
- public int wallDirection()
- {
- bool left1 = Physics2D.Raycast(new Vector2(player.transform.position.x - width, player.transform.position.y), -Vector2.right, length);
- bool right1 = Physics2D.Raycast(new Vector2(player.transform.position.x + width, player.transform.position.y), Vector2.right, length);
- if (left1)
- return -1;
- else if (right1)
- 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(1);
- 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()
- {
- if (death == true)
- {
- return;
- }
- if (powerJump && (groundState.isTouching() || GetComponent<Rigidbody2D>().velocity.y < 0))
- {
- powerJump = false;
- }
- if (input.y == 1 && groundState.isTouching())
- powerJump = true;
- GetComponent<Rigidbody2D>().AddForce(new Vector2(((input.x * speed) - GetComponent<Rigidbody2D>().velocity.x) * (groundState.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 && groundState.isGround()) ? 0 : GetComponent<Rigidbody2D>().velocity.x, (input.y == 1 && groundState.isTouching()) ? jump : GetComponent<Rigidbody2D>().velocity.y);
- if (groundState.isWall() && !groundState.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", groundState.isGround());
- animator.SetBool("walled", groundState.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