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 jscnet · Dec 10, 2012 at 05:54 AM · c#movementplayerpaths

Moving player along path?

I'm sorta' a noob, however pretty familiar with Unity3D. I'd would like to know the best way to move my player along a path?

I have an level where my player can go forward, backwards, left or right. I need my player to drive down the center of each path (my player moves automatically, the user only controls the direction; forward, backwards, left or right; think PacMan).

What are the best techniques for doing this? My paths may also be curved. Keeping the player center is important and keeping him looking forward in the direction of movement is also. I was thinking i could trace out some sort of spline along my paths, but unity doesn't have that (that I can tell). Can/should I use Unity's built-in Navigation functionality? Looks like that might only be good for patrollers or drones, not a player object. Seems pretty simple, but I'm feel'n a bit stumped on how to approach it.

Any recommendations 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 greatestprez · Dec 10, 2012 at 06:44 PM 0
Share

I think you can do that with ITween.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FlowStateGames · Dec 10, 2012 at 07:11 PM

The simplest answer is to create a set of pathnodes. Put these pathnodes in a collection. Add a public variable of this type of collection on your player. This way you can assign them from the editor (so that you could have different paths for different scenes/behaviors). Then use the collection to iterate through them whenever you decide to move on. Here's some pseudocode:

 //class declaration
 
 public NodeCollection currentPath;
 
 void Update(){
 
   if(followingPath){
     // evaluate your condition for moving onto the next node
     UpdatePath();
     
     // rotate player towards next node
     LookAtPath();
   }
 }
 
 void UpdatePath(){
    
     // check if you're within the desired distance to the current node
     if(closeEnough){
       
       // if we're not at the end of the path
       if(!currentPath.HasNext()){
         currentPath.NextNode();
       }
    
       else{
         // here we can deal with coming to the end of the path.
         // is this the beginning of the next scene? do we loop around and start the path over?
         // either way, one way to deal with it is to black box the behavior into a method 
         //(maybe this can even be a method from another script, so you can 
           //overload it with different behaviours)
 
         EndOfLine();
       }
     }
 }

Obviously, this is basic code. You could implement this as an FSM, where your states are something like "NoPath, ToNode, AtNode", and then your edges between states handle what to do when you get to a node.

You could also extend this for multiple paths, to have conditions which may allow you to move from one path to another, etc.

This is very basic, but has the upside of being extremely lightweight. For something a bit more complex, you could also do something similar to A* pathing (or a more realistic, slightly more complex algorithm, theta-star pathing), but this should get you started.

Let me know if any of this is obfuscated or confusing, kinda jotted this down in a hurry.

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

Making a bubble level (not a game but work tool) 1 Answer

Circular movement of the player 0 Answers

How can i smooth out the rotation? 1 Answer

Player movement isn't working 1 Answer

How do i use animated Objects as a player? 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