- Home /
targeting problems
basically i start the game but i have problem with targeting game objects
wondering if anyway can find any problem the mobs are spawned into the game so would that impact anything? and what needs to happen is they spawn in and they then select a defense out of the 4 defenses available once it has 0 hp then they move on onto the base but if the character gets in the way it will change to that target
using UnityEngine; using System.Collections;
public class MobAiEast : MonoBehaviour {
public float preceptionradius = 10;
public float BaseMeleeRange = 4;
public Transform target;
public float Nill = 0;
public Transform _myTransform;
public Transform _east;
public Transform _base;
public Transform _player;
public GameObject East;
public int random;
public GameObject[] Defense;
void Start () {
random = Random.Range(0, 5);
Defense = GameObject.FindGameObjectsWithTag("east");
CharacterController cc = GetComponent<CharacterController>();
if(cc == null) {
Debug.LogError("no controller");
}
else{
}
East = Defense[random];
_myTransform = transform;
GameObject Player = GameObject.FindGameObjectWithTag("player");
GameObject Base = GameObject.FindGameObjectWithTag("Base");
_east = East.transform;
_base = Base.transform;
_player = Player.transform;
_east = target;
}
void Update () {
Defense other = East.GetComponent<Defense>();
if(other.CurHealth == 0 || other.CurHealth < 0) {
if(Vector3.Distance(transform.position, _player.transform.position) < 8) {
target = _player;
}
else
target = _base;
}
else
target = _east;
if(target){
goes on with moving to target stuff (can ignore i know it works :P)
thanks for any help!
Answer by fafase · Apr 25, 2012 at 06:45 AM
I have here : http://users.metropolia.fi/~lucasc/AiEnemy.js
an AI script that I think would do what you are looking for.
It is a simplified version as it does not use the CC (it should though...and maybe will later when I have time)
The script has 4 waypoints that are randomly assigned as target. When the NPC reaches a target a new one is given until the player gets inside a range then the player becomes the target until he escapes the range. It is in Js and is pretty simple.
So you shoul dbe able to find what you need.
had a look at the script seems pretty good but id have to chnage it to c# cause my script connects to other scripts for example my attack script for the mob uses the targets from this script to decrease their health You script is very well put together. maybe i should just use this script and add the attack stuff into the script? i would still be more comfortable if i put it into c#
It shouldn't be too much of a hassle since you only need the main idea of it. Since it does not have any CC, the guys would go through walls...this script would be fine on a field. $$anonymous$$aybe I will get back to it and make it better. Thanks for the comment.
ill get back to you when i have finished the changes but it will be a few hours got other things to do :/ ... i might try and get out of work and do it haha!
By the way, I have improved my script if you need it. Still in Js though...
Your answer
![](https://koobas.hobune.stream/wayback/20220613070718im_/https://answers.unity.com/themes/thub/images/avi.jpg)