175 lines
5.9 KiB
C#
175 lines
5.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using KinematicCharacterController;
|
|
using KinematicCharacterController.Examples;
|
|
using System.Linq;
|
|
|
|
namespace KinematicCharacterController.Walkthrough.NoClipState
|
|
{
|
|
public class MyPlayer : MonoBehaviour
|
|
{
|
|
public ExampleCharacterCamera OrbitCamera;
|
|
public Transform CameraFollowPoint;
|
|
public MyCharacterController Character;
|
|
|
|
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()
|
|
{
|
|
// Cursor.lockState = CursorLockMode.Locked;
|
|
|
|
// Tell camera to follow transform
|
|
OrbitCamera.SetFollowTransform(CameraFollowPoint);
|
|
|
|
|
|
// Ignore the character's collider(s) for camera obstruction checks
|
|
OrbitCamera.IgnoredColliders.Clear();
|
|
OrbitCamera.IgnoredColliders.AddRange(Character.GetComponentsInChildren<Collider>());
|
|
|
|
|
|
|
|
playerCharacterInputs.NoClipDown = true;
|
|
}
|
|
PlayerCharacterInputs playerCharacterInputs = new PlayerCharacterInputs();
|
|
private void Update()
|
|
{
|
|
//if (Input.GetMouseButton(0))
|
|
//{
|
|
// Cursor.lockState =CursorLockMode.None;
|
|
//}
|
|
Cursor.visible = true;
|
|
|
|
HandleCharacterInput();
|
|
}
|
|
private int maxView = 80;
|
|
private int minView = 10;
|
|
private float slideSpeed = 20;
|
|
private void LateUpdate()
|
|
{
|
|
//获取虚拟按键(鼠标中轴滚轮)
|
|
//float mouseCenter = Input.GetAxis("Mouse ScrollWheel");
|
|
//if (mouseCenter < 0)
|
|
//{
|
|
|
|
// if (OrbitCamera.transform.GetComponent<Camera>().fieldOfView <= maxView) {
|
|
|
|
// OrbitCamera.transform.GetComponent<Camera>().fieldOfView += 10 * slideSpeed * Time.deltaTime;
|
|
|
|
|
|
// }
|
|
// //mouseCenter >0 = 正数 往前滑动,放大镜头
|
|
//}
|
|
//else if (mouseCenter > 0) {
|
|
|
|
// if (OrbitCamera.transform.GetComponent<Camera>().fieldOfView >= minView)
|
|
// {
|
|
|
|
|
|
// OrbitCamera.transform.GetComponent<Camera>().fieldOfView -= 10 * slideSpeed * Time.deltaTime;
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
//相机移动
|
|
HandleCameraInput();
|
|
}
|
|
private float _zoomSpeed = 1.6f;// 80.0f;
|
|
public float _ZoomSpeed { get => _zoomSpeed /** ZXK.GYJQR.GameManager.Instance._CurMouseFlexible*/; }
|
|
private float _minZoom = 5.0f;
|
|
private float _maxZoom = 100f;
|
|
private void HandleCameraInput()
|
|
{
|
|
// Create the look input vector for the camera
|
|
//float mouseLookAxisUp = Input.GetAxisRaw(MouseYInput);
|
|
//float mouseLookAxisRight = Input.GetAxisRaw(MouseXInput);
|
|
float mouseLookAxisUp = 0;
|
|
float mouseLookAxisRight = 0;
|
|
if (CG.UTility.PopUpMng._TriAble)
|
|
{
|
|
mouseLookAxisUp = Input.GetAxis(MouseYInput);
|
|
mouseLookAxisRight = Input.GetAxis(MouseXInput);
|
|
}
|
|
if (Input.GetKey(KeyCode.Mouse1))
|
|
{
|
|
Vector3 lookInputVector = new Vector3(mouseLookAxisRight, mouseLookAxisUp, 0f);
|
|
float scrollInput = 0;
|
|
OrbitCamera.UpdateWithInput(Time.deltaTime, scrollInput, lookInputVector);
|
|
OrbitCamera.TargetDistance = 0;
|
|
}
|
|
|
|
|
|
// Prevent moving the camera while the cursor isn't locked
|
|
//if (Cursor.lockState != CursorLockMode.Locked)
|
|
//{
|
|
// lookInputVector = Vector3.zero;
|
|
//}
|
|
|
|
// Input for zooming the camera (disabled in WebGL because it can cause problems)
|
|
// float scrollInput = -Input.GetAxis(MouseScrollInput);
|
|
// float scrollInput = 0;
|
|
|
|
|
|
// float scrollInput = 0;
|
|
//#if UNITY_WEBGL
|
|
// scrollInput = 0f;
|
|
//#endif
|
|
|
|
//永远传入0
|
|
// WDebug.Log("scrollInput!!!!!" + scrollInput);
|
|
|
|
// Apply inputs to the camera
|
|
// OrbitCamera.UpdateWithInput(Time.deltaTime, scrollInput, lookInputVector);
|
|
|
|
|
|
// Handle toggling zoom level
|
|
// 鼠标右键缩放值 0-6
|
|
//if (Input.GetMouseButtonDown(1))
|
|
//{
|
|
// WDebug.Log("?????");
|
|
// OrbitCamera.TargetDistance = (OrbitCamera.TargetDistance == 0f) ? OrbitCamera.DefaultDistance : 0f;
|
|
|
|
// WDebug.Log(OrbitCamera.TargetDistance+"????????????????????");
|
|
//}
|
|
//永远是0
|
|
// OrbitCamera.TargetDistance = 0;
|
|
}
|
|
|
|
private void HandleCharacterInput()
|
|
{
|
|
PlayerCharacterInputs characterInputs = new PlayerCharacterInputs();
|
|
|
|
//检测按键输入
|
|
|
|
|
|
characterInputs.MoveAxisForward = Input.GetAxisRaw(VerticalInput);
|
|
characterInputs.MoveAxisRight = Input.GetAxisRaw(HorizontalInput);
|
|
characterInputs.JumpHeld = Input.GetKey(KeyCode.Q);
|
|
characterInputs.CrouchHeld = Input.GetKey(KeyCode.E);
|
|
characterInputs.CameraRotation = OrbitCamera.Transform.rotation;
|
|
|
|
// characterInputs.JumpDown = Input.GetKeyDown(KeyCode.Space);
|
|
|
|
|
|
|
|
|
|
// characterInputs.CrouchDown = Input.GetKeyDown(KeyCode.C);
|
|
// characterInputs.CrouchUp = Input.GetKeyUp(KeyCode.C);
|
|
|
|
|
|
// characterInputs.NoClipDown = Input.GetKeyUp(KeyCode.Q);
|
|
|
|
|
|
// Apply inputs to character
|
|
Character.SetInputs(ref characterInputs);
|
|
}
|
|
}
|
|
} |