- Home /
Animation for character movement not being stopped on Task.WhenAll()
Currently have a await for my character movement (2 characters run into position once they reach their destination) the issue is the run (movement) animation doesn't stop even when I break out of one of the loops. It is waiting for both to finish the while loop, any ideas?:
async Task MoveCharacter(Character character, int n)
{
character.SetMoveAnimation(true);
while (Vector3.Distance(character.transform.position, character.PositionData[n].transform.position) > .01f)
{
character.transform.position = Vector3.MoveTowards(character.transform.position, character.PositionData[n].transform.position, Time.deltaTime * 3f);
await Task.Yield();
}
//Shouldn't this be called on the character to stop it's animation when it reaches the destination?
character.SetMoveAnimation(false);
}
List<Task> TaskList = new List<Task>();
TaskList.Add(Move(characterA, n));
TaskList.Add(Move(characterB, n));
await Task.WhenAll(TaskList.ToArray());
Answer by sacredgeometry · Mar 19 at 02:24 PM
Unity isn't thread safe I would avoid using the TPL or any multithreaded work that involves talking to its API/ during gameplay unless you are absolutely sure you know what you are doing.
This is not an answer.
I would also do some research in what "await task" is because it's not automatically multithreaded.
Unity uses the default TaskScheduler, so it absolutely is.
I suggest some reading
http://www.albahari.info/threading/threading.pdf
https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.taskscheduler?view=net-6.0
Mate, you really need to learn about parallel programming, I'm not running thread with the above code: https://docs.microsoft.com/en-ca/archive/blogs/benwilli/tasks-are-still-not-threads-and-async-is-not-parallel
Your answer
Follow this Question
Related Questions
Why can't I build Web Player in Unity while I have no problems with building standalone versions? 2 Answers
Can the animation editor create local rotational data? 3 Answers
Animation not working properly in unity web player 0 Answers
Adding animation clips via script 2 Answers
animation/audio not looping 0 Answers