标签:
向原创致敬http://blog.csdn.net/janeky/article/details/17406095
我们要控制角色的移动,可以全部细节都由自己来实现。控制角色模型的移动,同时移动摄影机,改变视角。当然Unity也提供了一些组件,可以让我们做更少的工作,实现我们所期望的功能。今天我们就一起系统来学习相关的内容吧。
- Charactor Controller(角色控制器)
"角色控制器允许你在受制于碰撞的情况下很容易的进行运动,而不用处理刚体。角色控制器不受力的影响,仅仅当你调用Move函数时才运动。然后它将执行运动,但是受制于碰撞。"(---from unity3d官方文档) 我们通常在人物模型上加上这个组件后,就可以控制模型的移动了。要注意的一点是。加了角色控制器后,他就不受重力影响。所以要自己在move函数中处理重力的情况。即我们要自己出来y轴方向上的速度变化。
1.function SimpleMove (speed : Vector3) : bool
以一定的速度移动。将忽略Y轴上的速度。单位是m/s。重力被自动应用。建议每帧只调用一次Move或者SimpleMove。返回值是是否着地。
例子
- CharacterController controller= GetComponent<CharacterController>();
- Vector3 forward= transform.TransformDirection(Vector3.forward);
- float curSpeed = speed * Input.GetAxis ("Vertical");
- ontroller.SimpleMove(forward * curSpeed);
2.function Move (motion : Vector3) : CollisionFlags
通过动力来移动控制器。动力只受限制于碰撞。它将沿着碰撞器滑动。这个函数不应用任何重力
如果只是单纯控制玩家的移动,那么用Character Controller足够了。如果还涉及到视角的切换。Unity提供了相关的组件。在项目中引入Character Controller(Asset->Import Asset),就可以将角色控制器组件导入我们的项目了。
经典的游戏CS就是第一人称视角的,摄像机就是我们的视角。人物的移动,导致视角的移动。(源码first.unity)
1.删除默认的摄像机
2.新建一个地形Terrain
3.从角色控制器组件中引入 First Person Controller到项目中
4.拖动First Person Controller到合适的位置
我们就可以看到效果了,以第一人称的视角移动,巡视整个场景。鼠标控制整体视角,方向键或者wasd按钮控制摄像机的移动。
很多角色扮演游戏(wow,dota)常用到第三人称视角。摄像机离我们的角色保持有一定距离,可以详细看到我们所扮演角色的各种行为动作。(源码third.unity)
1.创建一个地形
2.引入3rd Person Controller组件到项目中
3.修改默认摄像机的Tag为MainCamera
4.选中3rd Person Controller组件,将其 Third Person Camera 设置为MainCamera
可以看到效果了,可以看到扮演的角色。方向键或者wasd按键可以控制角色的移动,同时可以发现整个视角也会跟着移动
效果图
第一人称控制器脚本FPSInputController.js
- function Update () {
-
- var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
-
-
- if (directionVector != Vector3.zero) {
-
- var directionLength = directionVector.magnitude;
-
- directionVector = directionVector / directionLength;
-
-
- 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
- {
-
- var xzMove = movement;
- xzMove.y = 0;
- if (xzMove.sqrMagnitude > 0.001)
- {
- transform.rotation = Quaternion.LookRotation(xzMove);
- }
- }
-
- if (IsGrounded())
- {
-
- lastGroundedTime = Time.time;
-
- 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;
-
- if (Input.GetButton("Fire2"))
- snap = true;
-
- if (snap)
- {
-
- if (AngleDistance (currentAngle, originalTargetAngle) < 3.0)
- snap = false;
-
- currentAngle = Mathf.SmoothDampAngle(currentAngle, targetAngle, angleVelocity, snapSmoothLag, snapMaxSpeed);
- }
-
- else
- {
-
- if (controller.GetLockCameraTimer () < lockCameraTimeout)
- {
- targetAngle = currentAngle;
- }
-
- if (AngleDistance (currentAngle, targetAngle) > 160 && controller.IsMovingBackwards ())
- targetAngle += 180;
-
- currentAngle = Mathf.SmoothDampAngle(currentAngle, targetAngle, angleVelocity, angularSmoothLag, angularMaxSpeed);
- }
-
-
- 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);
- }
角色控制,可以方便的控制游戏的视角。在很多游戏中,可以直接使用该组件,减少我们的重复开发工作
http://pan.baidu.com/s/1BwArJ
http://unity3d.com/learn
Unity手游之路<七>角色控制器
标签:
原文地址:http://www.cnblogs.com/lindan929/p/4346628.html