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 (Input.GetButtonDown("Jump") && onGround == true && wallCheck == false)
- rb.velocity = Vector2.up * jumpForce;
- //Movement check
- isMoving = move != 0 || onGround == false ? true : false;
- #endregion
- }
- //Collision check
- private void OnTriggerEnter2D(Collider2D col) {
- switch (col.tag) {
- case "ground":
- clearAxisZ();
- floorCheck = true;
- rb.constraints = RigidbodyConstraints2D.FreezeRotation;
- break;
- case "spike":
- deathState = true;
- break;
- case "jumpPad":
- rb.velocity = Vector2.up * jumpPadForce;
- break;
- case "portal":
- transistionState = "portal";
- break;
- case "swing":
- rb.constraints = RigidbodyConstraints2D.None;
- break;
- default:
- rb.constraints = RigidbodyConstraints2D.FreezeRotation;
- break;
- }
- }
- private void clearAxisZ() {
- Quaternion rotation = transform.localRotation;
- rotation.z = 0;
- transform.localRotation = rotation;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement