Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.17 KB | None | 0 0
  1. float speed = 8f;
  2.     float rotSpeed = 140f;
  3.     float rot = 0f;
  4.     float Gravity = 6f;
  5.     float distance = 1f;
  6.  
  7.     public static float weapon;
  8.     public static float damage = 10f;
  9.     public static float health;
  10.     public static float maxHealth = 100f;
  11.     bool isAlive = true;
  12.  
  13.     public static bool isPaused = false;
  14.  
  15.     public Collider swordCollider;
  16.     public Collider bodyCollider;
  17.    
  18.  
  19.     private int allSlots;
  20.     private int enabledSlots;
  21.     private bool inventoryOn;
  22.     public GameObject inventory;
  23.    
  24.  
  25.     Vector3 moveDir;
  26.     CharacterController Controller;
  27.     Animator anim;
  28.  
  29.     void Start()
  30.     {
  31.         Controller = GetComponent<CharacterController> ();
  32.         anim = GetComponent<Animator> ();
  33.         Cursor.visible = false;
  34.         health = maxHealth;
  35.         swordCollider.enabled = false;
  36.     }
  37.  
  38.     void Update()
  39.     {
  40.         Move();
  41.         Look();
  42.         StartCoroutine(AttackRoutine());
  43.         CheckIfInvOn();
  44.  
  45.         if (health <= 0) { Destroy(this.gameObject); }
  46.  
  47.     }
  48.  
  49.     void Move()
  50.     {
  51.         if (Input.GetKey(KeyCode.W))
  52.         {
  53.             if (anim.GetBool("Attacking") == true) { return; }
  54.  
  55.             else if (anim.GetBool("Attacking") == false)
  56.             {
  57.                 anim.SetBool("Walking", true);
  58.                 anim.SetInteger("Condition", 1);
  59.                 moveDir = new Vector3(0f, 0f, Input.GetAxis("Vertical") * speed);
  60.                 moveDir = transform.TransformDirection(Vector3.forward) * speed;
  61.  
  62.                 moveDir.y -= Gravity;
  63.  
  64.                 Controller.Move(moveDir * Time.deltaTime);
  65.             }
  66.         }
  67.  
  68.         if (Input.GetKeyUp(KeyCode.W))
  69.         {
  70.             anim.SetBool("Walking", false);
  71.             anim.SetInteger("Condition", 0);
  72.         }
  73.     }
  74.  
  75.     void Look()
  76.     {
  77.         RaycastHit hit;
  78.         Ray mousePos = Camera.main.ScreenPointToRay(Input.mousePosition);
  79.         if (Physics.Raycast(mousePos, out hit))
  80.         {
  81.             transform.LookAt(new Vector3(hit.point.x, transform.position.y, hit.point.z));
  82.         }
  83.  
  84.     }
  85.  
  86.     IEnumerator AttackRoutine()
  87.     {
  88.         if (Input.GetKey(KeyCode.Mouse0))
  89.         {
  90.             anim.SetBool("Attacking", true);
  91.             anim.SetInteger("Condition", 2);
  92.             yield return new WaitForSeconds(0.4f);
  93.             swordCollider.enabled = !swordCollider.enabled;
  94.             yield return new WaitForSeconds(0.35f);
  95.             swordCollider.enabled = !swordCollider.enabled;
  96.             anim.SetBool("Attacking", false);
  97.             anim.SetInteger("Condition", 0);
  98.         }
  99.  
  100.     }
  101.  
  102.     private void OnTriggerEnter(Collider body)
  103.     {
  104.         if(body.CompareTag("Enemy")) { health -= 10f; }
  105.         Debug.Log(health);
  106.     }
  107.  
  108.     void CheckIfInvOn()
  109.     {
  110.         if (Input.GetKeyDown(KeyCode.E)) { inventoryOn = !inventoryOn; }
  111.  
  112.         if(inventoryOn == true)
  113.         {
  114.             inventory.SetActive(true);
  115.             Time.timeScale = 0f;
  116.             Cursor.visible = true;
  117.         }
  118.  
  119.         else
  120.         {
  121.             inventory.SetActive(false);
  122.             Cursor.visible = false;
  123.             Time.timeScale = 1f;
  124.         }
  125.     }
  126.  
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement