quality-control/Assets/Plugins/KinematicCharacterController/ExampleCharacter/Scripts/ExamplePlayer.cs

99 lines
3.8 KiB
C#
Raw Permalink Normal View History

2024-08-16 22:50:47 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using KinematicCharacterController;
using KinematicCharacterController.Examples;
namespace KinematicCharacterController.Examples
{
public class ExamplePlayer : MonoBehaviour
{
public ExampleCharacterController Character;
public ExampleCharacterCamera CharacterCamera;
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()
{
2024-08-20 01:30:10 +00:00
//Cursor.lockState = CursorLockMode.Locked;
2024-08-16 22:50:47 +00:00
// Tell camera to follow transform
CharacterCamera.SetFollowTransform(Character.CameraFollowPoint);
// Ignore the character's collider(s) for camera obstruction checks
CharacterCamera.IgnoredColliders.Clear();
CharacterCamera.IgnoredColliders.AddRange(Character.GetComponentsInChildren<Collider>());
}
private void Update()
{
2024-08-20 01:30:10 +00:00
//if (Input.GetMouseButtonDown(0))
//{
// Cursor.lockState = CursorLockMode.Locked;
//}
2024-08-16 22:50:47 +00:00
HandleCharacterInput();
}
private void LateUpdate()
{
// Handle rotating the camera along with physics movers
if (CharacterCamera.RotateWithPhysicsMover && Character.Motor.AttachedRigidbody != null)
{
CharacterCamera.PlanarDirection = Character.Motor.AttachedRigidbody.GetComponent<PhysicsMover>().RotationDeltaFromInterpolation * CharacterCamera.PlanarDirection;
CharacterCamera.PlanarDirection = Vector3.ProjectOnPlane(CharacterCamera.PlanarDirection, Character.Motor.CharacterUp).normalized;
}
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
CharacterCamera.UpdateWithInput(Time.deltaTime, scrollInput, lookInputVector);
// Handle toggling zoom level
if (Input.GetMouseButtonDown(1))
{
CharacterCamera.TargetDistance = (CharacterCamera.TargetDistance == 0f) ? CharacterCamera.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 = CharacterCamera.Transform.rotation;
characterInputs.JumpDown = Input.GetKeyDown(KeyCode.Space);
characterInputs.CrouchDown = Input.GetKeyDown(KeyCode.C);
characterInputs.CrouchUp = Input.GetKeyUp(KeyCode.C);
// Apply inputs to character
Character.SetInputs(ref characterInputs);
}
}
}