Unity-人物移动
人物模型
参考以下视频:
如何在Unity中导入pmx格式的MMD模型_哔哩哔哩_bilibili
用的是原神模型,这里要注意导入后把人物模型的Rig换为Humanoid
人物动作
使用的Unity-Chan! Model的动作,在asset store找到这个资源添加并导入就行。
设置动画
使用blend-tree混合人物移动的动画
代码脚本编写
输入系统
- 安装插件
- 为Input Actions创建按键映射
在这之前要创建Input Actions,并 勾选生成C#类
- 脚本控制输入
using System;
using System.Collections;
using System.Collections.Generic;
using System.Input;
using UnityEngine;
namespace Player.Common
{
/**
* 输入系统
*/
public class InputSystem : MonoBehaviour
{
[Header("移动")]
[HideInInspector]public float horizontal;
[HideInInspector]public float vertical;
[HideInInspector]public float moveAmount;
[HideInInspector]public bool isRun;
// private float mouseX;
// private float mouseY;
private InputControls inputControls;
private Vector2 movementInput;
[HideInInspector]
public bool jumpInput;
private Vector2 cameraInput;
public void OnEnable()
{
if (inputControls == null)
{
inputControls = new InputControls();
inputControls.Player.Move.performed +=
inputControls => movementInput = inputControls.ReadValue<Vector2>();
inputControls.Player.Jump.performed +=
inputControls => jumpInput = inputControls.ReadValueAsButton();
inputControls.Player.Run.performed +=
inputControls => isRun = inputControls.ReadValueAsButton();
// inputControls.PlayerMovment.Camera.performed += i => cameraInput = i.ReadValue<Vector2>();
}
inputControls.Enable();
}
public void OnDisable()
{
inputControls.Disable();
}
public void UpdateMoveInput()
{
horizontal = movementInput.x;
vertical = movementInput.y;
moveAmount = Mathf.Clamp01(Mathf.Abs(horizontal) + Mathf.Abs(vertical));
// mouseX = cameraInput.x;
// mouseY = cameraInput.y;
}
}
}
动画脚本
using UnityEngine;
namespace Player.Animation
{
public class AnimationHandler : MonoBehaviour
{
[HideInInspector]
public Animator anim;
[HideInInspector]public bool IsRun;
[HideInInspector]public bool IsWalk;
private int vertical;
private int horizontal;
public bool canRotate = true;
public void Initialize()
{
anim = GetComponent<Animator>();
vertical = Animator.StringToHash("Vertical");
horizontal = Animator.StringToHash("Horizontal");
}
public void UpdateAnimatorValues(bool isWalk,bool isRun,float verticalMovement, float horizontalMovement)
{
anim.SetBool("IsRun",isRun);
anim.SetBool("IsWalk",isWalk);
// if (isRun)
// {
verticalMovement = Mathf.Abs(verticalMovement);
horizontalMovement = Mathf.Abs(horizontalMovement);
anim.SetFloat(vertical,verticalMovement,0.1f,Time.deltaTime);
anim.SetFloat(horizontal,horizontalMovement,0.1f,Time.deltaTime);
// }
}
public void CanRotate()
{
canRotate = true;
}
public void StopRotation()
{
canRotate = false;
}
}
}
人物引擎
using Player.Animation;
using UnityEngine;
namespace Player.Common
{
public class PlayerMotor : MonoBehaviour
{
[Header("基本属性")]
private InputSystem inputSystem;
private Rigidbody rigidbody;
[HideInInspector]
public Transform playerTransForm;
[HideInInspector]
public AnimationHandler animationHandler;
[Header("移动参数")]
public float WalkSpeed = 5f;
public float RunSpeed = 12f;
private Vector3 moveDirection;
public float RotateSpeed = 5f;
void Start()
{
inputSystem = GetComponent<InputSystem>();
rigidbody = GetComponent<Rigidbody>();
playerTransForm = GetComponent<Transform>();
animationHandler = GetComponentInChildren<AnimationHandler>();
}
void Update()
{
PlayerMove();
PlayerRotate(moveDirection);
}
private void PlayerMove()
{
inputSystem.UpdateMoveInput();
moveDirection = new Vector3(inputSystem.horizontal,0,inputSystem.vertical);
moveDirection *= WalkSpeed;
rigidbody.velocity = moveDirection;
animationHandler.Initialize();
bool isWalk = (Mathf.Abs(inputSystem.horizontal) > 0 || Mathf.Abs(inputSystem.vertical) > 0) ? true : false;
animationHandler.UpdateAnimatorValues(isWalk,inputSystem.isRun,inputSystem.vertical,inputSystem.horizontal);
}
private void PlayerRotate(Vector3 target)
{
if (Vector3.zero.Equals(target)) return;
Quaternion tr = Quaternion.LookRotation(target);
Quaternion targetRotation = Quaternion.Slerp(playerTransForm.rotation,tr,RotateSpeed*Time.deltaTime);
playerTransForm.rotation = targetRotation;
}
}
}