x


Locking onto Multiple AI's

I'm making a hack n slash. The lock on system I'm currently using will focus the camera on both the character it follows and the enemy click by the mouse. The problem here is that because the camera searches by tag, it will only lock on to the closest enemy. Okay so the way I've fixed this is by making different tags entitled enemy1, enemy2, enemy3, etc. and scripts saying if they've been clicked or not. Is there at all an easier way to do this by just having 1 "enemy" tag? I basically want it so that whichever enemy I click, the camera will follow. The current way Im doing this is very tedious because of the repetition of nearly the exact same scripts.

I won't post the entire codes because they're really long, but I'll give a somewhat basic representation.

3 Enemies in the scene, respectively tagged 1,2, and 3.

On tag Enemy1:

static var HasBeenClicked:boolean = false;

function OnMouseDown()
{
    Enemy1.HasBeenClicked = true;
    Enemy2.HasBeenClicked = false;
    Enemy3.HasBeenClicked = false;
}

On the Camera to focus on the enemy:

var damp = 20;
static var Ready:boolean = true;
var rotate;

function Update () 
{
    if(Enemy1.HasBeenClicked == true)
    {
        rotate = Quaternion.LookRotation(GameObject.FindWithTag("enemy1").transform.position - transform.position);
        transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
    }
    if(Enemy2.HasBeenClicked == true)
    {
        rotate = Quaternion.LookRotation(GameObject.FindWithTag("enemy2").transform.position - transform.position);
        transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
    }
    if(Enemy3.HasBeenClicked == true)
    {
        rotate = Quaternion.LookRotation(GameObject.FindWithTag("enemy3").transform.position - transform.position);
        transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
    }
}
more ▼

asked Mar 01 '11 at 10:37 PM

Xatoku gravatar image

Xatoku
149 74 82 88

(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

What I'd do is create an array holding reference to all enemy GameObjects. I guess a simple way would be to use:

var allEnemies = FindObjectsOfType(enemyScript).gameObject;
var myTarget:GameObject;

then, in the script catching input, simply set myTarget to the enemy you clicked. Finally,have the camera focus on myTarget.

Plenty of better way, but that's the first I can think of :)

EDIT: Long story short, hold a single target reference rather than have plenty of on/off switch to parse through.

more ▼

answered Mar 02 '11 at 12:24 AM

by0log1c gravatar image

by0log1c
2.1k 6 9 18

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3124
x670
x319
x129
x72

asked: Mar 01 '11 at 10:37 PM

Seen: 708 times

Last Updated: Mar 01 '11 at 10:37 PM