生活随笔
收集整理的這篇文章主要介紹了
Unity手游之路lt;七gt;角色控制器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我們要控制角色的移動,能夠所有細節都由自己來實現。控制角色模型的移動,同一時候移動攝影機,改變視角。當然Unity也提供了一些組件,能夠讓我們做更少的工作,實現我們所期望的功能。今天我們就一起系統來學習相關的內容吧。
Charactor Controller(角色控制器) "角色控制器同意你在受制于碰撞的情況下非常easy的進行運動,而不用處理剛體。角色控制器不受力的影響,只當你調用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到合適的位置
我們就能夠看到效果了,以第一人稱的視角移動,巡視整個場景。鼠標控制總體視角,方向鍵或者wasdbutton控制攝像機的移動。
非常多角色扮演游戲(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);?? }?? 角色控制,能夠方便的控制游戲的視角。在非常多游戲中,能夠直接使用該組件,降低我們的反復開發工作
pan.baidu.com/s/1BwArJ
http://unity3d.com/learn
轉載于:https://www.cnblogs.com/blfshiye/p/3802883.html
總結
以上是生活随笔 為你收集整理的Unity手游之路lt;七gt;角色控制器 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。