animations and player movement

I am currently trying to finish a school project, I have almost all of it done, apart from one issue. I have a character in a 3D scene that can move normally using WASD. Forward for W, left for A etc etc. However the character looks towards the mouse, so they can be pressing W but looking to the right. This was all working fine until I had to add animations in. Basically if the character is pressing W and facing in the forward direction the walk animation should play, if they are pressing W but looking to the side a strafe animation should play.

I have all the animations set up, I just can’t think of the best method to get the animations to work correctly. I thought I could just use if W was pressed walk, if A is pressed strafe, but when looking at the mouse this no longer worked. A classmate came up with the idea to base it off of the direction of the player facing the mouse, this does not work either. I think I could combine these and check for every key press with every direction, but that seems that it would be bad coding practise and be a lot of if statements.

Does anyone possibly know how this is generally set up, or and idea’s of what I could try out.

One approach is to move a root object and animate only child objects accordingly.

That would mean your hierarchy looks like this:

VisualsAndEverythingAnimatedHereAndBelow```

That way they don't tangle.

Hello, Thank you for taking the time to respond. However, if possible would you be able to explain how this would work a bit more. I am uncertain how I could use this to solve my problem .

Well assuming your analysis above is correct, it should be self explanatory: the animations won’t affect the business logic of the player script because the two are separated.

If the analysis is not correct then strike everything I have said and go do some experiments to find out what is actually happening, otherwise you’re just chasing ghosts.

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

Sorry maybe I worded this wrongly. Originally I was simply using a capsule with eyes, the eyes would look towards the mouse position, the movement would also work correctly for it. The past week I set up the animations within the animator, but now I need to get them to work with the movement code. This is where the problem is occurring, I know how to get the animations to change and work in the scripts, for instance, if I press W i can set it up for the walk animation to play,

However, the issue is occurring because of the combination of the movement and the way the player looks at the mouse. The movement is always the say no matter what way the player is facing, so W is always forward (I believe Z axis) in the same direction, even if the player is facing that way, or facing the opposite way. Because of this getting the correct animation to show is causing issues. If my character is facing forward and pressing W the correct walk animation will play, however if the character is pressing W but facing to the right (the x axis), the walk animation will play but the animation will be playing showing the character walking to the right, but the actual movement will still be forward (z axis), so the movement will be like they are sliding to the side, while the walk animation is playing. Not sure if that explained it better. I can maybe try post a video or picture if that would help

I assumed you just meant the character +Z when you said “looks.”

Actually aiming something like eyes or a head or a gun on an animated character is going to involve blending the animations, possibly even IK depending on what you’re doing. I haven’t tinkered much with it recently but there’s videos for eyeballs and heads and guns pointing on an animated character and getting the blending correct.

I am looking into blending with the animations, I have not had a lot of experience with animations thought so not completely understanding it all. I will continue to look at tutorials though and hopefully something will help out. Thanks for the replies anyway