Unity Third Person Controller代码解析

Third Person Controller

https://assetstore.unity.com/packages/tools/utilities/third-person-controller-basic-locomotion-free-82048

 

脚本

绑定在角色模型上的主要是这两个脚步,以下简称Controller脚本和Input脚本

 

先分析vThirdPersonInput

ThirdPersonInput主要是对输入信号进行处理,并把相关的值传递给Controller

#region Variables       
[Header("Controller Input")]

public string horizontalInput = "Horizontal";
public string verticallInput = "Vertical";
/*空格键控制jump*/
public KeyCode jumpInput = KeyCode.Space;
/*侧向移动模式,控制角色侧向移动时候,是转身移动还是侧向移动*/
public KeyCode strafeInput = KeyCode.Tab;
/*sprint冲刺*/
public KeyCode sprintInput = KeyCode.LeftShift;

[Header("Camera Input")]
/*控制相机移动*/
public string rotateCameraXInput = "Mouse X";
public string rotateCameraYInput = "Mouse Y";
[HideInInspector] public vThirdPersonController cc;
[HideInInspector] public vThirdPersonCamera tpCamera;
[HideInInspector] public Camera cameraMain;
public string horizontalInput = "Horizontal";

cc.input.x = Input.GetAxis(horizontalInput);//horizontalInput对应键盘上面的左右箭头
cc.input.x = Input.GetAxis(verticallInput);//verticallInput对应键盘上面的上下箭头

 在Input中会调用Controller的updateAnimator方法

protected virtual void Update()
{
    InputHandle();                  // update the input methods
    cc.UpdateAnimator();            // updates the Animator Parameters
}

 

public virtual void UpdateAnimator()
{
    if (animator == null || !animator.enabled) return;

    animator.SetBool(vAnimatorParameters.IsStrafing, isStrafing); ;
    animator.SetBool(vAnimatorParameters.IsSprinting, isSprinting);
    animator.SetBool(vAnimatorParameters.IsGrounded, isGrounded);
    animator.SetFloat(vAnimatorParameters.GroundDistance, groundDistance);

    if (isStrafing)
    {
        animator.SetFloat(vAnimatorParameters.InputHorizontal, stopMove ? 0 : horizontalSpeed, strafeSpeed.animationSmooth, Time.deltaTime);
        animator.SetFloat(vAnimatorParameters.InputVertical, stopMove ? 0 : verticalSpeed, strafeSpeed.animationSmooth, Time.deltaTime);
    }
    else
    {
        animator.SetFloat(vAnimatorParameters.InputVertical, stopMove ? 0 : verticalSpeed, freeSpeed.animationSmooth, Time.deltaTime);
    }

    animator.SetFloat(vAnimatorParameters.InputMagnitude, stopMove ? 0f : inputMagnitude, isStrafing ? strafeSpeed.animationSmooth : freeSpeed.animationSmooth, Time.deltaTime);
}

updateAnimator是控制动画的关键函数,由于用的都是根动画,所以不需要计算位移量

 

Controller类的继承关系

vThirdPersonController : vThirdPersonAnimator

vThirdPersonAnimator : vThirdPersonMotor  发动机、马达

vThirdPersonMotor : MonoBehaviour

internal PhysicMaterial frictionPhysics, maxFrictionPhysics, slippyPhysics;         // create PhysicMaterial for the Rigidbody
protected v
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值