top of page

CG Lab 4

  • yasobel
  • 13 oct. 2020
  • 4 min de lecture

Dernière mise à jour : 31 oct. 2020

Adding a character and animating it.


I started by creating a character on blender to use as a player for my game but sadly I wasn't able to upload it to Mixamo for the animations nor to unity.

In unity I got the following error "Thread group count is above the maximum allowed limit. Maximum allowed thread group count is 65535". I think it's because my blender file is too voluminous but I didn't find a solution so I decided to upload an existing character from Mixamo.


Next, I downloaded 5 animations : Idle, Walking, Running, Jumping and a Victory animation.

To apply the animation to the character, I added an animator component and a controller. Then it was time to start animating the character.

In order to do so, I made the following controller and conditions :

ree
ree

We have two triggers : isJumping and isWinning.

And three floats : Speed, Direction and JumpCounter.


Idle animation:

Let's start with the idle animation because it was the easiest the make. It's the most important one because it gives life to the player and instantly makes the game more realistic.

To transition from the Idle to Jumping or to Victory, we add a trigger condition.

To transition from the Idle to MovementTree, the speed has to be greater than 0.4, meaning that the player is at least walking ( the walking speed is at 0.5).


Victory animation:

Then to make the custom animation, I just added a trigger named "isWinning" so that when the player crosses the finish line while having all of the collectibles it activates the animation.

I added it as a condition for the transition from idle to victory as well as from movementTree to victory. And I also added a new condition in the OnTriggerEnter function in the characterController script to trigger the animation.

ree

Jumping animation:

Now let's move on to the jumping animation. I actually had some trouble with this one.

I noticed that the animation was too long and too slow, so when I clicked on the space bar to jump, it wasn't very smooth to play with.

To solve this problem, I made the animation shorter and faster :

ree

ree

Then I also added a trigger named isJumping to add as a condition in the transitions between the jumping animation and the other animations.

After that I had to modify the characterController script in order to control the animation.

ree
ree








It was important to add the jumpBoost because otherwise the player always had the same jump force no matter its value. And I also added a line to trigger the jumping animation. The rest of the script is practically the same.


MovementTree animation:

Instead of creating 4 states, for walking, walking backwards, running and running backwards I used a 2D blend tree.

The point of using a blend tree is to regroup states to have a clearer vision on the animations.

ree
ree

By adding a speed condition, we could transition from walking to running or the opposite. And by adding a direction condition, we could make the player walk/run forward or backwards. So, instead of downloading 4 animations, we only need 2 of them.

If the speed is equals to 0.5 and the direction is positive : the player walks.

If the speed is equals to 1 and the direction is positive : the player runs.

If the speed is equals to 0.5 and the direction is negative : the player walks backwards.

If the speed is equals to 1 and the direction is negative : the player runs backwards.


One thing to keep in mind is not to forget to toggle the loop mode for all of the animations or it won't work.

ree
ree

Tip:

It's important to disable the "has exist time" option in the transitions when there are conditions so that you don't have to wait for the end of the animation to move on the next one (meaning that we get responsive controls). But, in case there are no conditions, the "has exist time" has to be enabled.

Now, you have a fully working player with all of the essential animations.


Building the game :

Finally, I built the game on my computer to test it out. And surprisingly, it was lagging a lot and the movements of the player weren't smooth at all.

So, I've done some research and it's most probably because I have a lot of elements for the environment (rocks, trees etc..). The first thing that I've done is to enable the camera occlusion culling, set all of the elements of the surrounding environment to static then bake.

By doing this, we only render the objects that the camera sees as opposed to before where we rendered all of the elements of the scene.

Next, I noticed that some elements had an animator (because I downloaded the package from the asset store) so I removed it because I'm not using it. And finally, I changed all of the mesh colliders to box colliders.

By doing this, the game that I built was running smoothly and I was able to play.


Here's the final product :


If you want to check the code, everything is available on GitHub in the "About" section (I don't know why but I couldn't put the whole project on GitHub but I managed to push the scripts).

Commentaires


bottom of page