我们要控制角色的移动,能够所有细节都由自己来实现。控制角色模型的移动,同一时候移动摄影机,改变视角。当然Unity也提供了一些组件,能够让我们做更少的工作,实现我们所期望的功能。今天我们就一起系统来学习相关的内容吧。
- Charactor Controller(角色控制器)
- 两个重要的函数
- CharacterController controller= GetComponent<CharacterController>();
- Vector3 forward= transform.TransformDirection(Vector3.forward);
- float curSpeed = speed * Input.GetAxis ("Vertical");
- ontroller.SimpleMove(forward * curSpeed);
- 第一人称控制器
- 第三人称控制器
效果图
- 核心代码解读
- function Update () {
- //获得键盘或者摇杆上的方向量(键盘默认是方向键和wasd键控制方向)
- var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
- //有方向变化
- if (directionVector != Vector3.zero) {
- //取得方向向量的长度
- var directionLength = directionVector.magnitude;
- //normal 方向向量(向量/长度)
- directionVector = directionVector / directionLength;
- //修正长度不大于1
- directionLength = Mathf.Min(1, directionLength);
- //为了效果更明显,长度平方扩大
- directionLength = directionLength * directionLength;
- //用我们修正后的长度来修正方向向量
- directionVector = directionVector * directionLength;
- }
- // 设置移动的方向
- motor.inputMoveDirection = transform.rotation * directionVector;
- //设置跳跃(默认键盘是空格键)
- motor.inputJump = Input.GetButton("Jump");
- }
第三人称角色控制器ThirdPersonController.js
- function Update() {
- if (!isControllable)
- {
- // 清除全部的输入,假设不处于控制
- Input.ResetInputAxes();
- }
- //按了跳跃键
- if (Input.GetButtonDown ("Jump"))
- {
- //设置按下跳跃键的时间
- lastJumpButtonTime = Time.time;
- }
- //控制角色的方向
- UpdateSmoothedMovementDirection();
- //处理重力
- ApplyGravity ();
- // 处理跳跃逻辑
- ApplyJumping ();
- //计算实际的动作(移动方向和重力方向的)
- var movement = moveDirection * moveSpeed + Vector3 (0, verticalSpeed, 0) + inAirVelocity;
- movement *= Time.deltaTime;
- // 移动角色
- var controller : CharacterController = GetComponent(CharacterController);
- collisionFlags = controller.Move(movement);
- // 动画处理
- if(_animation) {
- if(_characterState == CharacterState.Jumping) //跳跃
- {
- if(!jumpingReachedApex) { //没到达最高点,继续向上
- _animation[jumpPoseAnimation.name].speed = jumpAnimationSpeed;
- _animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
- _animation.CrossFade(jumpPoseAnimation.name);
- } else { //到了最高点,速度方向改变
- _animation[jumpPoseAnimation.name].speed = -landAnimationSpeed;
- _animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
- _animation.CrossFade(jumpPoseAnimation.name);
- }
- }
- else
- {
- if(controller.velocity.sqrMagnitude < 0.1) { //没有方向移动
- _animation.CrossFade(idleAnimation.name);//空暇状态
- }
- else
- {
- if(_characterState == CharacterState.Running) { //奔跑
- _animation[runAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0, runMaxAnimationSpeed);
- _animation.CrossFade(runAnimation.name);
- }
- else if(_characterState == CharacterState.Trotting) { //疾走
- _animation[walkAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0, trotMaxAnimationSpeed);
- _animation.CrossFade(walkAnimation.name);
- }
- else if(_characterState == CharacterState.Walking) { //普通走动
- _animation[walkAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0, walkMaxAnimationSpeed);
- _animation.CrossFade(walkAnimation.name);
- }
- }
- }
- }
- //在地上
- if (IsGrounded())
- {
- //旋转方向
- transform.rotation = Quaternion.LookRotation(moveDirection);
- }
- else
- {
- //在空中忽略y轴旋转
- var xzMove = movement;
- xzMove.y = 0;
- if (xzMove.sqrMagnitude > 0.001)
- {
- transform.rotation = Quaternion.LookRotation(xzMove);
- }
- }
- // 跳跃状态,刚好到达地面
- if (IsGrounded())
- {
- //记录到达地面的时间
- lastGroundedTime = Time.time;
- //空中的速度设置为0
- inAirVelocity = Vector3.zero;
- //更改相关状态
- if (jumping)
- {
- jumping = false;
- SendMessage("DidLand", SendMessageOptions.DontRequireReceiver);
- }
- }
- }
第三人控制器摄像机脚本ThirdPersonCamera.js
- function Apply (dummyTarget : Transform, dummyCenter : Vector3)
- {
- // 没有目标
- if (!controller)
- return;
- //目标中心和顶点
- var targetCenter = _target.position + centerOffset;
- var targetHead = _target.position + headOffset;
- //计算目标旋转角度和当前角度
- var originalTargetAngle = _target.eulerAngles.y;
- var currentAngle = cameraTransform.eulerAngles.y;
- // 调整目标的真实角度
- var targetAngle = originalTargetAngle;
- //按了Fire2(alt)摄像机的方向改变会加快
- if (Input.GetButton("Fire2"))
- snap = true;
- if (snap)
- {
- // 靠近角色了,重置snap
- if (AngleDistance (currentAngle, originalTargetAngle) < 3.0)
- snap = false;
- //计算当前角度
- currentAngle = Mathf.SmoothDampAngle(currentAngle, targetAngle, angleVelocity, snapSmoothLag, snapMaxSpeed);
- }
- // Normal 摄像机动作
- else
- {
- //延迟一点时间
- if (controller.GetLockCameraTimer () < lockCameraTimeout)
- {
- targetAngle = currentAngle;
- }
- // 向后走的时候锁住摄像机
- if (AngleDistance (currentAngle, targetAngle) > 160 && controller.IsMovingBackwards ())
- targetAngle += 180;//旋转180
- //插值改变相机角度
- currentAngle = Mathf.SmoothDampAngle(currentAngle, targetAngle, angleVelocity, angularSmoothLag, angularMaxSpeed);
- }
- //当跳跃时
- // When jumping don't move camera upwards but only down!
- if (controller.IsJumping ())
- {
- // 计算目标的高度
- var newTargetHeight = targetCenter.y + height;
- if (newTargetHeight < targetHeight || newTargetHeight - targetHeight > 5)
- targetHeight = targetCenter.y + height;
- }
- // 走动时,改变高度
- else
- {
- targetHeight = targetCenter.y + height;
- }
- // 计算当前高度
- var currentHeight = cameraTransform.position.y;
- currentHeight = Mathf.SmoothDamp (currentHeight, targetHeight, heightVelocity, heightSmoothLag);
- // 按角度旋转、
- var currentRotation = Quaternion.Euler (0, currentAngle, 0);
- //更新相机位置
- cameraTransform.position = targetCenter;
- cameraTransform.position += currentRotation * Vector3.back * distance;
- // 设置相机的高度
- cameraTransform.position.y = currentHeight;
- //摄像机一直朝向目标
- SetUpRotation(targetCenter, targetHead);
- }
- 总结
角色控制,能够方便的控制游戏的视角。在非常多游戏中,能够直接使用该组件,降低我们的反复开发工作
- 远吗
pan.baidu.com/s/1BwArJ
- 參考文档