Player, player movement, camera control

This commit is contained in:
2024-12-07 21:48:47 +01:00
parent 54fe327198
commit 8969435fda
1052 changed files with 166612 additions and 6768 deletions

View File

@@ -0,0 +1,70 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using KinematicCharacterController;
using System;
namespace KinematicCharacterController.Walkthrough.PlayerCameraCharacterSetup
{
public class MyCharacterController : MonoBehaviour, ICharacterController
{
public KinematicCharacterMotor Motor;
private void Start()
{
// Assign to motor
Motor.CharacterController = this;
}
public void BeforeCharacterUpdate(float deltaTime)
{
// This is called before the motor does anything
}
public void UpdateRotation(ref Quaternion currentRotation, float deltaTime)
{
// This is called when the motor wants to know what its rotation should be right now
}
public void UpdateVelocity(ref Vector3 currentVelocity, float deltaTime)
{
// This is called when the motor wants to know what its velocity should be right now
}
public void AfterCharacterUpdate(float deltaTime)
{
// This is called after the motor has finished everything in its update
}
public bool IsColliderValidForCollisions(Collider coll)
{
// This is called after when the motor wants to know if the collider can be collided with (or if we just go through it)
return true;
}
public void OnGroundHit(Collider hitCollider, Vector3 hitNormal, Vector3 hitPoint, ref HitStabilityReport hitStabilityReport)
{
// This is called when the motor's ground probing detects a ground hit
}
public void OnMovementHit(Collider hitCollider, Vector3 hitNormal, Vector3 hitPoint, ref HitStabilityReport hitStabilityReport)
{
// This is called when the motor's movement logic detects a hit
}
public void ProcessHitStabilityReport(Collider hitCollider, Vector3 hitNormal, Vector3 hitPoint, Vector3 atCharacterPosition, Quaternion atCharacterRotation, ref HitStabilityReport hitStabilityReport)
{
// This is called after every hit detected in the motor, to give you a chance to modify the HitStabilityReport any way you want
}
public void PostGroundingUpdate(float deltaTime)
{
// This is called after the motor has finished its ground probing, but before PhysicsMover/Velocity/etc.... handling
}
public void OnDiscreteCollisionDetected(Collider hitCollider)
{
// This is called by the motor when it is detecting a collision that did not result from a "movement hit".
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 4b647278478c8694ab6b00ac51347de7
timeCreated: 1507245023
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,71 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using KinematicCharacterController;
using KinematicCharacterController.Examples;
using System.Linq;
namespace KinematicCharacterController.Walkthrough.PlayerCameraCharacterSetup
{
public class MyPlayer : MonoBehaviour
{
public ExampleCharacterCamera OrbitCamera;
public Transform CameraFollowPoint;
public MyCharacterController Character;
private Vector3 _lookInputVector = Vector3.zero;
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 = Character.GetComponentsInChildren<Collider>().ToList();
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
Cursor.lockState = CursorLockMode.Locked;
}
}
private void LateUpdate()
{
HandleCameraInput();
}
private void HandleCameraInput()
{
// Create the look input vector for the camera
float mouseLookAxisUp = Input.GetAxisRaw("Mouse Y");
float mouseLookAxisRight = Input.GetAxisRaw("Mouse X");
_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("Mouse ScrollWheel");
#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;
}
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 0659d469a6cbf3448a556b714972454c
timeCreated: 1507245023
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: