Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
0
Question by leinaddc · May 28 at 06:12 PM · movement script

Using Vector3.MoveTowards with a list of waypoints

Hi all, I am trying to make a GameObject follow a certain path of waypoints by a roll dice.

I.E: Dice roll is 3, traverse a list of waypoints until it reaches the last one.

The code I have works but the GameObjects moves from the current waypoint it is in, to the last one, skipping the rest of waypoints in the middle.

  void Start()
     {
 
         _waypoints = _waypointsController.GetWaypoints();
 
         _currentWaypoint = _waypoints[0].transform;
         print("Starting: " +_currentWaypoint.name);
         _currentIndex = _waypoints.IndexOf(_currentWaypoint.gameObject);
         print("Starting index: " + _currentIndex);
 
         transform.position = _currentWaypoint.position;
 
     }
     void Update()
     {
 
         if (Input.GetKeyDown(KeyCode.Space))
         {
             _diceRoll = _diceManager.RollDice();
 
             print("Roll dice was: " + _diceRoll);
 
             _isMoving = true;
 
         }
         MovePlayer();
         if (Vector3.Distance(transform.position, _currentWaypoint.position) > 0.1f)
         {
             transform.position = Vector3.MoveTowards(transform.position, _currentWaypoint.position, _moveSpeed * Time.deltaTime);
         }
 
     }
 
 
     private void MovePlayer()
     {
         if (_isMoving)
         {
             print("Starting waypoint and index: " + _currentWaypoint.name + " " + _currentIndex);
 
             for (int k = 0; k <= _diceRoll - 1; k++)
             {
                 if (_waypoints.IndexOf(_currentWaypoint.gameObject) < _waypoints.Count - 1)
                 {
 
                     _currentWaypoint = _waypoints[_currentIndex + 1].transform;
                     print("New waypoint" + _currentWaypoint.name);
 
                 }
                 else
                 {
                     _currentWaypoint = _waypoints[0].transform;
 
                 }
                 _currentIndex = _waypoints.IndexOf(_currentWaypoint.gameObject);
             }
             print("Ending waypoint and index: " + _currentWaypoint.name + " " + _currentIndex);
             _diceRoll = 0;
             print("Stop moving");
             _isMoving = false;
         }
 
     }


I have tried putting the MoveTowards inside the MovePlayer method but it doesn't work, game object stutters each space press.

Any insight is appreciated.

Comment
Add comment · Show 1
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 rh_galaxy · May 29 at 03:17 AM 0
Share

My understanding of your code is that MovePlayer() will take you to the last waypoint directly, because your for loop will not allow movement along the waypoint. Update() needs to finish for every position along the way.

I'm not entirely certain, but maybe you can use something from my waypoint movement code in Update().

 int iCurWP = 1;
 float fWPTime = 0f;
 Vector3 vVel;
 
 //move along waypoints
 if (iNumWayPoints > 1)
 {
     fWPTime -= Time.fixedDeltaTime;
     if (fWPTime <= 0)
     {
         //find out velocity to next wp
         Vector3 vDist = new Vector3(vWayPoints[iCurWP].x - vPos.x,
             vWayPoints[iCurWP].y - vPos.y, 0);
         float fDist = vDist.magnitude;
         fWPTime = fDist / iSpeed;
         vVel = new Vector3(vDist.x / fWPTime, vDist.y / fWPTime, 0);
         iCurWP = (iCurWP + 1) % iNumWayPoints;
     }
     vPos += vVel * Time.fixedDeltaTime; //move along velocity
 }
 //set object to position vPos here

0 Replies

· Add your reply
  • Sort: 

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

145 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 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

Moving AI away or towards player not working correctly. 1 Answer

Audio PlayOneShot Help 1 Answer

I can't get framerate independence to work... 1 Answer

Touch movement too slow 0 Answers

Fighting Streets of Rage style movement script 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