Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Michael 12 · Mar 27, 2011 at 05:02 PM · aiwaypointsautomaticfleeing

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 &lt; scaredDistance)
 {
     status = AIStatus.Scared;
 }

 else if (dist &gt; 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 &gt; bestDot &amp;&amp; cur != currentWaypoint) {
         bestDot = dot;
         best = cur;
     }
 }

 return best;

}

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Meltdown · Mar 27, 2011 at 05:32 PM 1
Share

Have you tried calling Wander() in your idle() function?

avatar image Michael 12 · Mar 27, 2011 at 05:53 PM 0
Share

Not really sure what you mean $$anonymous$$eltdown, I'm not very good with code so I could use a little more explaination ;)

avatar image Meltdown · Mar 27, 2011 at 06:00 PM 0
Share

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.

avatar image Michael 12 · Mar 27, 2011 at 07:16 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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 &lt; scaredDistance)
 {
     status = AIStatus.Scared;
 }

 else if (dist &gt; 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 &gt; bestDot &amp;&amp; cur != currentWaypoint) {
         bestDot = dot;
         best = cur;
     }
 }

 return best;

}

This is what Meltdown meant. Try it see if it works.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Michael 12 · Mar 27, 2011 at 07:14 PM 0
Share

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?

avatar image Justin Warner · Mar 27, 2011 at 07:32 PM 0
Share

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.

avatar image Michael 12 · Mar 27, 2011 at 07:45 PM 0
Share

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. :(

avatar image Justin Warner · Mar 27, 2011 at 08:06 PM 0
Share

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.

avatar image Michael 12 · Mar 27, 2011 at 09:34 PM 0
Share

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".

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges