using KinematicCharacterController.Examples; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace CustomUse { public class MyCustomPlayer : MonoBehaviour { public MyCustomExampleCharacterCamera OrbitCamera; public MyCharacterController Character; public Transform CameraFollowPoint; private const string MouseXInput = "Mouse X"; private const string MouseYInput = "Mouse Y"; private const string MouseScrollInput = "Mouse ScrollWheel"; private const string HorizontalInput = "Horizontal"; private const string VerticalInput = "Vertical"; private void Start() { //告诉摄像机跟随变换 OrbitCamera.SetFollowTransform(CameraFollowPoint); //忽略角色的碰撞器进行镜头障碍检查 OrbitCamera.IgnoredColliders.Clear(); OrbitCamera.IgnoredColliders.AddRange(Character.GetComponentsInChildren()); } private void Update() { //人物控制器输入 KeyboardInput(); } private void LateUpdate() { //相机输入 CameraInput(); } /// /// 键盘输入 /// public void KeyboardInput() { PlayerCharacterInputs characterInputs = new PlayerCharacterInputs(); //相机垂直,水平,相机旋转, characterInputs.MoveAxisForward = Input.GetAxisRaw(VerticalInput); characterInputs.MoveAxisRight = Input.GetAxisRaw(HorizontalInput); characterInputs.CameraRotation = OrbitCamera.Transform.rotation; //键盘按键输入 // Character.SetInputs(ref characterInputs); //应用到人形控制器中 Character.SetInputs(ref characterInputs); } ///相机输入 private void CameraInput() { //输入 xy float mouseLookAxisUp = Input.GetAxisRaw(MouseYInput); float mouseLookAxisRight = Input.GetAxisRaw(MouseXInput); Vector3 lookInputVector = new Vector3(mouseLookAxisRight, mouseLookAxisUp, 0f); // 输入缩放相机(在WebGL中禁用,因为它可能会导致问题) float scrollInput = -Input.GetAxis(MouseScrollInput); #if UNITY_WEBGL scrollInput = 0f; #endif //应用到摄像机中 OrbitCamera.UpdateWithInput(Time.deltaTime, scrollInput, lookInputVector); } } }