Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Platformer/Platformer/Assets/Scripts/Player&Enemy/Weapon.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
111 lines (84 sloc)
2.74 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class Weapon : MonoBehaviour | |
{ | |
public Transform firePoint; //where player is shooting from | |
public GameObject bulletPrefab; //shuriken | |
public int shots; //amount of shots | |
public int shurikenDamage = 10; //shuriken damage | |
public Animator animator; //animator | |
//Sword | |
public Transform attackPoint; //point of attack | |
public float attackRange = .05f; //circular range | |
public LayerMask enemyLayers; //only hits the enemies on enemy layer | |
public int attackDamage = 20; //sword damage | |
public float attackRate = 2.75f;//attack rate per second | |
float nextAttackTime = 0f;//next attack time | |
private static bool facingright; //facing right? reference to player input | |
void Update() | |
{ | |
if (Input.GetKeyDown(KeyCode.K)) | |
{ | |
Shoot(); | |
} | |
if (Time.time >= nextAttackTime) | |
{ | |
if (Input.GetKeyDown(KeyCode.J)) | |
{ | |
Attack(); | |
nextAttackTime = Time.time + 1f / attackRate; | |
} | |
} | |
} | |
void Shoot() | |
{ | |
if (shots > 0) | |
{ | |
animator.SetTrigger("Shuriken"); | |
SoundManagerScript.PlaySound("Throw"); | |
Instantiate(bulletPrefab, firePoint.position, firePoint.rotation); | |
shots = shots - 1; | |
ShurikenAmount.text = "X: " + shots; | |
//Damage in Bullet Script | |
} | |
else | |
{ | |
return; | |
} | |
} | |
void Attack() | |
{ | |
//Attack Animation | |
animator.SetTrigger("Attack"); | |
SoundManagerScript.PlaySound("Sword"); | |
//Detect enemies | |
Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers ); | |
foreach(Collider2D enemy in hitEnemies) | |
{ | |
//was getting errors until this, helped recognize layers or something idk | |
if(enemy.CompareTag("Bee")) | |
{ | |
enemy.GetComponent<Enemy1>().TakeDamage(attackDamage); | |
} | |
enemy.GetComponent<Enemy>().TakeDamage(attackDamage); | |
Debug.Log("We hit" + enemy.name); | |
} | |
} | |
void OnDrawGizmosSelected() | |
{ | |
Gizmos.DrawWireSphere(attackPoint.position, attackRange); | |
} | |
public Text ShurikenAmount; //text | |
void OnTriggerEnter2D(Collider2D other) //Pickups | |
{ | |
if (other.gameObject.CompareTag("NinjaStar")) //If you got the star | |
{ | |
SoundManagerScript.PlaySound("Collecting"); | |
other.gameObject.SetActive(false); //Star Dissapears | |
shots = shots + 1; //increase amount of shots | |
ShurikenAmount.text = "X: " + shots; | |
} | |
} | |
} |