Hi, i have been a lurker of this forum for a while until i finally decided to do something, my fist project is kind of a DOTA, where my group of minions battle the army of my enemy ( this is done automatically without input on game) and i control my character in fps. So obviously im using and modifying the scripts from the FPS tutorial.
First of all i must say that while i know a bit of programing im not that good yet and some obvious things may scape to me. I found many problems when adapting the scripts to my project:
A- Waypoint system: The robot (model for testing im using) almost never choose the nearest waypoint to go to. I resolved this by blocking the lines used to connect waypoints leaving only one possible route. Still one problem remains which i explain in next point. (maybe this is related to the terrain being too big? 1000*1000 units)
B- AI script: the script is made just to look for the player or something tagged as player, so i used the tagging system to make the different sides kill each other. I manage this by adapting the FindGameObjectsWithTag found in documentation but many problems arise from here:
1- I have to harcode the tags im using in the script (enemy, friendly) is there a way to make them selectable from the inspector view? (like the use of “var target : Transform”) i cant find the way. This forces me to have 2 differents but almost identical scripts for ai (ai friendly, ai enemy) just changing the tag references.
2- Very oddly, the 2 AI scripts dont work together, one of the sides will never recognise any enemy and run past along without noticing them like a headless chicken under a bombing rain, this made me to use the original AI from the tutorial, wich main problem is that it will only choose the first tagged gameobject it finds and run to it while ignoring anything else, nor enemies or distance to them on the way (just a pitiful improvement over the former).
3- When the properly working AI kills and opponent and theres no other around, sometimes it decides to either stop or run back to its previous waypoint route instead of following the correct next waypoint.
This is the code:
var speed = 3.0;
var rotationSpeed = 5.0;
var shootRange = 15.0;
var attackRange = 30.0;
var shootAngle = 4.0;
var dontComeCloserRange = 5.0;
var delayShootTime = 0.35;
var pickNextWaypointDistance = 2.0;
var target : Transform;
private var lastShot = -10.0;
// Make sure there is always a character controller
@script RequireComponent (CharacterController)
function Start () {
// Auto setup player as target through tags
if (target == null GameObject.FindWithTag("friendly"))
//Var for getting all tagged objects
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("friendly");
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;
}
}
target = closest.transform;
//this down is the default way for doing it (FPS Tut script), while all above is what it should properly work
// but doesnt with the two scripts running together
//target = GameObject.FindWithTag("friendly").transform; <-- Just gets me one target, without checking
//distances or anything
Patrol();
}
function Patrol () {
var curWayPoint = AutoWayPoint.FindClosest(transform.position);
while (true) {
var waypointPosition = curWayPoint.transform.position;
// Are we close to a waypoint? -> pick the next one!
if (Vector3.Distance(waypointPosition, transform.position) < pickNextWaypointDistance)
curWayPoint = PickNextWaypoint (curWayPoint);
// Attack the player and wait until
// - player is killed
// - player is out of sight
if (CanSeeTarget ())
yield StartCoroutine("AttackPlayer");
// Move towards our target
MoveTowards(waypointPosition);
yield;
}
}
function CanSeeTarget () : boolean {
if ( target == null)
Start();
if (Vector3.Distance(transform.position, target.position) > attackRange)
return false;
var hit : RaycastHit;
if (Physics.Linecast (transform.position, target.position, hit))
return hit.transform == target;
return false;
}
function Shoot () {
// Start shoot animation
animation.CrossFade("shoot", 0.3);
// Wait until half the animation has played
yield WaitForSeconds(delayShootTime);
// Fire gun
BroadcastMessage("Fire");
// Wait for the rest of the animation to finish
yield WaitForSeconds(animation["shoot"].length - delayShootTime);
}
function AttackPlayer () {
var lastVisiblePlayerPosition = target.position;
while (true) {
if (CanSeeTarget ()) {
// Target is dead - stop hunting
if (target == null)
return;
// Target is too far away - give up
var distance = Vector3.Distance(transform.position, target.position);
if (distance > shootRange * 3)
return;
lastVisiblePlayerPosition = target.position;
if (distance > dontComeCloserRange)
MoveTowards (lastVisiblePlayerPosition);
else
RotateTowards(lastVisiblePlayerPosition);
var forward = transform.TransformDirection(Vector3.forward);
var targetDirection = lastVisiblePlayerPosition - transform.position;
targetDirection.y = 0;
var angle = Vector3.Angle(targetDirection, forward);
// Start shooting if close and play is in sight
if (distance < shootRange angle < shootAngle)
yield StartCoroutine("Shoot");
} else {
yield StartCoroutine("SearchPlayer", lastVisiblePlayerPosition);
// Player not visible anymore - stop attacking
if (!CanSeeTarget ())
return;
}
yield;
}
}
function SearchPlayer (position : Vector3) {
// Run towards the player but after 3 seconds timeout and go back to Patroling
var timeout = 3.0;
while (timeout > 0.0) {
MoveTowards(position);
// We found the player
if (CanSeeTarget ())
return;
timeout -= Time.deltaTime;
yield;
}
}
function RotateTowards (position : Vector3) {
SendMessage("SetSpeed", 0.0);
var direction = position - transform.position;
direction.y = 0;
if (direction.magnitude < 0.1)
return;
// Rotate towards the target
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);
}
function MoveTowards (position : Vector3) {
var direction = position - transform.position;
direction.y = 0;
if (direction.magnitude < 0.5) {
SendMessage("SetSpeed", 0.0);
return;
}
// Rotate towards the target
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);
// Modify speed so we slow down when we are not facing the target
var forward = transform.TransformDirection(Vector3.forward);
var speedModifier = Vector3.Dot(forward, direction.normalized);
speedModifier = Mathf.Clamp01(speedModifier);
// Move the character
direction = forward * speed * speedModifier;
GetComponent (CharacterController).SimpleMove(direction);
SendMessage("SetSpeed", speed * speedModifier, SendMessageOptions.DontRequireReceiver);
}
function PickNextWaypoint (currentWaypoint : AutoWayPoint) {
// We want to find the waypoint where the character has to turn the least
// The direction in which we are walking
var forward = transform.TransformDirection(Vector3.forward);
// The closer two vectors, the larger the dot product will be.
var best = currentWaypoint;
var bestDot = -10.0;
for (var cur : AutoWayPoint in currentWaypoint.connected) {
var direction = Vector3.Normalize(cur.transform.position - transform.position);
var dot = Vector3.Dot(direction, forward);
if (dot > bestDot cur != currentWaypoint) {
bestDot = dot;
best = cur;
}
}
return best;
}
Somehow i feel this is just product of some stupid error of mine, but i cant find it or the answer in the search of the forum. So i would be grateful if someone can clear my doubts. ![]()