- Home /
Help With Waypoints script
OK I'm trying to get a neutral Alien Zombie to return to wandering about his way point positions. Right now he does s good job of running away from my FPS player which is good. But what I want should for somereason my player descides NOT to shoot him, that he will return to wandering about his way points. But right now he's NOT, he'll just stop running when out of range and just Idle in one place.
EDIT: Another issue I just discovered is that sometimes when I chase him from off of the top of the hill he ends up, up in the air almost like gravity is not affecting him???
Here is my script I'm using:
var modelAnimation : Animation; var awareDistance : float = 25.0; var scaredDistance : float = 20.0; var target : Transform; var pickNextWaypointDistance = 2.0;
var runSpeed : float = 15.0;
enum AIStatus {idle = 0, Scared = 1} private var status = AIStatus.idle;
var controller : CharacterController;
private var moveDirection = Vector3.zero;
function Awake() { controller = GetComponent(CharacterController); }
function Wander () { 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); }
yield;
}
}
function Update() { CheckStatus();
switch(status)
{
case AIStatus.idle:
idle();
break;
case AIStatus.Scared:
RunAway();
break;
}
}
function idle() { modelAnimation.CrossFade ("idle"); }
function RunAway() { transform.eulerAngles.y = target.transform.eulerAngles.y; moveDirection = Vector3(0,0,40);
moveDirection = transform.TransformDirection(moveDirection); moveDirection = runSpeed; controller.SimpleMove(moveDirection Time.deltaTime);
modelAnimation.CrossFade ("RunAttack");
}
function CheckStatus() {
var dist = (target.position - transform.position).magnitude;
if(dist < scaredDistance)
{
status = AIStatus.Scared;
}
else if (dist > awareDistance)
{
status = AIStatus.idle;
}
}
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;
}
Have you tried calling Wander() in your idle() function?
Not really sure what you mean $$anonymous$$eltdown, I'm not very good with code so I could use a little more explaination ;)
Does he idle along his waypoints correctly to begin with? And then run when you get near him? Sorry the script is a little long to know 100% if everything is working.
he does now walk his waypoints at all, he just idles in one place until I chase him and if I stop chasing him he will just stop and idle.
Answer by Justin Warner · Mar 27, 2011 at 06:00 PM
var modelAnimation : Animation; var awareDistance : float = 25.0; var scaredDistance : float = 20.0; var target : Transform; var pickNextWaypointDistance = 2.0;
var runSpeed : float = 15.0;
enum AIStatus {idle = 0, Scared = 1} private var status = AIStatus.idle;
var controller : CharacterController;
private var moveDirection = Vector3.zero;
function Awake() { controller = GetComponent(CharacterController); }
function Wander () { 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); }
yield;
}
}
function Update() { CheckStatus();
switch(status)
{
case AIStatus.idle:
idle();
break;
case AIStatus.Scared:
RunAway();
break;
}
}
function idle() { modelAnimation.CrossFade ("idle"); wander(); }
function RunAway() { transform.eulerAngles.y = target.transform.eulerAngles.y; moveDirection = Vector3(0,0,40);
moveDirection = transform.TransformDirection(moveDirection); moveDirection = runSpeed; controller.SimpleMove(moveDirection Time.deltaTime);
modelAnimation.CrossFade ("RunAttack");
}
function CheckStatus() {
var dist = (target.position - transform.position).magnitude;
if(dist < scaredDistance)
{
status = AIStatus.Scared;
}
else if (dist > awareDistance)
{
status = AIStatus.idle;
}
}
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 what Meltdown meant. Try it see if it works.
I had to change your w to capital W otherwise I got an unknown identifier error, but he still just stopes after being chased and just idles?
I believe the problem is, you're never making the thing that uses the waypoints to actually $$anonymous$$OV$$anonymous$$ Like, it is getting the next closest Waypoint, but it never is actually told to GOTO the new waypoint... So somewhere in the script you're going to have to have the object $$anonymous$$OVE to the waypoint... If that makes sense, all it's doing is finding one, and doesn't do anything with the info. And sorry about the W, didn't read the script before haha.
Thanks Justin, but that of course is the very nature of my question, is what and where do I need to place that, I'm guessing it's an If statement maybe but I'm not sure how to code it or where in here it sould go. Also I edited my question with something else troubling I discovered about this script... seems there is always something. :(
Get the position of the new waypoint, and have the object move towards that waypoint. Just search around for that: http://www.google.com/search?client=opera&rls=en&q=move+an+object+towards+a+point+at+a+certain+time+unity3d&sourceid=opera&ie=utf-8&oe=utf-8&channel=suggest As for the weird activity, you may want to change the speed, make sure it has a rigid body, and if that doesn't work, you might have to play around with what you have... Not to certain, maybe give it more mass, as the default is 1, and that's 1 $$anonymous$$G... which is nothing, haha.
Thanks Justin, I'll look into some of that stuff. I can't give him a ridged body though because he already has a character controller and if I add a ridgedbody he does not work right. Takes too long to explain but because of the way my characters were exported out of $$anonymous$$AX 8 the yeach had to be placed inside of an empty game object in order for them to work properly otherwise I ended up with "Swim$$anonymous$$g Zombie".
Your answer
Follow this Question
Related Questions
FPS Waypoints not working right 1 Answer
make an AI Flee 0 Answers
Make player follow a waypoint path but still be able to control player movement 1 Answer
AI Car Waypoints - Odd Behaviour... 1 Answer
Need Help With My AI Script 1 Answer