Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class playerScript : MonoBehaviour {
  4.     #region Main Vars
  5.     [SerializeField] private float speed, jumpForce, jumpPadForce;
  6.  
  7.     private Rigidbody2D rb;
  8.  
  9.     public bool onGround, wallCheck, floorCheck, isMoving, isJumping;
  10.  
  11.     //Death state
  12.     public bool deathState = false;
  13.  
  14.     //Transtion state machine
  15.     public string transistionState = "none";
  16.     #endregion
  17.  
  18.     void Start() {
  19.         rb = gameObject.GetComponent<Rigidbody2D>();
  20.         }
  21.  
  22.     void Update() {
  23.  
  24.         #region Player Movement
  25.  
  26.         //Horizontall move
  27.         float move = Input.GetAxis("Horizontal");
  28.         rb.velocity = new Vector2(move * speed, rb.velocity.y);
  29.  
  30.         //Jump
  31.         if (canJump())
  32.             rb.velocity = Vector2.up * jumpForce;
  33.  
  34.         //Movement check
  35.         isMoving = move != 0 || onGround == false ? true : false;
  36.  
  37.         #endregion
  38.         }
  39.  
  40.     //Collision check
  41.     private void OnCollisionEnter2D(Collision2D collision) {
  42.  
  43.         string col = collision.collider.tag;
  44.  
  45.         if (isGround(col)) {
  46.             clearAxisZ();            
  47.             }
  48.  
  49.         if (isSwing(col)) {
  50.             rb.constraints = RigidbodyConstraints2D.None;
  51.             }
  52.         else {
  53.             clearAxisZ();
  54.             }
  55.  
  56.         if (isSpike(col))
  57.             deathState = true;
  58.  
  59.         if (isJumpPad(col))
  60.             rb.velocity = Vector2.up * jumpPadForce;
  61.  
  62.         if (isPortal(col))
  63.             transistionState = "portal";
  64.  
  65.         }
  66.  
  67.     private void clearAxisZ() {
  68.         Quaternion rotation = transform.localRotation;
  69.         rotation.z = 0;
  70.         transform.localRotation = rotation;
  71.         rb.constraints = RigidbodyConstraints2D.FreezeRotation;
  72.         }
  73.  
  74.  
  75.     private bool canJump() {
  76.         return Input.GetButtonDown("Jump") && onGround == true && wallCheck == false ? true : false;
  77.         }
  78.  
  79.  
  80.     #region Define collision types
  81.     private bool isGround(string type) {
  82.         return type.Equals("ground");
  83.         }
  84.  
  85.     private bool isSpike(string type) {
  86.         return type.Equals("spike");
  87.         }
  88.  
  89.     private bool isJumpPad(string type) {
  90.         return type.Equals("jumpPad");
  91.         }
  92.  
  93.     private bool isSwing(string type) {
  94.         return type.Equals("swing");
  95.         }
  96.  
  97.     private bool isPortal(string type) {
  98.         return type.Equals("portal");
  99.         }
  100.     #endregion
  101.  
  102.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement