Advertisement
Guest User

player anim

a guest
Mar 11th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.55 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3. public class Player : MonoBehaviour {
  4.  
  5.     public int direction = 0;
  6.     public KeyCode leftkey, rightkey, jumpkey;
  7.     public float moveSpeed = 3f;
  8.     public float jumpForce = 250f;
  9.  
  10.     private Rigidbody2D rbody;
  11.     private Animator anim;
  12.     private GuiManager guiManager;
  13.    
  14.     private void Start() {
  15.         rbody = GetComponent<Rigidbody2D>();
  16.         anim = GetComponent<Animator>();
  17.         guiManager = GameObject.Find ("Gui").GetComponent<GuiManager> ();
  18.     }
  19.  
  20.     private void Update() {
  21.         UpdateControls();
  22.         UpdateMovement();
  23.         UpdateBreaking();
  24.     }
  25.  
  26.     private void UpdateControls() {
  27.         // Movement controls
  28.         if (Input.GetKey (leftkey)) {
  29.             FaceDirection (-1);
  30.             anim.SetBool ("IsWalking", true);
  31.         } else if (Input.GetKey (rightkey)) {
  32.             FaceDirection (1);
  33.             anim.SetBool ("IsWalking", true);
  34.         } else {
  35.             FaceDirection (0);
  36.             anim.SetBool ("IsWalking", false);
  37.         }
  38.  
  39.         // jump controls
  40.         if (Input.GetKeyDown (jumpkey)) {
  41.             Jump ();
  42.         }
  43.  
  44.         // Inventory
  45.         if (Input.GetKeyDown (KeyCode.I)) {
  46.             if(guiManager.isPlayerInventoryOpen) {
  47.                 guiManager.ShowPlayerInventory(false);
  48.             } else {
  49.                 guiManager.ShowPlayerInventory(true);
  50.             }
  51.         }
  52.  
  53.     }
  54.  
  55.     private void FaceDirection(int dir) {
  56.         if (dir == direction) return;
  57.  
  58.         direction = dir;
  59.  
  60.         if(dir == 0) { //steve facing front
  61.             transform.FindChild("Side").gameObject.SetActive(false);
  62.             transform.FindChild("Front").gameObject.SetActive(true);
  63.         } else { // steve facing side
  64.             transform.FindChild("Side").gameObject.SetActive(true);
  65.             transform.FindChild("Front").gameObject.SetActive(false);
  66.             transform.FindChild("Side").localScale = new Vector3(dir * -1, 1, 1);
  67.         }
  68.     }
  69.  
  70.     private void UpdateMovement() {
  71.         rbody.velocity = new Vector2(moveSpeed * direction, rbody.velocity.y);
  72.     }
  73.  
  74.     private void UpdateBreaking() {
  75.         if(Input.GetMouseButtonDown(0)) {
  76.             Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  77.             RaycastHit2D hit = Physics2D.Raycast(pos, transform.position);
  78.             if(hit.collider != null) {
  79.                if(hit.collider.gameObject.tag == "Block") {
  80.                     GameObject.Find("World").GetComponent <WorldGen>().DestroyBlock(hit.collider.gameObject);
  81.                 }
  82.             }
  83.         }
  84.     }
  85.  
  86.     private void Jump() {
  87.         rbody.AddForce(transform.up * jumpForce);
  88.     }
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement