- Home /
I'm still having Trouble with this AI script
OK I've made some changes to my Alien Zombie AI script but I still can't seem to get it to function the way it sould. Right now my Alien Zombies will follow me arround like lost puppies if I get within their attack range. And when I get out of range instead of returning to their patrol routine or even going back to just plain "Idle" they kind of run on the spot doing their "RunAttack" animation?? It's cufuddling me buggy trying to figure out why they are doing that.
Also I can't figure out how to get my Alien Zombie to inflict damage to my FPS player using his "Attack" animation when he is right in front of him or right up close if you like??
Here is my code so far: Sorry for posting this same question only asked differently but they seem to get lost after a while and get no more responses.
var speed = 3.0; var rotationSpeed = 5.0; var attackRange = 30.0; var dontComeCloserRange = 5.0; var pickNextWaypointDistance = 2.0; var target : Transform; var modelAnimation : Animation;
// 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("Player")) target = GameObject.FindWithTag("Player").transform;
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 (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 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 > attackRange * 3)
return;
lastVisiblePlayerPosition = target.position;
if (distance > dontComeCloserRange)
MoveTowards (lastVisiblePlayerPosition);
//Attack player with "Attack" animation which is basically a clawing and biting animation
//this "Attack" is supposed to inflict damage to the FPS player
else {
modelAnimation.CrossFade("Attack");
RotateTowards(lastVisiblePlayerPosition);
}
var forward = transform.TransformDirection(Vector3.forward);
var targetDirection = lastVisiblePlayerPosition - transform.position;
targetDirection.y = 0;
var angle = Vector3.Angle(targetDirection, forward);
}
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;
}
This is barely an answer, but I would check out what Speed$$anonymous$$odifier is doing with a Debug.Log. That's mainly because I don't really understand or trust "dot". If you totally know what that's doing, disregard. Also, "while (true) {" sticks out as something I wouldn't write. Have several boolean variables declared at the top and check them;
var patrolling : System.Boolean = true;
while (patrolling) {
I think you need to have distinct modes for your enemy and maybe now you don't. That is, either it's patrolling, or it's chasing the player, or it's attacking, but make sure it's never trying to do more than one thing at a time. Right now it may be running more than one of those functions at the same time which causes the 'lost puppy' behavior.
Is this just an edit of the FPS tutorial robot script?
To make the attack animation do damage I could write a little something that would work for the robot of that tutorial. I would attach a ball collider to this hand that did damage to things inside when it was "fired"
Answer by Owen-Reynolds · Mar 22, 2011 at 04:45 PM
The animation can fool you -- for example, your code may be correctly not moving for a while, but the runAttack animation you started 6 seconds ago is still playing, so you think the code is trying to run.
Maybe leave animation off, so you can focus on the movement and turning. Once the code works, there are usually very obvious places to set animation. For example, whenever the code sets direction to 0, also play "idle."
For checking the move code, these are two old programming tricks, Unity style:
o Add things like Debug.Log("Back to patrol from attack");
so you can see if the flow is correct.
o Show some vars in the inspector: Make public var ShowWayPnt : Transform;
and set it in Patrol: ShowWayPnt = waypointPosition;
This will tell you if the stall is because you're on the Waypoint already, or if direction
is somehow always set to 0 (do the same thing to show direction
.)
Eventually, you'll get a result that isn't what you expected, which leads to the problem.
Thanks Pat for the awesome advice. But looking at my code can you spot any what might be obvious errors to someone far more code savy than I? I've been checking the Unity Animation script reference but I can't seem to get much headway from that. I'm such a "noob" ;)
Do the animation later, after the motion is done. Focus on why it won't return to the waypoint after attacking. If you have zero animation, it will be much easier to see if it is stopped, or just moving very slowly (for example.) I didn't spot any obvious problems.
I'm not sure what's going on inside of this thing. I've noticed that sometimes when I'm out of range they will wander back to their waypoint and sometimes they just sort of "Run on the Spot" at the last place where I sudenly bacame out of range. Plus, and this is the REALLY big pain in the ass, is I have STILL NOT been able to figure out haw to make these guys actually attack my player and inflict damage. It's getting to the pount where I'm just going to say "Frak It!" and just give them weapons and make a gun fire animation and just do it that way which is TOTALLY NOT what I want! :(
Test until you see a pattern -- maybe they are O$$anonymous$$ if they lose you out of attack range, but get stuck going from attack range to out-of-sight. That will narrow down the problem. Highlight the enemy and watch what the variables are. For attack, break it down. Do 1 point of damage every frame you are in range. Then think how to wait in between damage ticks (another coroutine? A timeLeftToattack
variable?)
How does that work with his "Attack" animation though. Heck I don't really understand how this script call the other animations into play. That FPS tutorial does not get into HOW that AI script works they just say attach this tou your robot and then do this and that and the other things and Whallah! Your robot works. There is an AI animation script and I guess they somehow talk to each other but where and how I don't understand. Right now this is a $$anonymous$$AJOR stumbling block for me been searching all over hells 1/2 acre trying to find answers too.
Answer by LukaKotar · Dec 11, 2011 at 10:49 AM
Try setting rotation speed a little bit higher, if they spin around you in circles. I hope this answers your question! ;)
Your answer
Follow this Question
Related Questions
Ai Zombie Melee Attack script. 5 Answers
Not identifying the collider? 2 Answers
How to make an animation play for AI? 0 Answers
Melee attacking, using force and health values 1 Answer
2 Animations At once 0 Answers