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 Ninita · Feb 13, 2013 at 04:33 PM · movementtimerwaypointnpcmotion

Problem with MoveToWaypoint

Hi! In my game I want my NPC move from one point to another and therefore I created the following script based on an example I found on the web. But the NPC is not having a uniform motion, and doesn't stop for a while when it reaches the target position as I wanted. He is always rotating and moving around the map. Also, appears the error: "Script error: Update () can not be a coroutine." Someone can help me solve this problem please?

     var pointB : Vector3;
     private var pointA : Vector3;
     var moveSpeed : float = 10.0;
     
     var waitTime : float = 5.0;
     private var waitTimer : float;
     
     //enum State {Waiting, Moving}
     private var currentState : State;
     
     function Start () {
         pointA = transform.position;
         
         currentState = State.Moving;
     }
     
     function Update () {
         switch(currentState){
             case State.Waiting:
                 Wait();
                 break;
             case State.Moving:
                 if(transform.position==pointA)
                     MoveObject(transform, pointA, pointB, 3.0);
                 if(transform.position==pointB)
                     MoveObject(transform, pointB, pointA, 3.0);
                 break;
         }
     }
     
     function MoveObject (thisTransform : Transform, startPos : Vector3, endPos : Vector3, time : float) {
         var i = 0.0;
         var rate = 1.0/time;
         while (i < 1.0) {
             i += Time.deltaTime * rate;
             
             thisTransform.position = Vector3.Lerp(startPos, endPos, i);
             thisTransform.LookAt(endPos);
             //yield; 
         }
         if(thisTransform.position == endPos)
             currentState = State.Waiting;
     }
     
     function Wait() : void{
         waitTimer += Time.deltaTime;
         if(waitTimer > waitTime){
             waitTimer = 0.0;
             currentState = State.Moving;
         }
     }
     
Comment
Add comment · Show 2
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 DeBunked · Feb 13, 2013 at 07:10 PM 1
Share

You cannot put yield statements directly inside the function Update, at most you can call a function outside of the actual update that has Yield in it.

avatar image Ninita · Feb 13, 2013 at 09:59 PM 0
Share

Thanks, I didn't know that. I fixed that, but its motion is the same: with a random motion around the map and not between two defined points linearly :S

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by g0tNoodles · Feb 13, 2013 at 10:46 PM

This is what I use for a simple waypoint script. Modify it how you see fit!

 var waypoint : Transform[];
 var speed : float = 20;
 private var currentWaypoint : int;
 var loop : boolean = true;
 
 
 
 
 function Update () {
     if(currentWaypoint < waypoint.length){
         var target : Vector3 = waypoint[currentWaypoint].position;
         var moveDirection : Vector3 = target - transform.position;
     
         var velocity = rigidbody.velocity;
     
         if(moveDirection.magnitude < 1){
             currentWaypoint++;
         } else {
             velocity = moveDirection.normalized * speed;
         }
     } else {
         if(loop){
             currentWaypoint = 0;
         } else {
             velocity = Vector3.zero;
         }
     }
     
     rigidbody.velocity = velocity;
         
 }
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

11 People are following this question.

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

Related Questions

How to move an npc? 1 Answer

Making an NPC go up and down in UNITY 0 Answers

Moving model with animation 2 Answers

Rotate, follow randomly generted waypoints? 2 Answers

moving object toward waypoint and alignment 0 Answers


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