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 subzul · Aug 08, 2012 at 12:11 PM · aiwaypointpatrol

AI will complete patrolling and wait for 5 seconds

Hi!

I have a question. I have this script:

 enter code here
 var Waypoint : Transform[];
 var loop : boolean = true;
 var speed : float = 20;
 private var currentWaypoint : int;
 private var timer = 0;
 function Awake()
 {
   Waypoint[0] = transform;
 
 }
 function Update()
 {
 if(currentWaypoint < Waypoint.length)
 {
 var target : Vector3 = Waypoint[currentWaypoint].position;
 var move_direction : Vector3 = target - transform.position;
 var velocity = rigidbody.velocity;if(move_direction.magnitude < 1)
 {
 currentWaypoint++;
 }else{
 velocity = move_direction.normalized * speed;
 }
 }
 else{
 if(loop)
 {
  currentWaypoint = 0;
 }else{
 velocity = Vector3.zero;
 }
 } 
 rigidbody.velocity = velocity;

}

How can i add timer to it? Like this. AI will go through all waypoints and then stops for 5 seconds and then go through those waypoints again and then again stops for 5 seconds and do it again in loop.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Griffo · Aug 08, 2012 at 12:17 PM

Hope this helps, put the number of way points you want to use in the inspector then drag your waypoints in to the inspector.

 #pragma strict
 
 var waypoint : Transform[]; // The amount of Waypoint you want
 var patrolSpeed : float = 3; // The walking speed between Waypoints
 var loop : boolean = true; // Do you want to keep repeating the Waypoints
 var player : Transform; // Referance to the Player
 var dampingLook = 6.0; // How slowly to turn
 var pauseDuration : float = 0; // How long to pause at a Waypoint
 
 private var curTime : float;
 private var currentWaypoint : int = 0;
 private var character : CharacterController;
 
 function Awake(){
 
 }
 
 function Start(){
 
     character = GetComponent(CharacterController);
 }
 
 function Update(){
 
  if(currentWaypoint < waypoint.length){
  patrol();
  }else{ 
  if(loop){
  currentWaypoint=0;
         } 
  }
 }
 
 function patrol(){
 
         var target : Vector3 = waypoint[currentWaypoint].position;
         target.y = transform.position.y; // Keep waypoint at character's height
         var moveDirection : Vector3 = target - transform.position;
 
  if(moveDirection.magnitude < 0.5){ // If this number is 1 the character will jerk the last bit to the waypoint and not be over it
  // any lower and the character will get stuck for a second over the waypoint on the iPhone
  if (curTime == 0)
  curTime = Time.time; // Pause over the Waypoint
  if ((Time.time - curTime) >= pauseDuration){
  currentWaypoint++;
  curTime = 0;
  }
  }else{        
  //transform.LookAt(target); // Use this instead of below to look at target without damping the rotation
  // Look at and dampen the rotation
  var rotation = Quaternion.LookRotation(target - transform.position);
  transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingLook);
  character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime);
  } 
 }
Comment
Add comment · Show 6 · 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 subzul · Aug 08, 2012 at 12:48 PM 0
Share

Nope. That doesnt work. when i hit play that cube just stands there and isnt doing anything. here is my inspector: http://imageshack.us/photo/my-images/94/patrolinspector.png/

avatar image Griffo · Aug 08, 2012 at 12:57 PM 0
Share

Sorry forgot to say add a character controller to the cube, replace the box collider ...

avatar image subzul · Aug 08, 2012 at 01:04 PM 0
Share

Sorry, still nothing and i replaced box collider with character controller. but when i hit play nothing happens.

avatar image Griffo · Aug 08, 2012 at 01:08 PM 0
Share

Have a look at my pic in the answer further up, I've just made a new scene with a cube with a character controller and 3 empty game objects for my waypoints and dragged them into the inspector and it works fine .. with the script dragged onto the cube ..

I've added the .js file to the above answer ..

avatar image subzul · Aug 08, 2012 at 01:23 PM 1
Share

Thanks a lot. It works perfectly now.

Show more comments
avatar image
0

Answer by Griffo · Aug 08, 2012 at 01:06 PM

Sorry forgot to say add a character controller to the cube, replace the box collider ...

link text


inspector.jpg (35.9 kB)
waypionts.js.zip (911 B)
Comment
Add comment · 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

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

free roaming enemy ai 1 Answer

iTween Help Please MoveTo not working 1 Answer

how to detect if transform.lookat is going through walls? 1 Answer

Send an enemy back to its spawn point using waypoints 2 Answers

AI patrolling 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