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 heeho17 · Jan 29, 2015 at 09:31 PM · scriptingbasicsplayer movement

Player move from waypoint to waypoint?

Hey Unity, I'm trying to make the player of my game move from column to column (6 columns in game) in order to dodge obstacles inside the columns. Any ideas for a script? Sorry, I am recently new to Unity and this concept. Thanks in advance! PS: will post pictures of inspector soon, not near computer at the moment.

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 Cherno · Jan 30, 2015 at 01:31 AM 0
Share

Please provide more information. Is there any pathfinding involveD? Does the character use a ChracterController or rigidbody? What about physics and collision?

To move a gameobject, you can use Vector3.Lerp or Transform.translate, for example. Look up both in the Unity User $$anonymous$$anual.

As for going from waypoint to waypoint, that's simple. You need three variables: An array of Vector3s which stores the coordinates of you waypoints, an index as and indicator of you current waypoint (int), and a threshold (float).

While the player is moving towards the current waypoint, continually check if the Distance between him and the current waypoint is smaller than the threshold. If it is, then increase the index by one and make the player move to this array position.

1 Reply

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

Answer by Chris333 · Jan 30, 2015 at 04:20 PM

Hi,

you could use a easy waypoint solution where you have a list of all waypoints stored in a array as GameObjects. You calculate the vector form your current position to the current waypoint of the array and move your player in that direction unless the distance to that waypoint reaches a minValue. Than you switch to the next waypoint in the array. When you reached the end of the array you begin from start. Thats a way how you can achieve a walking path for a guard for instance.

Example code for a 2D-top-down walk path:

 public float actualSpeed = 10.0f;
 public GameObject[] checkpoints;
 int counter = 0;
 public float distance = 2.0f; //on which distance you want to switch to the next waypoint

 void FixedUpdate ()
     {
         direction = Vector3.zero;

         //get the vector from your position to current waypoint
         direction = checkpoints[counter].transform.position - transform.position;
         //check our distance to the current waypoint, Are we near enough?
         if(direction.magnitude < distance)
         {
             if(counter < checkpoints.Length-1) //switch to the nex waypoint if exists
             {
                 counter++;
             }
             else //begin from new if we are already on the last waypoint
             {
                 counter = 0;
             }
         }
         direction = direction.normalized;
         Vector3 dir = direction;
 
         GetComponent<Rigidbody2D>().velocity = new Vector2(direction.x * actualSpeed, direction.y * actualSpeed);
     }
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do i keep my characters orientation? 0 Answers

How could I add ui images to an instantiated prefab by photon ?? 0 Answers

Play animation with FPS controller? 2 Answers

Wall Jump Scripting Help 2 Answers

Best way to store large quantities of information 2 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