- Home /
Two player cube battle can't attack at the same game.
Hi, i'm trying to make 2 cube fighting each other, then i try to make it a two player mode. then i decide to input w a s d for player one movement and space for it attack. the player two using key up down right left and using L key to attack. the problem is, each time i try to test play, only one player can do attack ( i don't know why) then after that i try to test play once again, another player which can't attack before, now can attack, and the player can do attack before, it was unable to attack anymore. this is my script for attack and health: using UnityEngine; using System.Collections;
public class PlayerAttack : MonoBehaviour {
public GameObject target;
public float attackTimer;
public float coolDown;
// Use this for initialization
void Start () {
//ngitung timer serangan
attackTimer = 0;
//cooldown serangan
coolDown = 2f;
}
// Update is called once per frame
void Update () {
if (attackTimer > 0) attackTimer -= Time.deltaTime;
//pencet spasi
if (Input.GetKeyUp(KeyCode.Space))
{
//when attack timer reach 0
if (attackTimer <= 0)
{
//call attack
Attack();
//attack timer back to cooldown
attackTimer = coolDown;
}
}
}
//ngutak ngatik attack nya disini
private void Attack(){
//ngitung jarak serangan
float distance = Vector3.Distance (target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position).normalized;
float direction = Vector3.Dot (dir, transform.forward);
//jika jarak nya dibawah 2.5 maka enemy health kurangin 10
if (distance < 2.5f) {
if (direction > 0) {
EnemyHealth eh = (EnemyHealth)target.GetComponent ("EnemyHealth");
eh.AddjustCurrentHealth (-10);
}
}
}
}
Health:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerHealth : MonoBehaviour {
private int maxHealth = 100;
public int curHealth = 100;
public GameObject deathPraticles;
public float healthBarLength;
// Use this for initialization
void Start () {
//panjang si darah bar nya
healthBarLength = Screen.width / 2;
}
// Update is called once per frame
void Update () {
AddjustCurrentHealth (0);
}
//urusan GUI darah
void OnGUI(){
GUI.Box(new Rect(10, 10, healthBarLength, 20 ), curHealth + "/" + maxHealth);
}
//Setting Cur Health disini
public void AddjustCurrentHealth(int adj){
//lolol. ngeliat kemungkinan yg terjadi
curHealth += adj;
if (curHealth > maxHealth)
curHealth = maxHealth;
if (maxHealth < 1)
maxHealth = 1;
//ngitung panjang darah
healthBarLength = (Screen.width / 4) * (curHealth / (float)maxHealth);
if (curHealth <= 0) {
curHealth = 0;
Instantiate(deathPraticles, transform.position, Quaternion.identity);
Destroy (gameObject);
}
}
}
im trying to figure it out for 8 hours straight. any answer will be help! thanks!
Your answer
Follow this Question
Related Questions
Player attack script + enemy health 1 Answer
Attack,Health and enemy health. 1 Answer
I need help with my health script 2 Answers
Atack and HP 'universal' scripts, begginer need guidance. 1 Answer