• 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 /
  • Help Room /
avatar image
0
Question by hilmimarzuqi · Mar 24, 2016 at 08:24 PM · coroutinesfilestreamcharstring.split

How to move player using Coroutine?

I have a player character that has its movements saved into a file. The saved inputs are mainly WASD chars. The file is then read from another script to iterate through the file and receive the inputs (ig. a,d,space). The codes are as below:-

     private void Update()
     {
     StartCoroutine("RelayInputs");
     }

     IEnumerator RelayInputs(){
     readTextFile(Application.dataPath + "/KeycodeList.txt");
     
     yield return new WaitForSeconds (0.5f);
             
         }

     void readTextFile(string file_path)
     {
         StreamReader inp_stm = new StreamReader(file_path);
         
         while(!inp_stm.EndOfStream)
         {
             string inp_ln = inp_stm.ReadLine( );
             // Do Something with the input. 
             string[] splitArray =  ((string) inp_ln).Split(char.Parse(" ")); // split space in string
         for(int i = 0; i < splitArray.Length; i++)
         {
             Debug.Log(splitArray[i]);
             switch (splitArray[i])
             {
             case "a":
                 transform.position = new Vector2(1*10*timePressed,0);
                 break;

             case "d":
                 transform.position = new Vector2(-1 * 10 * timePressed, 0);
                 break;

             case "space":
                 transform.position = new Vector2(0,1 * 400);
                 break;
         
             }
         }
         //    Debug.Log ("STRING FROM FILE:" + inp_ln);
         }
         inp_stm.Close( );
     }

timePressed is the duration of how long the key is pressed by the player

I managed to be able to run it but it only displays each character that exists in the file and how many of it is in the file. Image below is the Debug: alt text

I wanted to know if there is a way to read the file and allow the player to move based on what the sequence of chars that has been stored in file.

codes.png (6.6 kB)
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 LazyElephant · Mar 24, 2016 at 09:03 PM

The problem is the way you set up your coroutine. Coroutine don't automatically repeat every frame, so you need to use some form of iteration inside. The yield statement is then used to break out of the function for a period of time before continuing where it left off.

When you call StartCoroutine, the readTextFile function is running to completion before you ever reach the yield statement.

I'm guessing that you want to pause in between player moves, so the following code which uses readTextFile as your coroutine should work.

  private void Update()
  {
  StartCoroutine(readTextFile(Application.dataPath + "/KeycodeList.txt"));
   }

  IEnumerator readTextFile(string file_path)
  {
      StreamReader inp_stm = new StreamReader(file_path);
      
      while(!inp_stm.EndOfStream)
      {
          string inp_ln = inp_stm.ReadLine( );
          // Do Something with the input. 
          string[] splitArray =  ((string) inp_ln).Split(char.Parse(" ")); // split space in string
      for(int i = 0; i < splitArray.Length; i++)
      {
          Debug.Log(splitArray[i]);
          switch (splitArray[i])
          {
          case "a":
              transform.position = new Vector2(1*10*timePressed,0);
              break;
          case "d":
              transform.position = new Vector2(-1 * 10 * timePressed, 0);
              break;
          case "space":
              transform.position = new Vector2(0,1 * 400);
              break;
          }
          yield return new WaitForSeconds (0.5f); 
       }
      //    Debug.Log ("STRING FROM FILE:" + inp_ln);
      }
      inp_stm.Close( );
  }
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 hilmimarzuqi · Mar 25, 2016 at 03:31 AM 0
Share

This works wonders! Thank you so much, now it iterates one by one each char in the file and displays in the console, but somehow the transform.position isnt working as the player doesnt move, the switch case statements should be correct right?

avatar image LazyElephant hilmimarzuqi · Mar 25, 2016 at 03:42 AM 0
Share

It looks like you're just setting the position to the new Vector2. If you want the character to move, you should probably be adding the new Vector2 to the current position.

 transform.position += new Vector2...
avatar image hilmimarzuqi LazyElephant · Mar 25, 2016 at 03:48 AM 0
Share

I did the same thing before but it displays an error

The call is ambiguous between the following methods or properties: UnityEngine.Vector2.operator +(UnityEngine.Vector2, UnityEngine.Vector2)' and UnityEngine.Vector3.operator +(UnityEngine.Vector3, UnityEngine.Vector3)'

Here is the code case "a": transform.position -= new Vector2(1*10*timePressed, 0);

case "d": transform.position += new Vector2(1*10*timePressed, 0);

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

52 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

Related Questions

How to split a string into multiple floats? 2 Answers

Same Coroutine on multiple objects 1 Answer

using coroutine with fixedupdate and time. 0 Answers

Disabling jump for 0.25 sec after you hit the ground after jumping? 1 Answer

How to stop audio in coroutine unity 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