using System.Collections; using System.Collections.Generic; using UnityEngine; using KinematicCharacterController; using KinematicCharacterController.Examples; using System.Linq; namespace KinematicCharacterController.Walkthrough.BasicMovement { 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()); } private void Update() { if (Input.GetMouseButtonDown(0)) { Cursor.lockState = CursorLockMode.Locked; } HandleCharacterInput(); } private void LateUpdate() { HandleCameraInput(); } private void HandleCameraInput() { // Create the look input vector for the camera float mouseLookAxisUp = Input.GetAxisRaw(MouseYInput); float mouseLookAxisRight = Input.GetAxisRaw(MouseXInput); Vector3 lookInputVector = new Vector3(mouseLookAxisRight, mouseLookAxisUp, 0f); // 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); #if UNITY_WEBGL scrollInput = 0f; #endif // Apply inputs to the camera OrbitCamera.UpdateWithInput(Time.deltaTime, scrollInput, lookInputVector); // Handle toggling zoom level if (Input.GetMouseButtonDown(1)) { OrbitCamera.TargetDistance = (OrbitCamera.TargetDistance == 0f) ? OrbitCamera.DefaultDistance : 0f; } } private void HandleCharacterInput() { PlayerCharacterInputs characterInputs = new PlayerCharacterInputs(); // Build the CharacterInputs struct characterInputs.MoveAxisForward = Input.GetAxisRaw(VerticalInput); characterInputs.MoveAxisRight = Input.GetAxisRaw(HorizontalInput); characterInputs.CameraRotation = OrbitCamera.Transform.rotation; // Apply inputs to character Character.SetInputs(ref characterInputs); } } }