3D Beginner: Tutorial Resources | John Lemon's Haunted Jaunt: Detailed 3D Beginner project (refer to official documents)
The purpose of the article is to make a more detailed summary of some noteworthy points in the UnityLearn project, as well as the places where the picture demonstration is not shown. It will solve problems for friends who have errors during the project and do not know how to start.
Preparations before starting
This unity official project download address (the link to the official tutorial is also included)
3D Beginner: Tutorial Resources | Tutorials | Unity Asset Store
save it to my project
Create a 3D empty core project, my version is 2021.1.11f1c1
Download the imported project in Window's Package Manager
1. Save the scene
Create a scene resource called MainScene.
2. Add character model
Drag the JohnLemon game object into the scene
Internals of the JohnLemon object
The way JohnLemon is essentially animated is as follows: the Animator component on the character's parent game object will change the rotation of all skeletal game object Transform components, and these changes will happen together to animate the character.
3. What is prefab?
4. Convert the character to a prefab (put into the Prefebs folder)
5. Animate your character (just say that it is ready to be animated, but there is no operation)
6. Create Animator Controller (created in the corresponding folder Animator)
7. Set up the animation
Is the JohnLemon game object already a prefab?
Enter prefab mode to disable the Auto Save checkbox (enabling this checkbox will slow down)
Add animation to the game object JohnLemon, you can see that there is an Animator component, you need to create an Animator Controller
After the corresponding location is created, go to find the animation resource and drag it into the Animator window
Create a bool quantity in Parameters, named LsWalking, used to determine whether the game object is walking, used for transition parameters
Now, there are two states in the state machine, but there is no logic to define which state should be played. Currently, the state machine will start with a default state and it will never change, so JohnLemon will always be idle, to add some logic, Animator transitions need to be created.
8. Create Animator Transition
Right-click Make Trasition to create a transition
Click the connection line to enter the Insperctor, cancel the Has Exit Time check box, if Has Exit Time is true (enable this check box), then after a certain period of time, the transition will be performed automatically, and the state machine will play the next state . In this game, it's important to control the timing of transitions, so disable the Has Exit Time checkbox.
9. Add Conditions to Transitions
The following will add the condition to provide the transition
Add conditions to Conditions
From walk to idle, emptying is the opposite, and it is done by itself.
10. Assign the Animator Controller to the JohnLemon prefab
Drag the Animator Controller to the Controller property of the Animator component of the game object
11. Make characters react to physics
Add physical effects to the game object below
Start Play, and, well, something is wrong: the character stops after falling a short distance. Press the Play button again to exit playback mode.
This is caused by Animator. The third property on the Animator component is Apply Root Motion, which is currently enabled. Root motion
12. What is Root Motion?
13. What is update loop?
14. Fix John Lemon's movement problem
Enter the prefab mode of the game object, and change the UpdateMode to Animate Physics in Animator (previously it was Normal)
This change will make Animator move the character in time with physics. Now, there should be no race between update loops moving the character, and the character will react to the physics setup as it should.
You've seen that the animation doesn't have any vertical root motion, but that won't stop JohnLemon from moving vertically after colliding with some objects. There are several ways to prevent this.
The most obvious option is to disable Use Gravity on the Rigidbody component:
This prevents the character from falling, but it's not what you want. With Use Gravity disabled, a character may still fall if a collision pushes it down, or float away if a collision pushes it up. You definitely don't want this to happen, so you'll need to use physics to limit your character's movement.
Locate the RigidBody component and click the arrow to expand the Constraints property. These Constraint properties will limit the movement direction of the rigid body (Rigidbody). Let's explore Unity's coordinate system to see exactly how it works.
15. Coordinates in Unity
Position and orientation in Unity take 3D coordinates represented by x, y, and z (which together form a vector). In the next tutorial, you'll discover more about vectors. A scene has a global definition of these directions, visible at the top right of the Scene window.
Colored arrows indicate the positive direction of each axis, while opposite gray arrows indicate the negative direction:
- x-axis is red
- y-axis is green
- z axis is blue
16. Position and Rotation
The height of the character should not change, so it should be Freeze position Y
The character should not lie down on the front and side, so Freeze RotationX, Z
17. Add colliders to John Lemon
Next add a Collider to the game object
The simplest shape that works best for JohnLemon is a Capsule Collider, not a Box Collider.
resize to fit
18 Summary:
You have started working on your game character. You took a first look at the functionality of the prefab and created a simple set of rules to constrain JohnLemon's behavior.