Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.96 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 (Input.GetButtonDown("Jump") && onGround == true && wallCheck == false)
  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 OnTriggerEnter2D(Collider2D col) {
  42.  
  43.         switch (col.tag) {
  44.             case "ground":
  45.                 clearAxisZ();
  46.                 floorCheck = true;
  47.                 rb.constraints = RigidbodyConstraints2D.FreezeRotation;
  48.                 break;
  49.  
  50.             case "spike":
  51.                 deathState = true;
  52.                 break;
  53.  
  54.             case "jumpPad":
  55.                 rb.velocity = Vector2.up * jumpPadForce;
  56.                 break;
  57.  
  58.             case "portal":
  59.                 transistionState = "portal";
  60.                 break;
  61.  
  62.             case "swing":
  63.                 rb.constraints = RigidbodyConstraints2D.None;
  64.                 break;
  65.  
  66.             default:
  67.                 rb.constraints = RigidbodyConstraints2D.FreezeRotation;
  68.                 break;
  69.         }
  70.     }
  71.  
  72.     private void clearAxisZ() {
  73.         Quaternion rotation = transform.localRotation;
  74.         rotation.z = 0;
  75.         transform.localRotation = rotation;
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement