Hello, im in need of help with my enemy code. There are no errors but what is happening is that when there are multiple enemies surrounding the hero (thats you), once you start attacking not only is the enemy you click on hit but all the others around you in range gets hit too. This bug is only effecting the melee class of the project so the ranged code isnt needed.
this is what i have for both Enemy and PlayerController:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
//MainStats
public int PlayerHealth;
public int PlayerMaxHealth;
public int PlayerEnergy;
public int PlayerMaxEnergy;
public int PlayerMeleeAttack;
public int PlayerRangeAttack;
public float MeleeSpeed;
public float RangeSpeed;
//Minor Stats
public int KeyRing;
public float Speed;
public float Sprint;
public float JumpHeight;
public bool IsGrounded;
private new Rigidbody rigidbody;
void Awake(){
}
void Start(){
//MainStats
PlayerHealth = PlayerMaxHealth;
PlayerEnergy = PlayerMaxEnergy;
//Minor Stats
Speed = 5.0f;
Sprint = 8.0f;
JumpHeight = 5f;
rigidbody = GetComponent ();
PlayerEnergy = 0;
}
void Update(){
Forward_Back ();
Jump ();
EnergyControl ();
}
//Controls the player's Energy
void EnergyControl(){
if (PlayerEnergy >= PlayerMaxEnergy) {
PlayerEnergy = PlayerMaxEnergy;
}
}
//Movement Controll.
//Allows The Movement of the Character.
void Forward_Back(){
if (Input.GetKey (KeyCode.W)) {
transform.Translate (Vector3.forward * Time.deltaTime * Speed);
}
if (Input.GetKey (KeyCode.S)) {
Speed = 3.0f;
transform.Translate (Vector3.back * Time.deltaTime * Speed);
} else {
Speed = 5.0f;
}
if (Input.GetKey (KeyCode.W) && Input.GetKey(KeyCode.LeftShift)) {
transform.Translate (Vector3.forward * Time.deltaTime * Sprint);
}
}
void Jump(){
if (Input.GetKeyDown (KeyCode.Space) && IsGrounded == true) {
rigidbody.AddForce (transform.up * JumpHeight, ForceMode.Impulse);
}
}
void OnTriggerEnter(Collider col){
if (col.tag == "Floor") {
IsGrounded = true;
}
}
void OnTriggerExit(Collider col){
if (col.tag == "Floor") {
IsGrounded = false;
}
}
//Player Turning Controll.
//Player Will Be Lock Onto the Mouse Cursor.
public int CameraLookSpeed;
void FixedUpdate(){
Plane playerPlane = new Plane (Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast (ray, out hitdist)) {
Vector3 targetPoint = ray.GetPoint (hitdist);
Quaternion targetRotation = Quaternion.LookRotation (targetPoint - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, CameraLookSpeed * Time.deltaTime);
}
}
}
and this is the Enemy code.
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
public float Speed;
public int EnemyHealth;
public int EnemyMaxHealth;
public float MeleeRange;
public int EnemyDamage;
public float EnemyAttackSpeed;
public float AggroRange;
private float EnemyMinRange;
private bool BLOCK;
private bool EBLOCK;
public bool Dummy;
public bool KeyStone;
public bool JellyCube;
public bool Rat;
public bool Spider;
public bool Zombie;
public bool Skeleton;
private GameObject Player;
public GameObject HitEffect;
void Start(){
EnemyHealth = EnemyMaxHealth;
Player = GameObject.FindGameObjectWithTag ("Player");
MeleeRange = 4.0f;
EnemyMinRange = 3.0f;
}
void Update(){
_DummyEnemy ();
_JellyCube ();
_DestroyableObject ();
_Attacked ();
InAggroRange ();
}
//Immortal Creatures----------------------------------------------------------------------------------------
void _DummyEnemy(){
if (EnemyHealth <= EnemyMaxHealth && Dummy) {
EnemyHealth = EnemyMaxHealth;
}
}
//-----------------------------------------------------------------------------------------------------------
//Creature Stats---------------------------------------------------------------------------------------------
void _JellyCube(){
if (JellyCube) {
EnemyMaxHealth = 10;
EnemyDamage = 1;
EnemyAttackSpeed = 0.5f;
}
if (EnemyHealth <= 0) {
Destroy (this.gameObject);
}
}
//This Is Dying-----------------------------------------------------------------------------------------------
void _DestroyableObject(){
if (EnemyHealth <= 0 && KeyStone) {
Player.GetComponent ().KeyRing += 1;
Destroy (this.gameObject);
}
}
//------------------------------------------------------------------------------------------------------------
//This Is Getting Attacked-------------------------------------------------------------------------------------
void _Attacked(){
if (Input.GetKey (KeyCode.Mouse0)) {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray)) {
if (Vector3.Distance (transform.position, Player.transform.position) < MeleeRange) {
if (!BLOCK) {
Invoke ("_TakeMeleeDamage", 0.0f);
Instantiate (HitEffect, this.transform.position, this.transform.rotation);
BLOCK = true;
Invoke ("_UnBLOCK", Player.GetComponent ().MeleeSpeed);
}
}
}
}
}
//----------------------------------------------------------------------------------------------------------------
//Enemy Attacking-------------------------------------------------------------------------------------------------
void _Attacking(){
Player.GetComponent ().PlayerHealth -= this.EnemyDamage;
}
//----------------------------------------------------------------------------------------------------------------
//If Player is in Range-------------------------------------------------------------------------------------------
void InAggroRange(){
if (Vector3.Distance (transform.position, Player.transform.position) < AggroRange) {
this.transform.LookAt (Player.transform);
this.transform.Translate (Vector3.forward * Time.deltaTime * Speed);
}
if (Vector3.Distance (transform.position, Player.transform.position) < EnemyMinRange) {
Speed = 0.0f;
if (!EBLOCK) {
Invoke ("_Attacking", 0.0f);
EBLOCK = true;
Invoke ("_UnEBLOCK", EnemyAttackSpeed);
}
} else if (Vector3.Distance (transform.position, Player.transform.position) > EnemyMinRange) {
Speed = 1.0f;
}
}
//-----------------------------------------------------------------------------------------------------------------
//Payer hit the Enemy----------------------------------------------------------------------------------------------
void _TakeMeleeDamage(){
this.EnemyHealth -= Player.GetComponent ().PlayerMeleeAttack;
Player.GetComponent ().PlayerEnergy += Player.GetComponent ().PlayerMeleeAttack;
}
void OnTriggerEnter(Collider Col){
if (Col.tag == "Bolt") {
EnemyHealth -= Player.GetComponent ().PlayerRangeAttack;
Instantiate (HitEffect, this.transform.position, this.transform.rotation);
Player.GetComponent ().PlayerEnergy += Player.GetComponent ().PlayerRangeAttack + 1;
}
if (Col.tag == "MagicBolt") {
EnemyHealth -= Player.GetComponent ().PlayerRangeAttack;
Instantiate (HitEffect, this.transform.position, this.transform.rotation);
Player.GetComponent ().PlayerEnergy += Player.GetComponent ().PlayerRangeAttack + 1;
}
}
//------------------------------------------------------------------------------------------------------------------
void _UnBLOCK(){
BLOCK = false;
}
void _UnEBLOCK(){
EBLOCK = false;
}
}
and if there are any tips on how to optimize my scripts i would very much appreciate it!
↧