Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using UnityEngine;
- public class Player : MonoBehaviour {
- public int direction = 0;
- public KeyCode leftkey, rightkey, jumpkey;
- public float moveSpeed = 3f;
- public float jumpForce = 250f;
- private Rigidbody2D rbody;
- private Animator anim;
- private GuiManager guiManager;
- private void Start() {
- rbody = GetComponent<Rigidbody2D>();
- anim = GetComponent<Animator>();
- guiManager = GameObject.Find ("Gui").GetComponent<GuiManager> ();
- }
- private void Update() {
- UpdateControls();
- UpdateMovement();
- UpdateBreaking();
- }
- private void UpdateControls() {
- // Movement controls
- if (Input.GetKey (leftkey)) {
- FaceDirection (-1);
- anim.SetBool ("IsWalking", true);
- } else if (Input.GetKey (rightkey)) {
- FaceDirection (1);
- anim.SetBool ("IsWalking", true);
- } else {
- FaceDirection (0);
- anim.SetBool ("IsWalking", false);
- }
- // jump controls
- if (Input.GetKeyDown (jumpkey)) {
- Jump ();
- }
- // Inventory
- if (Input.GetKeyDown (KeyCode.I)) {
- if(guiManager.isPlayerInventoryOpen) {
- guiManager.ShowPlayerInventory(false);
- } else {
- guiManager.ShowPlayerInventory(true);
- }
- }
- }
- private void FaceDirection(int dir) {
- if (dir == direction) return;
- direction = dir;
- if(dir == 0) { //steve facing front
- transform.FindChild("Side").gameObject.SetActive(false);
- transform.FindChild("Front").gameObject.SetActive(true);
- } else { // steve facing side
- transform.FindChild("Side").gameObject.SetActive(true);
- transform.FindChild("Front").gameObject.SetActive(false);
- transform.FindChild("Side").localScale = new Vector3(dir * -1, 1, 1);
- }
- }
- private void UpdateMovement() {
- rbody.velocity = new Vector2(moveSpeed * direction, rbody.velocity.y);
- }
- private void UpdateBreaking() {
- if(Input.GetMouseButtonDown(0)) {
- Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
- RaycastHit2D hit = Physics2D.Raycast(pos, transform.position);
- if(hit.collider != null) {
- if(hit.collider.gameObject.tag == "Block") {
- GameObject.Find("World").GetComponent <WorldGen>().DestroyBlock(hit.collider.gameObject);
- }
- }
- }
- }
- private void Jump() {
- rbody.AddForce(transform.up * jumpForce);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement