Article directory
-
- 1. Recent thoughts
- 2. Movement
-
- 1) Create a plane to move
- 2) Import character model
- 3) Install the input installation package of unity
- 4) Set the input system of unity (involving three scripts)
- 5) Character collision and rigid body
- 6) Character movement module and input script (player_locomotion)
- 7) Animation script control
- 8) Animation control and character execution animation frame control
- 3. Explanation of some terms
1. Recent thoughts
Recently, I am a bit confused. If I want to be a mmorpg server programmer, I must at least know some client-side things, so I want to refer to the guidance video on Youtube for making Black Soul to learn. My brother is called Sebastian Graves, Portal, and I am learning in docker After the end, there will also be nginx construction and blog writing for a simple understanding of the principle.
2. Movement
Brother sebastian's video portal
1) Create a plane to move
But this kind of floor pattern is very hardcore, so you have to change the texture first
Rename it to Floor_MAT, and select the texture
and select the size of the grid as 5x5
2) Import character model
- Load the character model, because it is charged, so there is no link to download it here, but this is available on the Internet, you only need to buy it online, and you need to match the corresponding character movement animation
- Create scene obj and name it Player
- Import the character model into this obj,
now there is a character obj after importing
- Now you need to edit the C# script to make the character move
- Noun explanation
locomotion move - Here two things are needed to make the characters move
①InputHandler player input recognition system
②player_locomotion player movement system
3) Install the input installation package of unity
-
Install the installation package for mobile recognition input
-
If you can't find this Input System, then click Advanced and select
-
Points to note
Restart unity to reload the project after installation -
New Input Actions
-
The script generates a C# class, and then apply
4) Set the input system of unity (involving three scripts)
(1) Scripts involved
①InputHandler input
②PlayerControls control input
③PlayerLocomotion character movement
(2) Set which keyboard keys the PlayerControl is monitoring (InputHandler.cs)
①Double-click PlayerControls to bring up a window and create a new action.
②Rename it to Player Movement, rename actions to Movement, change actions type to Pass Through,
and change control Type to Vector. ③Delete the default No Bingdings, add 2D Vector Composite, and rename it to WASD
④ Modify the corresponding Composite to 2D Vector, Mode to Analog
⑤ Let wasd bind the corresponding keyboard settings
⑥ Add Camera monitoring, let the camera follow on the right side, add StickDeazone
⑦ Add Binding (Delta(Mouse))
Processors and select Normalize Vector2
⑧ Set items Configuration (this can modify some input settings)
change Default Hold Time to 0, if it is not set to 0, there will be a delay when the character moves
5) Character collision and rigid body
Unity Rigid Body Document
①Click on the GameObject of the character Player to add a capsule collider
and modify some parameters
②Add RighidBody, then set Constraints, set Freeze Rotation (because we want to control the rotation by ourselves)
③Modify the C# script of InputHandler()
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace SG
{
public class InputHandler : MonoBehaviour
{
public float horizontal;
public float vertical;
public float moveAmount;
public float mouseX;
public float mouseY;
//人物移动键盘监听的管理器
PlayerControls inputActions;
CameraHandler cameraHandler;
//移动输入
Vector2 movementInput;
//摄像头输入
Vector2 cameraInput;
private void Awake()
{
cameraHandler = CameraHandler.singleton;
}
private void FixedUpdate()
{
float delta = Time.fixedDeltaTime;
if (cameraHandler != null)
{
cameraHandler.FollowTarget(delta);
cameraHandler.HandlerCameraRotation(delta,mouseX,mouseY);
}
}
public void OnEnable()
{
//①初始化单例和单例对应的回调函数
//获取键盘的输入数据,从2D坐标数据读进来
if (inputActions == null)
{
inputActions = new PlayerControls();
inputActions.PlayerMovement.Movement.performed += inputActions => movementInput = inputActions.ReadValue<Vector2>();
inputActions.PlayerMovement.Camera.performed += i => cameraInput = i.ReadValue<Vector2>();
}
//②初始化这个输入资源库
inputActions.Enable();
}
private void OnDisable()
{
//回收输入资源库对应数据
inputActions.Disable();
}
//这里应该是每帧去调用,转进来应该是时间?
public void TickInput(float delta)
{
MoveInput(delta);
}
//这里应该是每帧去调用,转进来应该是时间?
private void MoveInput(float delta)
{
//记录水平垂直移动值,并计算移动值
horizontal = movementInput.x;
vertical = movementInput.y;
moveAmount = Mathf.Clamp01(Mathf.Abs(horizontal) + Mathf.Abs(vertical));
mouseX = cameraInput.x;
mouseY = cameraInput.y;
}
}
}
6) Character movement module and input script (player_locomotion)
① code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace SG
{
public class PlayerLocomotion : MonoBehaviour
{
//主摄像机的坐标
Transform cameraObject;
InputHandler inputHandler;
Vector3 moveDirection;
[HideInInspector]
public Transform myTransform;
[HideInInspector]
public AnimatorHandler animatorHandler;
public new Rigidbody rigidbody;
//后面会有个lock Camera
public GameObject normalCamera;
[Header("Stats")]
[SerializeField]
float movementSpeed = 5;
[SerializeField]
float rotationSpeed = 1;
void Start()
{
//初始化单例
rigidbody = GetComponent<Rigidbody>();
inputHandler = GetComponent<InputHandler>();
animatorHandler = GetComponentInChildren<AnimatorHandler>();
cameraObject = Camera.main.transform;
//这个的transform是默认的哪个?
myTransform = transform;
animatorHandler.Initialize();
}
public void Update()
{
//获得距离上一帧的间隔时间
float delta = Time.deltaTime;
inputHandler.TickInput(delta);
//移动主摄像头坐标位置
moveDirection = cameraObject.forward * inputHandler.vertical;
moveDirection += cameraObject.right * inputHandler.horizontal;
moveDirection.Normalize();
moveDirection.y = 0;
float speed = movementSpeed;
moveDirection *= speed;
Vector3 projectedVelocity = Vector3.ProjectOnPlane(moveDirection, normalVector);
rigidbody.velocity = projectedVelocity;
animatorHandler.UpdateAnimatorValues(inputHandler.moveAmount, 0);
if (animatorHandler.canRotate)
{
HandleRotation(delta);
}
}
#region Movement
Vector3 normalVector; //后面会用到
Vector3 targetPosition;
//调整玩家的旋转
private void HandleRotation(float delta)
{
Vector3 targetDir = Vector3.zero;
float moveOverride = inputHandler.moveAmount;
targetDir = cameraObject.forward * inputHandler.vertical;
targetDir += cameraObject.right * inputHandler.horizontal;
//向量单位化,a vector keeps the same direction but its length is 1.0
targetDir.Normalize();
targetDir.y = 0;
if (targetDir == Vector3.zero)
targetDir = myTransform.forward;
float rs = rotationSpeed;
Quaternion tr = Quaternion.LookRotation(targetDir);
Quaternion targetRotation = Quaternion.Slerp(myTransform.rotation, tr, rs * delta);
myTransform.rotation = targetRotation;
}
#endregion
}
}
②Click the player obj, mount the C# script under the character
③Mount locomotion
- Summary
In this way, the character can move on this plane
7) Animation script control
① New animation management script Animations.cs
② Code display
using System.Collections.Generic;
using UnityEngine;
namespace SG
{
public class AnimatorHandler : MonoBehaviour
{
public Animator anim;
int vertical;
int horizontal;
public bool canRotate;
//相当于C++类自定义个init函数
public void Initialize()
{
anim = GetComponent<Animator>();
vertical = Animator.StringToHash("Vertical");
horizontal = Animator.StringToHash("Horizontal");
}
//用移动值选择更新不同移动值,来选择不同动画
public void UpdateAnimatorValues(float verticalMovement, float horizontalMovement)
{
#region Vertical
float v = 0;
if (verticalMovement > 0 && verticalMovement < 0.55f)
{
v = 0.5f;
}
else if (verticalMovement > 0.55f)
{
v = 1;
}
else if (verticalMovement < 0 && verticalMovement > -0.55f)
{
v = -0.5f;
}
else if (verticalMovement < -0.55f)
{
v = -1;
}
else
{
v = 0;
}
#endregion
#region Horizontal
float h = 0;
if (horizontalMovement > 0 && horizontalMovement < 0.55f)
{
h = 0.5f;
}
else if (horizontalMovement > 0.55f)
{
h = 1;
}
else if (horizontalMovement < 0 && horizontalMovement > -0.55f)
{
h = -0.5f;
}
else if (horizontalMovement < -0.55f)
{
h = -1;
}
else
{
h = 0;
}
#endregion
anim.SetFloat(vertical, v, 0.1f, Time.deltaTime);
anim.SetFloat(horizontal, h, 0.1f, Time.deltaTime);
}
public void CanRotate()
{
canRotate = true;
}
public void StopRotation()
{
canRotate = false;
}
}
}
③Click the small player below the Player to add animation components
- Summary
Now you can rotate the character
8) Animation control and character execution animation frame control
①Add animation control component and change its name to humanoid
②Put humanoid in the corresponding component position of Controller
③Open animation component page
④Add two parameters
⑤Click Layers to create new Blend Tree
⑥Double-click to open this newly added BlendTree, modify Blend Type is 2D Freeform Cartesian, and the parameters are changed to the two newly added parameters
⑦Add three running and walking animations, and reset the parameters to 0
3. Explanation of some terms
1) velocity
2) InputSystem input system
playeControl 负责处理存储键鼠、手柄的输入。
InputHandle 处理PlayerControl里的数据,把InputSystem里的数据类型转化为float、Vector2这种常用、直接的数据类型。
AnimatorHandle 负责给animator传入数值,改变动画状态
PlayerLocomotion 负责Player的rigidbody.velocity,以及transform.rotation
CameraHandle 负责控制相机的跟随,旋转。
Supplementary note:
After the InputHandle has processed the input data, other scripts will perform calculations and encapsulation. The related functions of Player are executed uniformly in Update of PlayerLocomotion, and CameraHandle is executed in InputSystem.
3) animation animation
4) resemble looks like
5) locomotion mobile movement
6) encapsulating
v. brief description; generalization; compression;
7) magnitude
n. huge; significant; importance; magnitude; star brightness; magnitude;
8) animation
n. animation; animation; angry; vitality; full of vitality;