Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 HBishop · Apr 22, 2019 at 06:19 PM · playermovement scriptwaypoint

Player doesn't reach waypoint location

My player currently traverses the nodes in the Stack of Vector3 called currentPath. The problem is that it seems to do it in one frame. However when I change the moveTowards function from speed to speed*Time.deltaTime, it goes at the correct speed but it only goes halfway towards a node before starting to head for the next, but never fully reaches them.

 Update()
 {
     if (currentPath != null && currentPath.Count > 0)
     {
         if (transform.position!=currentPath.Peek())
         {
             transform.position = Vector3.MoveTowards(transform.position, currentPath.Peek(), speed);
         }
         else
         {
             currentPath.Pop();
             if (currentPath.Count == 0)
             {
                 moving = false;
             }  
         }
      }
 }

I've tried adding a timer but it didn't seem to work Any help, I'd be really grateful. Thank you!

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 DiegoSLTS · Apr 22, 2019 at 06:49 PM 0
Share

The last parameter of $$anonymous$$oveTowards is not meant to be a speed, it's "maxDistanceDelta". If the value of "speed" is higher than the distance the movement will happen in one call, that's what happens probably without the *TIme.deltaTime part.

Now, the behaviour when you add the *Time.deltaTime sounds weird. Have you tried to Debug.Log the transform.position and currentPath.Peek() or just call Debug.Break on the "else" block? Is it possible that other part of your code is changing the position in the same frame?

avatar image highpockets · Apr 22, 2019 at 06:52 PM 0
Share

What does currentPath.Peek() look like?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by highpockets · Apr 23, 2019 at 10:03 AM

MoveTowards is just as easy as lerp, it’s just a different way of accomplishing the same thing. Lerp will interpolate between 2 Vector3’s linearly and MoveTowards will just move towards a Vector3 position by the amount you pass as the last parameter. I think since you are constantly changing your end positions based on click positions, MoveTowards is a better bet, because if you change your target halfway through a Lerp, it is no longer linear and the speed will change unless you do extra calculations to compensate for the non linear movement.


If it is doing it all in one frame, your speed/maxDistanceDelta value is much to high. You should lower the speed value first of all and to make sure that you at least spend one frame at each waypoint you should check if speed is mor than the distance from the player to the waypoint and if it is, that frame, you should just move the length of distance rather than speed.

Cheers

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
avatar image
0

Answer by Magso · Apr 22, 2019 at 08:36 PM

I know this isn't similar to what you have but it would be simpler to use lerps like this

 public Transform[] points;
 public float distanceTravelled;
 public float speed;
 void Update()
     {
     distanceTravelled += speed;
     if(distanceTravelled < 1)
     {
         transform.position = Vector3.Lerp(points[0].position, points[1].position, distanceTravelled);
     }
     else if(distanceTravelled < 2)
     {
         transform.position = Vector3.Lerp(points[1].position, points[2].position, distanceTravelled-1);
     }
     //and so on.
 }

or use an int variable to manage the points if the path is only followed one way.

 public int currentPoint;
 void Update()
      {
      distanceTravelled += speed;
      if(distanceTravelled < currentPoint+1)
      {
          transform.position = Vector3.Lerp(points[currentPoint].position, points[currentPoint+1].position, distanceTravelled-currentPoint);
      }
     else{
         currentPoint++;
     }
 }
Comment
Add comment · Show 4 · 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 HBishop · Apr 22, 2019 at 09:12 PM 0
Share

Hi, Sorry, I forgot to specify, the currentPath stack varies. It isn't fixed. This is part of my Waypoint system. When I click the mouse, it finds the quickest route through set nodes from the current position to the mouse click and stores them in CurrentPath, then is meant to move the player using the code above. Thanks for the fast reply!

avatar image Magso HBishop · Apr 22, 2019 at 09:35 PM 0
Share

What exactly is currentPath?

avatar image HBishop Magso · Apr 23, 2019 at 08:06 AM 0
Share

Current path is a stack of vector3. It has previously stored all the waypoints that give the quickest route from the players current position to the final position. It all works except for the fact it's doing it all in one frame. Coroutines didn't work although I've not tried much with them. Thanks!

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

129 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need help setting up Simple_Swordman by Black Hammer 0 Answers

Player won't stop moving while blocking 1 Answer

whether the player can react based on user input while following in the waypoint system? 0 Answers

Waypoint marker that counts distance from player 0 Answers

Player movement boudaries in 2D 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