Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class playerScript : MonoBehaviour {
- #region Main Vars
- [SerializeField] private float speed, jumpForce, jumpPadForce;
- private Rigidbody2D rb;
- public bool onGround, wallCheck, floorCheck, isMoving, isJumping;
- //Death state
- public bool deathState = false;
- //Transtion state machine
- public string transistionState = "none";
- #endregion
- void Start() {
- rb = gameObject.GetComponent<Rigidbody2D>();
- }
- void Update() {
- #region Player Movement
- //Horizontall move
- float move = Input.GetAxis("Horizontal");
- rb.velocity = new Vector2(move * speed, rb.velocity.y);
- //Jump
- if (canJump())
- rb.velocity = Vector2.up * jumpForce;
- //Movement check
- isMoving = move != 0 || onGround == false ? true : false;
- #endregion
- }
- //Collision check
- private void OnCollisionEnter2D(Collision2D collision) {
- string col = collision.collider.tag;
- if (isGround(col)) {
- clearAxisZ();
- }
- if (isSwing(col)) {
- rb.constraints = RigidbodyConstraints2D.None;
- }
- else {
- clearAxisZ();
- }
- if (isSpike(col))
- deathState = true;
- if (isJumpPad(col))
- rb.velocity = Vector2.up * jumpPadForce;
- if (isPortal(col))
- transistionState = "portal";
- }
- private void clearAxisZ() {
- Quaternion rotation = transform.localRotation;
- rotation.z = 0;
- transform.localRotation = rotation;
- rb.constraints = RigidbodyConstraints2D.FreezeRotation;
- }
- private bool canJump() {
- return Input.GetButtonDown("Jump") && onGround == true && wallCheck == false ? true : false;
- }
- #region Define collision types
- private bool isGround(string type) {
- return type.Equals("ground");
- }
- private bool isSpike(string type) {
- return type.Equals("spike");
- }
- private bool isJumpPad(string type) {
- return type.Equals("jumpPad");
- }
- private bool isSwing(string type) {
- return type.Equals("swing");
- }
- private bool isPortal(string type) {
- return type.Equals("portal");
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement