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:
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.
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( );
}
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?
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...
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);
Your answer
Follow this Question
Related Questions
How Do I Fix Unexpected Char 0x201C? (Code Below) 1 Answer
Weird issue with l_ElapsedTime value only incrementing properly one time 1 Answer
Coroutine not working how I think it would? 1 Answer
Getting a value outside of Coroutine 2 Answers
How to Deactivate a Trigger collider on a Specific GameObject 2 Answers