Grid movement bug.
I have a 2D grid and I move from one place to the next and if I let go of the movement key, the player will continue moving untill he reaches the closest grid block. When I release the arrow key and the player hasn't reached the block yet, the animation changes to facing downward until he reaches the next block. How do I make it so that he continues his animation until he reaches the block?
This is my code. https://imgur.com/a/m665r
I believe the problem is in this part of the code.
// Move there
transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
if(transform.position == Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed))
{
playerMoving = false;
}
I need to somehow add a way of keeping the animation playing until reaching the next grid block and then change it to the direction the player is facing.
I also get this error:
difference in effective length between states is too big. transition preview will be disabled.
Answer by hexagonius · Nov 24, 2016 at 10:25 PM
the actual move and the check ate the same code, so it's basically always true. Check for the final position
if(transform.position == pos)
{
playerMoving = false;
}
Thanks. It's a lot shorter and easier to read. $$anonymous$$y character still has the animation problem though.
https://imgur.com/a/$$anonymous$$OP4Y
The closer i am to the grid block, the shorter the downward animation is but it's still there and I don't know why it's there or how to get rid of it.
well, you mapped the animator to your input directly. so even though he is making the last steps towards the target, the animatior gets all float set to false, which I guess is then your downward looking idle state.
What I would do is check if the player is on his way first. As long as he just return the whole method and keep moving. AFTER that check, when he has reached it, check for the input while setting your animator according to it. If there's input still, set a new target, which keeps code execution from altering your animator again. So in pseudo code:
Update
if (!reachedTarget)
return;
set animator to input
if (input)
set new target
What would that look like in the code? I'm somewhat new to coding and still getting a hold of things.
This is the code I've got so far.
Your answer
Follow this Question
Related Questions
Animation clip doesn't play 1 Answer
How do I know what animation I can transit to in the animator, from my current animation? 0 Answers
Why is the last frame of the animation not triggering? 0 Answers
How do you make sure that when you land on an object, an animation starts? 0 Answers
Animation from child object overrided by its parent and won't show in game window 0 Answers