Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by PrimeDerektive · Apr 27, 2010 at 08:37 PM · movementcharactercontrollerpauseyield

pause charactercontroller movement briefly?

So I set up a simple waypoint script, where my AI patrols several waypoints, and when it reaches a waypoint, it rotates towards and moves to the next waypoint.

All I want to do is make it so that when the AI gets to a waypoint, he pauses for a moment, and THEN rotates and moves to the next waypoint. I've tried a couple things, like setting up timers and using WaitForSeconds, but I can't figure it out. Anyway, here is my script in its current state:

var waypoints : Transform[]; var moveSpeed = 3.0; var rotationSpeed = 3.0; var waypointStopRange = 1.0;

private var currentWaypoint : int;

function Update(){

 PatrolWaypoints();

}

function PatrolWaypoints(){

 if(currentWaypoint < waypoints.length){

     var waypointDistance = Vector3.Distance(transform.position, waypoints[currentWaypoint].position);
     var waypointDirection = waypoints[currentWaypoint].position - transform.position;

     if(waypointDistance <= waypointStopRange){
         currentWaypoint++;
     }
     else{
         RotateTowards(waypointDirection);
         MoveForward();
     }

 }
 else{

     if(waypointLoop){
         currentWaypoint = 0;
     }

 }

}

function RotateTowards (position : Vector3) {

 position.y = 0;
 var targetRotation = Quaternion.LookRotation(position);
 transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);

}

function MoveForward(){

 var forward = transform.TransformDirection(Vector3.forward);
 var moveDirection = forward * moveSpeed;
 GetComponent (CharacterController).SimpleMove(moveDirection);

}

I've been trying to do the pause inside of if(waypointDistance <= waypointStopRange), because that is when he has reached currentWaypoint, and when currentWaypoint is incremented to the next. I cannot for the life of me figure out how to get him to stop moving for a few moments there. If I use yield WaitForSeconds(), he will just get stuck there indefinitely.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Peter G · Apr 27, 2010 at 11:10 PM

I would rather than calling the Patrol function from update, call it from a start function similar how the FPS tutorial does it. Then yield after every check and once the character reaches his waypoint, then you can use WaitForSeconds() without any issue.

//Simplified to show the idea. function Start () { PatrolWaypoints(); }

function Patrol () { while(true) { if(currentWaypoint < waypoints.length) { if( waypointDistance <= wayPointStopRange) { currentWaypoint++ yield WaitForSeconds(waitTime); }

      else{
          RotateTowards(waypointDirection);
          MoveForward();
      }
 }
 else{

     if(waypointLoop){
         currentWaypoint = 0;
     }

 }
  yield;

}

function RotateTowards (position : Vector3) { }

function MoveForward() { }

The script above looks very similar to the one you wrote so there would not be much to change. I find that state based behavior usually works better from system similar to above because then each state just calls the next instead of a central Update trying to figure it out.

Comment
Add comment · Show 2 · 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 PrimeDerektive · Apr 28, 2010 at 02:48 PM 0
Share

Why is it that yield WaitForSeconds would behave differently in Start() versus Update()?

Also, I removed it because it wasn't relevant to my question, but inside patrol() I also do my checks for distance to the player, and run the attack function if hes within range. Would that be effected if I moved Patrol() to Start()?

avatar image PrimeDerektive · Apr 28, 2010 at 06:52 PM 0
Share

Nice! Never$$anonymous$$d I figured it out. I can't say I fully understand how coroutines work, but at least now I can use them :)

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

controller.Move not working correctly 0 Answers

how to not move on the x axis while not jumping 2 Answers

Is using an RFID as a Character controller possible? 0 Answers

Player moves diagonally even when not telling him to 0 Answers

Character Controller - unknown rotation, flying 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