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 AliquidHacker · Oct 26, 2015 at 11:59 AM · playerlistvector2

Move the player smoothly from a list of vector2's

Hello ladies and gents, I am trying to find a way to move a player to a list of vector2's one by one, So that if the player is at position 0,0 and the first vector is 0,5 it will move to the position 0,5 before moving on to the second vector2 which might be something like 5,5 and so on. I have been trying to find ways to do it over and over again but every time I do something and finally get rid of all errors the player just wont move... Any idea how I should work on it to make it work, specific procedure, specific code? Help would be greatly appreciated thank you AliquidHacker

Comment
Add comment
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

1 Reply

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

Answer by NightLucididty · Oct 27, 2015 at 10:44 AM

This took me way longer than expected due to some unexpected errors but here is the code I came up with, basically it finds your list of vector2's and makes sure the player is at the same position as its previous vector2. Than it moves the player from its position to the next vector2 (Hense playerpos+1). And so on until the vector2 is done, you will however get an error once all of the vector2's are done, my time is limited at the moment so I didn't make a fix for that, it is relatively easy and you should be able to fix it on your own. Anyway here is the code, using UnityEngine; using System.Collections; using System.Collections.Generic;

 public class GhostPlayer : MonoBehaviour {
     
     public List<Vector2> playerMove;
 
     public int numberOfMoves = 0;
     public bool canMove = true;
 
    
     void Update () {
         if (this.transform.position.x == playerMove [numberOfMoves].x && this.transform.position.y == playerMove [numberOfMoves].y) {
             canMove = true;
         }
 
         if(canMove) {
             this.transform.position = Vector2.MoveTowards(this.transform.position, playerMove [numberOfMoves+1], 6 * Time.deltaTime);
             if (this.transform.position.x == playerMove [numberOfMoves+1].x && this.transform.position.y == playerMove [numberOfMoves+1].y) {
                 canMove = false;
                 numberOfMoves++;
             }
         }
     }
 }

the reason why I made a bool labeled can move is because if I left the command to be executed once the player would move halfway and the code wouldn't continue. I am not saying that my way is the most efficent way to do this but I hope its good enough for what you are looking for, NightLucidity

Comment
Add comment · Show 2 · 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 NightLucididty · Oct 27, 2015 at 10:47 AM 0
Share

Oh and I forgot to say that if you change the 6 on line 16 this will increase or decrease the speed.

avatar image AliquidHacker · Oct 28, 2015 at 01:12 PM 0
Share

This code makes no sence, but it seems to work, any chances you could explain it in more details?

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

A node in a childnode? 1 Answer

Next song problem (music player) 1 Answer

initialising a list with values, and adding a range to a list 2 Answers

drag drop a object into list objectes 0 Answers

Find the highest first value of a vector2 in a list? 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