删除飞行功能,改为跳跃
先删除¥Player里的Configurable Joint
勾选一下使用重力
在PlayerInput里
public class PlayerInput : MonoBehaviour
{
...
//private ConfigurableJoint joint;
void Start()
{
//joint = GetComponent<ConfigurableJoint>();
}
void Update()
{
...
//飞行
Vector3 force = Vector3.zero;
if (Input.GetButton("Jump")) {
force = Vector3.up * thrusterForce;
controller.Thrust(force);
//joint.yDrive = new JointDrive
//{
// positionSpring = 0f,
// positionDamper = 0f,
// maximumForce = 0f,
//};
}
//else
//{
// joint.yDrive = new JointDrive
// {
// positionSpring = 20f,
// positionDamper = 0f,
// maximumForce = 40f,
// };
//}
//controller.Rotate(yRotation, xRotation);
}
}
在PlayerController里
public class PlayerController : MonoBehaviour
{
...
private void PerformMovement()
{
...
if (thrusterForce != Vector3.zero)
{
...
//跳跃是给一下力,而不是持续的力,所以力要清零
thrusterForce = Vector3.zero;
}
}
}
判断双脚是否离地
在PlayerInput里
public class PlayerInput : MonoBehaviour
{
[SerializeField]
private float thrusterForce = 100f;
private float distToGround = 0f;
void Start()
{
//unity自带的api来判断物体里地面的距离
//返回中心点到碰撞检测下边界的长度
distToGround = GetComponent<Collider>().bounds.extents.y;
}
void Update()
{
...
//跳跃
if (Input.GetButton("Jump"))
{
if(Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f))
{
Vector3 force = Vector3.up * thrusterForce;
controller.Thrust(force);
}
}
}
}
*修改重力
将小球换成好的模型
修改碰撞体
修改向下看会看到自己身体的bug
在PlayerSetup里
public class PlayerSetup : NetworkBehaviour
{
...
public override void OnNetworkSpawn()
{
...
if (!IsLocalPlayer)
{
SetLayerMaskForAllChildren(transform, LayerMask.NameToLayer("Remote Player"));
...
}
else
{
sceneCamera = Camera.main;
if(sceneCamera != null)
{
SetLayerMaskForAllChildren(transform, LayerMask.NameToLayer("Player"));
...
}
}
...
}
//写一个递归函数将所有子节点的物体都换上指定的图层
private void SetLayerMaskForAllChildren(Transform transform, LayerMask layerMask)
{
transform.gameObject.layer = layerMask;
for(int i = 0; i < transform.childCount; i++)
{
SetLayerMaskForAllChildren(transform.GetChild(i), layerMask);
}
}
...
}
实现走路动画
创建Animator Controller来管理动画
实现动画
处理状态机图
编写参数direction
在PlayerController里
//改为NetworkBehaviour
public class PlayerController : NetworkBehaviour
{
...
private float eps = 0.01f;
//上一帧的位置
private Vector3 lastFramePosition = Vector3.zero;
private Animator animator;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
//随便初始化一下位置
lastFramePosition = transform.position;
animator = GetComponentInChildren<Animator>();
}
private void PerformAnimation()
{
//两个位置的差值为一个向量
Vector3 deltaPosition = transform.position - lastFramePosition;
//更新上一帧位置
lastFramePosition = transform.position;
//(以forward为例)如果是正的,代表这个差值,有向transform.forward
float forward = Vector3.Dot(deltaPosition, transform.forward);
float right = Vector3.Dot(deltaPosition, transform.right);
int direction = 0;
if(forward > eps)
{
direction = 1;
}
else if(forward < -eps)
{
if(right > eps)
{
direction = 4; //右后
}
else if (right < -eps)
{
direction = 6; // 左后
}
else
{
direction = 5; // 后
}
}else if(right > eps)
{
direction = 3;
}
else if(right < -eps)
{
direction = 7;
}
animator.SetInteger("direction", direction);
}
...
//好像是unity版本问题,如果PerformAnimation(), 放进Update()里会有问题
private void FixedUpdate()
{
if (IsLocalPlayer)
{
PerformMovement();
PerformRotation();
}
PerformAnimation();
}
private void Update()
{
}
}