Hi, i try to apply damage from my sword using Raycast.
When i attach game object in the inspector it work great, but i can attack only one enemy i attach.
I try to find all my enemy with tag, but when i attack i got error:
What wrong? Please help me figure it out)))
Here all script:
using UnityEngine;
using System.Collections;
public class PlayerAttack : MonoBehaviour {
public int damage;
public float attackTimer;
public float coolDown;
private GameObject barObject;
private HealthBar healthBarScript;
public GUITexture attackbutton;
// Use this for initialization
void Start () {
//Find bar
barObject = GameObject.Find(“Bar”);
//Get bar from bar object
healthBarScript = barObject.GetComponent();
attackTimer = 0;
coolDown = 2.0f;
}
// Update is called once per frame
void Update () {
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
GameObject gos = GameObject.FindWithTag(“Enemy”);
if (Input.GetMouseButtonDown(0)){
if (attackbutton.HitTest( Input.mousePosition )) {
if(attackTimer == 0){
RaycastHit hit;
Ray ray = Camera.mainCamera.ScreenPointToRay(new Vector3(Screen.width / 2.0f, Screen.height / 2.0f, 0));
if (Physics.Raycast(ray, out hit, 4.0f))
{
//Get health script from targets
Health healthScript = gos.GetComponent();
//Deal damage
healthScript.health -= damage;
//If HP <0 make it 0
if (healthScript.health < 0)
{
healthScript.health = 0;
}
//Send information about HP to bar
healthBarScript.health = healthScript.health;
healthBarScript.healthMax = healthScript.healthMax;
//Show bar
healthBarScript.showBar = true;
}
attackTimer = coolDown;
}
}
}
}
}
I answer my question for miself)) This sword attack script work like a charm)))
using UnityEngine;
using System.Collections;
public class PlayerAttack : MonoBehaviour {
//Target assigment
private Transform target;
public int damage;
public float attackTimer;
public float coolDown;
private GameObject barObject;
private HealthBar healthBarScript;
public GUITexture attackbutton;
void Start () {
//Find bar
barObject = GameObject.Find(“Bar”);
//Get bar from bar object
healthBarScript = barObject.GetComponent();
attackTimer = 0;
coolDown = 1.0f;
}
void Update () {
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if (Input.GetMouseButtonDown(0)){
if (attackbutton.HitTest( Input.mousePosition )) {
if(attackTimer == 0){
RaycastHit hit;
Ray ray = Camera.mainCamera.ScreenPointToRay(new Vector3(Screen.width / 2.0f, Screen.height / 2.0f, 0));
if (Physics.Raycast(ray, out hit, 4.0f)){
if (hit.collider.gameObject.CompareTag(“Enemy”)){
//print out the name if the raycast hits something
Debug.Log(hit.collider.name);
target = hit.collider.transform;
Debug.Log(“PlayerTarget Successfully changed”);
//Get health script from targets
Health healthScript = target.GetComponent();
//Deal damage
healthScript.health -= damage;
//If HP <0 make it 0
if (healthScript.health < 0){
healthScript.health = 0;
}
//Send information about HP to bar
healthBarScript.health = healthScript.health;
healthBarScript.healthMax = healthScript.healthMax;
//Show bar
healthBarScript.showBar = true;
}
}
attackTimer = coolDown;
}
}
}
}
}
May be some one find it useful))
Good job finding the solution by yourself, and thanks for sharing!