[Urgent] Finding Nearest Target
Hello as the title said this is urgent! I need to finish up the ai for my game before Next WEEK!! I need help getting the ai to find the nearest player. This is my basic code :
var Distance;
var Target : Transform;
var lookAtDistance = 25.0;
var attackRange = 15.0;
var moveSpeed = 5.0;
var Damping = 6.0;
function Update ()
{
Distance = Vector3.Distance(Target.position, transform.position);
if (Distance < lookAtDistance)
{
renderer.material.color = Color.yellow;
lookAt();
}
if (Distance > lookAtDistance)
{
renderer.material.color = Color.green;
}
if (Distance < attackRange)
{
renderer.material.color = Color.red;
attack ();
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
function attack ()
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
I also found this code that unity provides in their documentation:
function FindClosestEnemy () : GameObject {
// Find all game objects with tag Enemy
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("Enemy");
var closest : GameObject;
var distance = Mathf.Infinity;
var position = transform.position;
// Iterate through them and find the closest one
for (var go : GameObject in gos) {
var diff = (go.transform.position - position);
var curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closest = go;
distance = curDistance;
}
}
return closest;
}
Now i tried putting these to gether myself no luck :(. if you can, can you PLEASE help me!!!
Hey guys just as quick question, I'm chipping through it and almost have it but I keep getting an error when I try to convert the game object to a transform. I have attached the code and error. thank you in advance if you can help
var Distance;
var findClosest : GameObject[];
var closest : GameObject;
var lookAtDistance = 25.0;
var attackRange = 15.0;
var moveSpeed = 5.0;
var Damping = 6.0;
var Target : Transform;
function Update ()
{
findClosest = GameObject.FindGameObjectsWithTag("Player");
Target = Vector3 = closest.transform.position;
FindClosestEnemy();
Distance = Vector3.Distance(Target.position, transform.position);
if (Distance < lookAtDistance)
{
GetComponent.<Renderer>().material.color = Color.yellow;
lookAt();
}
if (Distance > lookAtDistance)
{
GetComponent.<Renderer>().material.color = Color.green;
}
if (Distance < attackRange)
{
GetComponent.<Renderer>().material.color = Color.red;
attack ();
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
function attack ()
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
function FindClosestEnemy () : GameObject {
// Find all game objects with tag Enemy
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("Player");
var distance = $$anonymous$$athf.Infinity;
var position = transform.position;
// Iterate through them and find the closest one
for (var go : GameObject in gos) {
var diff = (go.transform.position - position);
var curDistance = diff.sqr$$anonymous$$agnitude;
if (curDistance < distance) {
closest = go;
distance = curDistance;
}
}
return closest;
}
Error: Assets/AI Stuff/Scripts/Ai Simple Test.js(14,18): BCE0049: Expression 'Vector3' cannot be assigned to.
Answer by MegaUltimateHD · Jul 07, 2016 at 10:51 AM
FIXED IT!!!!! It was a pretty simple thing to do but i am a noob when it comes to coding anyway
'var Distance;
var findClosest : GameObject[];
var closest : GameObject;
var lookAtDistance = 25.0;
var attackRange = 15.0;
var moveSpeed = 5.0;
var Damping = 6.0;
function Update ()
{
FindClosestEnemy();
Distance = Vector3.Distance(closest.transform.position, transform.position);
if (Distance < lookAtDistance)
{
GetComponent.<Renderer>().material.color = Color.yellow;
lookAt();
}
if (Distance > lookAtDistance)
{
GetComponent.<Renderer>().material.color = Color.green;
}
if (Distance < attackRange)
{
GetComponent.<Renderer>().material.color = Color.red;
attack ();
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(closest.transform.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
function attack ()
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
function FindClosestEnemy () : GameObject {
// Find all game objects with tag Enemy
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("Player");
var distance = Mathf.Infinity;
var position = transform.position;
// Iterate through them and find the closest one
for (var go : GameObject in gos) {
var diff = (go.transform.position - position);
var curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closest = go;
distance = curDistance;
}
}
return closest;
}
Answer by Derenger21 · Jul 04, 2016 at 11:41 AM
I boggled my mind on find nearest target for weeks until I was shown a VERY easy solution.
Unfortunately I don't know java so unsure if I can help.
call a list of all possible targets, then run a loop
for (int i = 0; i < targets.Length; i++) {
//reset closest dist to ensure all targets accounted for.
closestDist = 100f;
//measure distance between self and target object in list per i.
float dist = Vector3.Distance (gameObject.transform.position, targets[i].transform.position);
//compare distance, if distance < last closest distance measured, new closest set.
if (dist < closestDist) {
closestDist = dist;
newTarget = targets [i].gameObject;
}
}
I had the benefit of knowing all objects would be within 100f, also I only fire this line if the AI has no current target, etc.
basically each time an object is closer than the last checked object, it changes target to that and sets the 'record' for closest distance.
sorry for C#. hope you can translate.
Thanks! i do know a little bit of C# so i will try to translate
Your answer
Follow this Question
Related Questions
How do I find a player for AI to target when spawning a player in? 0 Answers
Null Reference Exception for target in a script on a newly instantiated object 0 Answers
Smooth Rotation Quaternion Lerp? 1 Answer
How to automatic reference another GameObject Transform? 1 Answer
If two Vector2 are same 1 Answer