Hello im trying to make a game that has a fighting system like oblivion. I am trying to make it so any enemy within a certain distance of the player looses health i DON'T want it to be like most mmorpg's where they target enemys i want it to be base on skill.
Heres my script for PlayerAttack:
var target : GameObject;
var attackTimer : float;
var coolDown : float;
var damage : float = 10;
// Use this for initialization
function Start () {
attackTimer = 0;
coolDown = 1.0f;
}
// Update is called once per frame
function Update () {
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if (attackTimer < 0)
attackTimer = 0;
if(Input.GetKeyUp(KeyCode.F)) {
if(attackTimer == 0){
Attack();
attackTimer = coolDown;
}
}
}
function Attack() {
var distance = Vector3.Distance(target.transform.position, transform.position);
var direction = Vector3.Dot((target.transform.position - transform.position).normalized, transform.forward);
Debug.Log(direction);
if(distance < 3) {
if(direction > 0) {
target.GetComponent(EnemyHealth).AddjustCurrentHealth(-damage);
}
}
}
the scripts currently work fine but I want it to be adapted to muliple enemys.
I want it to find `GameObject.tag == "Enemy";` within a distance of 3 or something and is in the direction im facing or something along those lines.
Can someone please help thanks.
It would be helpful if you described what behaviour your seeing now? Does this currently not work? Or would you want the code adapted for multiple enemies?
What you want to do is set the tag "Enemy" for all your enemies.
In your PlayerAttack you find all gameobjects with the tag enemy (Save them into an array), then you find the distance between your player and your enemies (Save the distance in the array above as well).
Select the enemy who has the lowest distance and attack him.
var target = closest;
var closest : GameObject;
function Update(){
target = closest;
// Find all game objects with tag Enemy
var enemies : GameObject[];
enemies = GameObject.FindGameObjectsWithTag("Enemy");
var distance = Mathf.Infinity;
var position = transform.position;
// Iterate through them and find the closest one
for (var enemy : GameObject in enemies) {
var diff = (enemy.transform.position - transform.position);
var curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closest = enemy;
distance = curDistance;
}
}
return closest;
}
PlayerAttack Script
var target : GameObject;
var attackTimer : float;
var coolDown : float;
var damage : float = 10;
// Use this for initialization
function Start () {
attackTimer = 0;
coolDown = 1.0f;
}
// Update is called once per frame
function Update () {
target = GetComponent(Targeting).target;
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if (attackTimer < 0)
attackTimer = 0;
if(Input.GetKeyUp(KeyCode.F)) {
if(attackTimer == 0){
Attack();
attackTimer = coolDown;
}
}
}
function Attack() {
var distance = Vector3.Distance(target.transform.position, transform.position);
var direction = Vector3.Dot((target.transform.position - transform.position).normalized, transform.forward);
Debug.Log(direction);
if(distance < 3) {
if(direction > 0) {
target.GetComponent(EnemyHealth).AddjustCurrentHealth(-damage);
}
}
}
It would be helpful if you described what behaviour your seeing now? Does this currently not work? Or would you want the code adapted for multiple enemies?
– GesterX