working on fpp locomotion, camera spring
This commit is contained in:
8
Assets/jelycho/Code/KinematicCharacterController.meta
Normal file
8
Assets/jelycho/Code/KinematicCharacterController.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0578aed1892493947879cf69c357bde0
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,14 @@
|
||||
fileFormatVersion: 2
|
||||
guid: aadf824e76564404c8d43d99803c60a1
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 99131
|
||||
packageName: Kinematic Character Controller
|
||||
packageVersion: 3.4.4
|
||||
assetPath: Assets/KinematicCharacterController/APIReference.zip
|
||||
uploadId: 505601
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c8854cb84deea8146b0cc7833ff338fd
|
||||
folderAsset: yes
|
||||
timeCreated: 1499134826
|
||||
licenseType: Store
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 220667f043f505a4eb52d0208e14818e
|
||||
folderAsset: yes
|
||||
timeCreated: 1504492780
|
||||
licenseType: Store
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,28 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
namespace KinematicCharacterController
|
||||
{
|
||||
[CustomEditor(typeof(KinematicCharacterMotor))]
|
||||
public class KinematicCharacterMotorEditor : Editor
|
||||
{
|
||||
protected virtual void OnSceneGUI()
|
||||
{
|
||||
KinematicCharacterMotor motor = (target as KinematicCharacterMotor);
|
||||
if (motor)
|
||||
{
|
||||
Vector3 characterBottom = motor.transform.position + (motor.Capsule.center + (-Vector3.up * (motor.Capsule.height * 0.5f)));
|
||||
|
||||
Handles.color = Color.yellow;
|
||||
Handles.CircleHandleCap(
|
||||
0,
|
||||
characterBottom + (motor.transform.up * motor.MaxStepHeight),
|
||||
Quaternion.LookRotation(motor.transform.up, motor.transform.forward),
|
||||
motor.Capsule.radius + 0.1f,
|
||||
EventType.Repaint);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7db3ed6d74cbf2b489cab5e1586bed7a
|
||||
timeCreated: 1518400757
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 99131
|
||||
packageName: Kinematic Character Controller
|
||||
packageVersion: 3.4.4
|
||||
assetPath: Assets/KinematicCharacterController/Core/Editor/KinematicCharacterMotorEditor.cs
|
||||
uploadId: 505601
|
||||
@@ -0,0 +1,21 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
namespace KinematicCharacterController
|
||||
{
|
||||
[CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
|
||||
public class ReadOnlyPropertyDrawer : PropertyDrawer
|
||||
{
|
||||
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
|
||||
{
|
||||
return EditorGUI.GetPropertyHeight(property, label, true);
|
||||
}
|
||||
|
||||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
||||
{
|
||||
GUI.enabled = false;
|
||||
EditorGUI.PropertyField(position, property, label, true);
|
||||
GUI.enabled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0d4351ab88853824b85a8f458928d825
|
||||
timeCreated: 1504492796
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 99131
|
||||
packageName: Kinematic Character Controller
|
||||
packageVersion: 3.4.4
|
||||
assetPath: Assets/KinematicCharacterController/Core/Editor/ReadOnlyPropertyDrawer.cs
|
||||
uploadId: 505601
|
||||
@@ -0,0 +1,50 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace KinematicCharacterController
|
||||
{
|
||||
public interface ICharacterController
|
||||
{
|
||||
/// <summary>
|
||||
/// This is called when the motor wants to know what its rotation should be right now
|
||||
/// </summary>
|
||||
void UpdateRotation(ref Quaternion currentRotation, float deltaTime);
|
||||
/// <summary>
|
||||
/// This is called when the motor wants to know what its velocity should be right now
|
||||
/// </summary>
|
||||
void UpdateVelocity(ref Vector3 currentVelocity, float deltaTime);
|
||||
/// <summary>
|
||||
/// This is called before the motor does anything
|
||||
/// </summary>
|
||||
void BeforeCharacterUpdate(float deltaTime);
|
||||
/// <summary>
|
||||
/// This is called after the motor has finished its ground probing, but before PhysicsMover/Velocity/etc.... handling
|
||||
/// </summary>
|
||||
void PostGroundingUpdate(float deltaTime);
|
||||
/// <summary>
|
||||
/// This is called after the motor has finished everything in its update
|
||||
/// </summary>
|
||||
void AfterCharacterUpdate(float deltaTime);
|
||||
/// <summary>
|
||||
/// This is called after when the motor wants to know if the collider can be collided with (or if we just go through it)
|
||||
/// </summary>
|
||||
bool IsColliderValidForCollisions(Collider coll);
|
||||
/// <summary>
|
||||
/// This is called when the motor's ground probing detects a ground hit
|
||||
/// </summary>
|
||||
void OnGroundHit(Collider hitCollider, Vector3 hitNormal, Vector3 hitPoint, ref HitStabilityReport hitStabilityReport);
|
||||
/// <summary>
|
||||
/// This is called when the motor's movement logic detects a hit
|
||||
/// </summary>
|
||||
void OnMovementHit(Collider hitCollider, Vector3 hitNormal, Vector3 hitPoint, ref HitStabilityReport hitStabilityReport);
|
||||
/// <summary>
|
||||
/// This is called after every move hit, to give you an opportunity to modify the HitStabilityReport to your liking
|
||||
/// </summary>
|
||||
void ProcessHitStabilityReport(Collider hitCollider, Vector3 hitNormal, Vector3 hitPoint, Vector3 atCharacterPosition, Quaternion atCharacterRotation, ref HitStabilityReport hitStabilityReport);
|
||||
/// <summary>
|
||||
/// This is called when the character detects discrete collisions (collisions that don't result from the motor's capsuleCasts when moving)
|
||||
/// </summary>
|
||||
void OnDiscreteCollisionDetected(Collider hitCollider);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f13aa252bd3bf9943b1d65e68d93dcfb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 99131
|
||||
packageName: Kinematic Character Controller
|
||||
packageVersion: 3.4.4
|
||||
assetPath: Assets/KinematicCharacterController/Core/ICharacterController.cs
|
||||
uploadId: 505601
|
||||
@@ -0,0 +1,14 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace KinematicCharacterController
|
||||
{
|
||||
public interface IMoverController
|
||||
{
|
||||
/// <summary>
|
||||
/// This is called to let you tell the PhysicsMover where it should be right now
|
||||
/// </summary>
|
||||
void UpdateMovement(out Vector3 goalPosition, out Quaternion goalRotation, float deltaTime);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8ae7eceec45f1be4c8450edc46c0ad36
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 99131
|
||||
packageName: Kinematic Character Controller
|
||||
packageVersion: 3.4.4
|
||||
assetPath: Assets/KinematicCharacterController/Core/IMoverController.cs
|
||||
uploadId: 505601
|
||||
@@ -0,0 +1,33 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace KinematicCharacterController
|
||||
{
|
||||
[CreateAssetMenu]
|
||||
public class KCCSettings : ScriptableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// Determines if the system simulates automatically.
|
||||
/// If true, the simulation is done on FixedUpdate
|
||||
/// </summary>
|
||||
[Tooltip("Determines if the system simulates automatically. If true, the simulation is done on FixedUpdate")]
|
||||
public bool AutoSimulation = true;
|
||||
/// <summary>
|
||||
/// Should interpolation of characters and PhysicsMovers be handled
|
||||
/// </summary>
|
||||
[Tooltip("Should interpolation of characters and PhysicsMovers be handled")]
|
||||
public bool Interpolate = true;
|
||||
/// <summary>
|
||||
|
||||
/// Initial capacity of the system's list of Motors (will resize automatically if needed, but setting a high initial capacity can help preventing GC allocs)
|
||||
/// </summary>
|
||||
[Tooltip("Initial capacity of the system's list of Motors (will resize automatically if needed, but setting a high initial capacity can help preventing GC allocs)")]
|
||||
public int MotorsListInitialCapacity = 100;
|
||||
/// <summary>
|
||||
/// Initial capacity of the system's list of Movers (will resize automatically if needed, but setting a high initial capacity can help preventing GC allocs)
|
||||
/// </summary>
|
||||
[Tooltip("Initial capacity of the system's list of Movers (will resize automatically if needed, but setting a high initial capacity can help preventing GC allocs)")]
|
||||
public int MoversListInitialCapacity = 100;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2b43d8459fe4812488b2c47ac60e41c3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 99131
|
||||
packageName: Kinematic Character Controller
|
||||
packageVersion: 3.4.4
|
||||
assetPath: Assets/KinematicCharacterController/Core/KCCSettings.cs
|
||||
uploadId: 505601
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4d1bc5515e3ab954e80599c538834774
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 99131
|
||||
packageName: Kinematic Character Controller
|
||||
packageVersion: 3.4.4
|
||||
assetPath: Assets/KinematicCharacterController/Core/KinematicCharacterMotor.cs
|
||||
uploadId: 505601
|
||||
@@ -0,0 +1,294 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using UnityEngine;
|
||||
|
||||
namespace KinematicCharacterController
|
||||
{
|
||||
/// <summary>
|
||||
/// The system that manages the simulation of KinematicCharacterMotor and PhysicsMover
|
||||
/// </summary>
|
||||
[DefaultExecutionOrder(-100)]
|
||||
public class KinematicCharacterSystem : MonoBehaviour
|
||||
{
|
||||
private static KinematicCharacterSystem _instance;
|
||||
|
||||
public static List<KinematicCharacterMotor> CharacterMotors = new List<KinematicCharacterMotor>();
|
||||
public static List<PhysicsMover> PhysicsMovers = new List<PhysicsMover>();
|
||||
|
||||
private static float _lastCustomInterpolationStartTime = -1f;
|
||||
private static float _lastCustomInterpolationDeltaTime = -1f;
|
||||
|
||||
public static KCCSettings Settings;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a KinematicCharacterSystem instance if there isn't already one
|
||||
/// </summary>
|
||||
public static void EnsureCreation()
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
GameObject systemGameObject = new GameObject("KinematicCharacterSystem");
|
||||
_instance = systemGameObject.AddComponent<KinematicCharacterSystem>();
|
||||
|
||||
systemGameObject.hideFlags = HideFlags.NotEditable;
|
||||
_instance.hideFlags = HideFlags.NotEditable;
|
||||
|
||||
Settings = ScriptableObject.CreateInstance<KCCSettings>();
|
||||
|
||||
GameObject.DontDestroyOnLoad(systemGameObject);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the KinematicCharacterSystem instance if any
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static KinematicCharacterSystem GetInstance()
|
||||
{
|
||||
return _instance;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the maximum capacity of the character motors list, to prevent allocations when adding characters
|
||||
/// </summary>
|
||||
/// <param name="capacity"></param>
|
||||
public static void SetCharacterMotorsCapacity(int capacity)
|
||||
{
|
||||
if (capacity < CharacterMotors.Count)
|
||||
{
|
||||
capacity = CharacterMotors.Count;
|
||||
}
|
||||
CharacterMotors.Capacity = capacity;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers a KinematicCharacterMotor into the system
|
||||
/// </summary>
|
||||
public static void RegisterCharacterMotor(KinematicCharacterMotor motor)
|
||||
{
|
||||
CharacterMotors.Add(motor);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unregisters a KinematicCharacterMotor from the system
|
||||
/// </summary>
|
||||
public static void UnregisterCharacterMotor(KinematicCharacterMotor motor)
|
||||
{
|
||||
CharacterMotors.Remove(motor);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the maximum capacity of the physics movers list, to prevent allocations when adding movers
|
||||
/// </summary>
|
||||
/// <param name="capacity"></param>
|
||||
public static void SetPhysicsMoversCapacity(int capacity)
|
||||
{
|
||||
if (capacity < PhysicsMovers.Count)
|
||||
{
|
||||
capacity = PhysicsMovers.Count;
|
||||
}
|
||||
PhysicsMovers.Capacity = capacity;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers a PhysicsMover into the system
|
||||
/// </summary>
|
||||
public static void RegisterPhysicsMover(PhysicsMover mover)
|
||||
{
|
||||
PhysicsMovers.Add(mover);
|
||||
|
||||
mover.Rigidbody.interpolation = RigidbodyInterpolation.None;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unregisters a PhysicsMover from the system
|
||||
/// </summary>
|
||||
public static void UnregisterPhysicsMover(PhysicsMover mover)
|
||||
{
|
||||
PhysicsMovers.Remove(mover);
|
||||
}
|
||||
|
||||
// This is to prevent duplicating the singleton gameobject on script recompiles
|
||||
private void OnDisable()
|
||||
{
|
||||
Destroy(this.gameObject);
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_instance = this;
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (Settings.AutoSimulation)
|
||||
{
|
||||
float deltaTime = Time.deltaTime;
|
||||
|
||||
if (Settings.Interpolate)
|
||||
{
|
||||
PreSimulationInterpolationUpdate(deltaTime);
|
||||
}
|
||||
|
||||
Simulate(deltaTime, CharacterMotors, PhysicsMovers);
|
||||
|
||||
if (Settings.Interpolate)
|
||||
{
|
||||
PostSimulationInterpolationUpdate(deltaTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
if (Settings.Interpolate)
|
||||
{
|
||||
CustomInterpolationUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remembers the point to interpolate from for KinematicCharacterMotors and PhysicsMovers
|
||||
/// </summary>
|
||||
public static void PreSimulationInterpolationUpdate(float deltaTime)
|
||||
{
|
||||
// Save pre-simulation poses and place transform at transient pose
|
||||
for (int i = 0; i < CharacterMotors.Count; i++)
|
||||
{
|
||||
KinematicCharacterMotor motor = CharacterMotors[i];
|
||||
|
||||
motor.InitialTickPosition = motor.TransientPosition;
|
||||
motor.InitialTickRotation = motor.TransientRotation;
|
||||
|
||||
motor.Transform.SetPositionAndRotation(motor.TransientPosition, motor.TransientRotation);
|
||||
}
|
||||
|
||||
for (int i = 0; i < PhysicsMovers.Count; i++)
|
||||
{
|
||||
PhysicsMover mover = PhysicsMovers[i];
|
||||
|
||||
mover.InitialTickPosition = mover.TransientPosition;
|
||||
mover.InitialTickRotation = mover.TransientRotation;
|
||||
|
||||
mover.Transform.SetPositionAndRotation(mover.TransientPosition, mover.TransientRotation);
|
||||
mover.Rigidbody.position = mover.TransientPosition;
|
||||
mover.Rigidbody.rotation = mover.TransientRotation;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ticks characters and/or movers
|
||||
/// </summary>
|
||||
public static void Simulate(float deltaTime, List<KinematicCharacterMotor> motors, List<PhysicsMover> movers)
|
||||
{
|
||||
int characterMotorsCount = motors.Count;
|
||||
int physicsMoversCount = movers.Count;
|
||||
|
||||
#pragma warning disable 0162
|
||||
// Update PhysicsMover velocities
|
||||
for (int i = 0; i < physicsMoversCount; i++)
|
||||
{
|
||||
movers[i].VelocityUpdate(deltaTime);
|
||||
}
|
||||
|
||||
// Character controller update phase 1
|
||||
for (int i = 0; i < characterMotorsCount; i++)
|
||||
{
|
||||
motors[i].UpdatePhase1(deltaTime);
|
||||
}
|
||||
|
||||
// Simulate PhysicsMover displacement
|
||||
for (int i = 0; i < physicsMoversCount; i++)
|
||||
{
|
||||
PhysicsMover mover = movers[i];
|
||||
|
||||
mover.Transform.SetPositionAndRotation(mover.TransientPosition, mover.TransientRotation);
|
||||
mover.Rigidbody.position = mover.TransientPosition;
|
||||
mover.Rigidbody.rotation = mover.TransientRotation;
|
||||
}
|
||||
|
||||
// Character controller update phase 2 and move
|
||||
for (int i = 0; i < characterMotorsCount; i++)
|
||||
{
|
||||
KinematicCharacterMotor motor = motors[i];
|
||||
|
||||
motor.UpdatePhase2(deltaTime);
|
||||
|
||||
motor.Transform.SetPositionAndRotation(motor.TransientPosition, motor.TransientRotation);
|
||||
}
|
||||
#pragma warning restore 0162
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initiates the interpolation for KinematicCharacterMotors and PhysicsMovers
|
||||
/// </summary>
|
||||
public static void PostSimulationInterpolationUpdate(float deltaTime)
|
||||
{
|
||||
_lastCustomInterpolationStartTime = Time.time;
|
||||
_lastCustomInterpolationDeltaTime = deltaTime;
|
||||
|
||||
// Return interpolated roots to their initial poses
|
||||
for (int i = 0; i < CharacterMotors.Count; i++)
|
||||
{
|
||||
KinematicCharacterMotor motor = CharacterMotors[i];
|
||||
|
||||
motor.Transform.SetPositionAndRotation(motor.InitialTickPosition, motor.InitialTickRotation);
|
||||
}
|
||||
|
||||
for (int i = 0; i < PhysicsMovers.Count; i++)
|
||||
{
|
||||
PhysicsMover mover = PhysicsMovers[i];
|
||||
|
||||
if (mover.MoveWithPhysics)
|
||||
{
|
||||
mover.Rigidbody.position = mover.InitialTickPosition;
|
||||
mover.Rigidbody.rotation = mover.InitialTickRotation;
|
||||
|
||||
mover.Rigidbody.MovePosition(mover.TransientPosition);
|
||||
mover.Rigidbody.MoveRotation(mover.TransientRotation);
|
||||
}
|
||||
else
|
||||
{
|
||||
mover.Rigidbody.position = (mover.TransientPosition);
|
||||
mover.Rigidbody.rotation = (mover.TransientRotation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles per-frame interpolation
|
||||
/// </summary>
|
||||
private static void CustomInterpolationUpdate()
|
||||
{
|
||||
float interpolationFactor = Mathf.Clamp01((Time.time - _lastCustomInterpolationStartTime) / _lastCustomInterpolationDeltaTime);
|
||||
|
||||
// Handle characters interpolation
|
||||
for (int i = 0; i < CharacterMotors.Count; i++)
|
||||
{
|
||||
KinematicCharacterMotor motor = CharacterMotors[i];
|
||||
|
||||
motor.Transform.SetPositionAndRotation(
|
||||
Vector3.Lerp(motor.InitialTickPosition, motor.TransientPosition, interpolationFactor),
|
||||
Quaternion.Slerp(motor.InitialTickRotation, motor.TransientRotation, interpolationFactor));
|
||||
}
|
||||
|
||||
// Handle PhysicsMovers interpolation
|
||||
for (int i = 0; i < PhysicsMovers.Count; i++)
|
||||
{
|
||||
PhysicsMover mover = PhysicsMovers[i];
|
||||
|
||||
mover.Transform.SetPositionAndRotation(
|
||||
Vector3.Lerp(mover.InitialTickPosition, mover.TransientPosition, interpolationFactor),
|
||||
Quaternion.Slerp(mover.InitialTickRotation, mover.TransientRotation, interpolationFactor));
|
||||
|
||||
Vector3 newPos = mover.Transform.position;
|
||||
Quaternion newRot = mover.Transform.rotation;
|
||||
mover.PositionDeltaFromInterpolation = newPos - mover.LatestInterpolationPosition;
|
||||
mover.RotationDeltaFromInterpolation = Quaternion.Inverse(mover.LatestInterpolationRotation) * newRot;
|
||||
mover.LatestInterpolationPosition = newPos;
|
||||
mover.LatestInterpolationRotation = newRot;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dbd80c9a62d41084aad50ae44255beb9
|
||||
timeCreated: 1500241576
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 99131
|
||||
packageName: Kinematic Character Controller
|
||||
packageVersion: 3.4.4
|
||||
assetPath: Assets/KinematicCharacterController/Core/KinematicCharacterSystem.cs
|
||||
uploadId: 505601
|
||||
@@ -0,0 +1,261 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace KinematicCharacterController
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the entire state of a PhysicsMover that is pertinent for simulation.
|
||||
/// Use this to save state or revert to past state
|
||||
/// </summary>
|
||||
[System.Serializable]
|
||||
public struct PhysicsMoverState
|
||||
{
|
||||
public Vector3 Position;
|
||||
public Quaternion Rotation;
|
||||
public Vector3 Velocity;
|
||||
public Vector3 AngularVelocity;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Component that manages the movement of moving kinematic rigidbodies for
|
||||
/// proper interaction with characters
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
public class PhysicsMover : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// The mover's Rigidbody
|
||||
/// </summary>
|
||||
[ReadOnly]
|
||||
public Rigidbody Rigidbody;
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the platform moves with rigidbody.MovePosition (when true), or with rigidbody.position (when false)
|
||||
/// </summary>
|
||||
public bool MoveWithPhysics = true;
|
||||
|
||||
/// <summary>
|
||||
/// Index of this motor in KinematicCharacterSystem arrays
|
||||
/// </summary>
|
||||
[NonSerialized]
|
||||
public IMoverController MoverController;
|
||||
/// <summary>
|
||||
/// Remembers latest position in interpolation
|
||||
/// </summary>
|
||||
[NonSerialized]
|
||||
public Vector3 LatestInterpolationPosition;
|
||||
/// <summary>
|
||||
/// Remembers latest rotation in interpolation
|
||||
/// </summary>
|
||||
[NonSerialized]
|
||||
public Quaternion LatestInterpolationRotation;
|
||||
/// <summary>
|
||||
/// The latest movement made by interpolation
|
||||
/// </summary>
|
||||
[NonSerialized]
|
||||
public Vector3 PositionDeltaFromInterpolation;
|
||||
/// <summary>
|
||||
/// The latest rotation made by interpolation
|
||||
/// </summary>
|
||||
[NonSerialized]
|
||||
public Quaternion RotationDeltaFromInterpolation;
|
||||
|
||||
/// <summary>
|
||||
/// Index of this motor in KinematicCharacterSystem arrays
|
||||
/// </summary>
|
||||
public int IndexInCharacterSystem { get; set; }
|
||||
/// <summary>
|
||||
/// Remembers initial position before all simulation are done
|
||||
/// </summary>
|
||||
public Vector3 Velocity { get; protected set; }
|
||||
/// <summary>
|
||||
/// Remembers initial position before all simulation are done
|
||||
/// </summary>
|
||||
public Vector3 AngularVelocity { get; protected set; }
|
||||
/// <summary>
|
||||
/// Remembers initial position before all simulation are done
|
||||
/// </summary>
|
||||
public Vector3 InitialTickPosition { get; set; }
|
||||
/// <summary>
|
||||
/// Remembers initial rotation before all simulation are done
|
||||
/// </summary>
|
||||
public Quaternion InitialTickRotation { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The mover's Transform
|
||||
/// </summary>
|
||||
public Transform Transform { get; private set; }
|
||||
/// <summary>
|
||||
/// The character's position before the movement calculations began
|
||||
/// </summary>
|
||||
public Vector3 InitialSimulationPosition { get; private set; }
|
||||
/// <summary>
|
||||
/// The character's rotation before the movement calculations began
|
||||
/// </summary>
|
||||
public Quaternion InitialSimulationRotation { get; private set; }
|
||||
|
||||
private Vector3 _internalTransientPosition;
|
||||
|
||||
/// <summary>
|
||||
/// The mover's rotation (always up-to-date during the character update phase)
|
||||
/// </summary>
|
||||
public Vector3 TransientPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
return _internalTransientPosition;
|
||||
}
|
||||
private set
|
||||
{
|
||||
_internalTransientPosition = value;
|
||||
}
|
||||
}
|
||||
|
||||
private Quaternion _internalTransientRotation;
|
||||
/// <summary>
|
||||
/// The mover's rotation (always up-to-date during the character update phase)
|
||||
/// </summary>
|
||||
public Quaternion TransientRotation
|
||||
{
|
||||
get
|
||||
{
|
||||
return _internalTransientRotation;
|
||||
}
|
||||
private set
|
||||
{
|
||||
_internalTransientRotation = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void Reset()
|
||||
{
|
||||
ValidateData();
|
||||
}
|
||||
|
||||
private void OnValidate()
|
||||
{
|
||||
ValidateData();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle validating all required values
|
||||
/// </summary>
|
||||
public void ValidateData()
|
||||
{
|
||||
Rigidbody = gameObject.GetComponent<Rigidbody>();
|
||||
|
||||
Rigidbody.centerOfMass = Vector3.zero;
|
||||
Rigidbody.maxAngularVelocity = Mathf.Infinity;
|
||||
Rigidbody.maxDepenetrationVelocity = Mathf.Infinity;
|
||||
Rigidbody.isKinematic = true;
|
||||
Rigidbody.interpolation = RigidbodyInterpolation.None;
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
KinematicCharacterSystem.EnsureCreation();
|
||||
KinematicCharacterSystem.RegisterPhysicsMover(this);
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
KinematicCharacterSystem.UnregisterPhysicsMover(this);
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Transform = this.transform;
|
||||
ValidateData();
|
||||
|
||||
TransientPosition = Rigidbody.position;
|
||||
TransientRotation = Rigidbody.rotation;
|
||||
InitialSimulationPosition = Rigidbody.position;
|
||||
InitialSimulationRotation = Rigidbody.rotation;
|
||||
LatestInterpolationPosition = Transform.position;
|
||||
LatestInterpolationRotation = Transform.rotation;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the mover's position directly
|
||||
/// </summary>
|
||||
public void SetPosition(Vector3 position)
|
||||
{
|
||||
Transform.position = position;
|
||||
Rigidbody.position = position;
|
||||
InitialSimulationPosition = position;
|
||||
TransientPosition = position;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the mover's rotation directly
|
||||
/// </summary>
|
||||
public void SetRotation(Quaternion rotation)
|
||||
{
|
||||
Transform.rotation = rotation;
|
||||
Rigidbody.rotation = rotation;
|
||||
InitialSimulationRotation = rotation;
|
||||
TransientRotation = rotation;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the mover's position and rotation directly
|
||||
/// </summary>
|
||||
public void SetPositionAndRotation(Vector3 position, Quaternion rotation)
|
||||
{
|
||||
Transform.SetPositionAndRotation(position, rotation);
|
||||
Rigidbody.position = position;
|
||||
Rigidbody.rotation = rotation;
|
||||
InitialSimulationPosition = position;
|
||||
InitialSimulationRotation = rotation;
|
||||
TransientPosition = position;
|
||||
TransientRotation = rotation;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns all the state information of the mover that is pertinent for simulation
|
||||
/// </summary>
|
||||
public PhysicsMoverState GetState()
|
||||
{
|
||||
PhysicsMoverState state = new PhysicsMoverState();
|
||||
|
||||
state.Position = TransientPosition;
|
||||
state.Rotation = TransientRotation;
|
||||
state.Velocity = Velocity;
|
||||
state.AngularVelocity = AngularVelocity;
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies a mover state instantly
|
||||
/// </summary>
|
||||
public void ApplyState(PhysicsMoverState state)
|
||||
{
|
||||
SetPositionAndRotation(state.Position, state.Rotation);
|
||||
Velocity = state.Velocity;
|
||||
AngularVelocity = state.AngularVelocity;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Caches velocity values based on deltatime and target position/rotations
|
||||
/// </summary>
|
||||
public void VelocityUpdate(float deltaTime)
|
||||
{
|
||||
InitialSimulationPosition = TransientPosition;
|
||||
InitialSimulationRotation = TransientRotation;
|
||||
|
||||
MoverController.UpdateMovement(out _internalTransientPosition, out _internalTransientRotation, deltaTime);
|
||||
|
||||
if (deltaTime > 0f)
|
||||
{
|
||||
Velocity = (TransientPosition - InitialSimulationPosition) / deltaTime;
|
||||
|
||||
Quaternion rotationFromCurrentToGoal = TransientRotation * (Quaternion.Inverse(InitialSimulationRotation));
|
||||
AngularVelocity = (Mathf.Deg2Rad * rotationFromCurrentToGoal.eulerAngles) / deltaTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 075ccbe9837b0744985e09ba3f015b9b
|
||||
timeCreated: 1488089271
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 99131
|
||||
packageName: Kinematic Character Controller
|
||||
packageVersion: 3.4.4
|
||||
assetPath: Assets/KinematicCharacterController/Core/PhysicsMover.cs
|
||||
uploadId: 505601
|
||||
@@ -0,0 +1,8 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace KinematicCharacterController
|
||||
{
|
||||
public class ReadOnlyAttribute : PropertyAttribute
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 63e8828991cf67f42bfeb498686aad0b
|
||||
timeCreated: 1504493014
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 99131
|
||||
packageName: Kinematic Character Controller
|
||||
packageVersion: 3.4.4
|
||||
assetPath: Assets/KinematicCharacterController/Core/ReadOnlyAttribute.cs
|
||||
uploadId: 505601
|
||||
Binary file not shown.
@@ -0,0 +1,14 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bf7d84d269b2ecf4db54490aa12a2044
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 99131
|
||||
packageName: Kinematic Character Controller
|
||||
packageVersion: 3.4.4
|
||||
assetPath: Assets/KinematicCharacterController/UpgradeGuide.pdf
|
||||
uploadId: 505601
|
||||
BIN
Assets/jelycho/Code/KinematicCharacterController/UserGuide.pdf
Normal file
BIN
Assets/jelycho/Code/KinematicCharacterController/UserGuide.pdf
Normal file
Binary file not shown.
@@ -0,0 +1,14 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 218002b0df18c6f408925b74449e3784
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 99131
|
||||
packageName: Kinematic Character Controller
|
||||
packageVersion: 3.4.4
|
||||
assetPath: Assets/KinematicCharacterController/UserGuide.pdf
|
||||
uploadId: 505601
|
||||
BIN
Assets/jelycho/Code/KinematicCharacterController/Walkthrough.pdf
Normal file
BIN
Assets/jelycho/Code/KinematicCharacterController/Walkthrough.pdf
Normal file
Binary file not shown.
@@ -0,0 +1,14 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1523509a877e6cc41b0578ebaf09994b
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 99131
|
||||
packageName: Kinematic Character Controller
|
||||
packageVersion: 3.4.4
|
||||
assetPath: Assets/KinematicCharacterController/Walkthrough.pdf
|
||||
uploadId: 505601
|
||||
59
Assets/jelycho/Code/Player/CameraSpring.cs
Normal file
59
Assets/jelycho/Code/Player/CameraSpring.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace RealityReboot.jelycho.Player {
|
||||
public class CameraSpring : MonoBehaviour {
|
||||
[Min(0.01f), SerializeField] float m_HalfLife = 0.075f;
|
||||
[SerializeField] float m_Frequency = 10.0f;
|
||||
[SerializeField] float m_ForwardAngularDisplacement = 1.0f;
|
||||
[SerializeField] float m_SidewaysAngularDisplacement = 1.0f;
|
||||
[SerializeField] float m_HeightAngularDisplacement = 2.0f;
|
||||
[SerializeField] float m_LinearDisplacement = 0.05f;
|
||||
|
||||
Vector3 m_SpringPosition;
|
||||
Vector3 m_SpringVelocity;
|
||||
|
||||
public void Initialize() {
|
||||
m_SpringPosition = transform.position;
|
||||
m_SpringVelocity = Vector3.zero;
|
||||
}
|
||||
|
||||
public void UpdateSpring(float deltaTime, Vector3 up, Vector3 right, Vector3 forward) {
|
||||
Spring(ref m_SpringPosition,
|
||||
ref m_SpringVelocity,
|
||||
transform.position,
|
||||
m_HalfLife,
|
||||
m_Frequency,
|
||||
deltaTime);
|
||||
|
||||
Vector3 localSpringPosition = m_SpringPosition - transform.position;
|
||||
float springHeight = Vector3.Dot(localSpringPosition, up);
|
||||
float springForward = Vector3.Dot(localSpringPosition, forward);
|
||||
float springSideways = Vector3.Dot(localSpringPosition, right);
|
||||
|
||||
float pitch = -springHeight * m_HeightAngularDisplacement + -springForward * m_ForwardAngularDisplacement;
|
||||
transform.localEulerAngles = new Vector3(pitch,
|
||||
0.0f,
|
||||
springSideways * m_SidewaysAngularDisplacement);
|
||||
}
|
||||
|
||||
// Source: https://allenchou.net/2015/04/game-math-precise-control-over-numeric-springing/
|
||||
static void Spring(ref Vector3 current,
|
||||
ref Vector3 velocity,
|
||||
Vector3 target,
|
||||
float halfLife,
|
||||
float frequency,
|
||||
float timeStep) {
|
||||
|
||||
float dampingRatio = -Mathf.Log(0.5f) / (frequency * halfLife);
|
||||
float f = 1.0f + 2.0f * timeStep * dampingRatio * frequency;
|
||||
float oo = frequency * frequency;
|
||||
float hoo = timeStep * oo;
|
||||
float hhoo = timeStep * hoo;
|
||||
float detInv = 1.0f / (f + hhoo);
|
||||
Vector3 detX = f * current + timeStep * velocity + hhoo * target;
|
||||
Vector3 detV = velocity + hoo * (target - current);
|
||||
current = detX * detInv;
|
||||
velocity = detV * detInv;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/jelycho/Code/Player/CameraSpring.cs.meta
Normal file
3
Assets/jelycho/Code/Player/CameraSpring.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f3305bd258b340d69d9b813f76dc5a01
|
||||
timeCreated: 1749115409
|
||||
59
Assets/jelycho/Code/Player/FPPCamera.cs
Normal file
59
Assets/jelycho/Code/Player/FPPCamera.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using RebootKit.Engine.Foundation;
|
||||
using RebootKit.Engine.Services.Simulation.Sensors;
|
||||
using Unity.Cinemachine;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RealityReboot.jelycho.Player {
|
||||
[AddComponentMenu(GameConsts.k_AddComponentMenu + "Player/First Person Camera")]
|
||||
public class FPPCamera : MonoBehaviour {
|
||||
[ConfigVar("fpp.camera.fov", 60.0f, "Field of view for the first person camera.")]
|
||||
static ConfigVar s_cameraFOV;
|
||||
|
||||
[SerializeField] float _pickDistance = 5.0f;
|
||||
|
||||
[SerializeField] LayerMask _pickLayer;
|
||||
|
||||
[field: SerializeField]
|
||||
public float Sensitivity { get; set; }
|
||||
|
||||
[SerializeField] float _pitchMin = -80f;
|
||||
|
||||
[SerializeField] float _pitchMax = 80f;
|
||||
|
||||
[field: SerializeField]
|
||||
public CinemachineCamera Camera { get; private set; }
|
||||
|
||||
readonly RaycastSensor _raycastSensor = new();
|
||||
|
||||
public float Pitch { get; private set; }
|
||||
public float Yaw { get; private set; }
|
||||
|
||||
public ISensor Sensor {
|
||||
get {
|
||||
_raycastSensor.ray = new Ray(Camera.transform.position, Camera.transform.forward);
|
||||
_raycastSensor.maxDistance = _pickDistance;
|
||||
_raycastSensor.layerMask = _pickLayer;
|
||||
return _raycastSensor;
|
||||
}
|
||||
}
|
||||
|
||||
public void Tick() {
|
||||
Camera.Lens.FieldOfView = s_cameraFOV.FloatValue;
|
||||
Camera.transform.localRotation = Quaternion.Euler(Pitch, 0f, 0f);
|
||||
}
|
||||
|
||||
public void Rotate(float x, float y) {
|
||||
float sens = Sensitivity;
|
||||
|
||||
Pitch -= y * sens;
|
||||
Pitch = Mathf.Clamp(Pitch, _pitchMin, _pitchMax);
|
||||
|
||||
Yaw += x * sens;
|
||||
}
|
||||
|
||||
public void SetLookDirection(Vector3 forward) {
|
||||
Pitch = Mathf.Asin(-forward.y) * Mathf.Rad2Deg;
|
||||
Yaw = Mathf.Atan2(forward.x, forward.z) * Mathf.Rad2Deg;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/jelycho/Code/Player/FPPCamera.cs.meta
Normal file
2
Assets/jelycho/Code/Player/FPPCamera.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b82691de261d95842bde9bca81db0893
|
||||
@@ -1,7 +1,6 @@
|
||||
using RebootKit.Engine.Extensions;
|
||||
using RebootKit.Engine.Foundation;
|
||||
using RebootKit.Engine.Services.Simulation;
|
||||
using RebootKit.FPPKit;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
using Logger = RebootKit.Engine.Foundation.Logger;
|
||||
@@ -9,22 +8,33 @@ using Logger = RebootKit.Engine.Foundation.Logger;
|
||||
namespace RealityReboot.jelycho.Player {
|
||||
public class PlayerActor : Actor {
|
||||
static readonly Logger s_logger = new(nameof(PlayerActor));
|
||||
|
||||
[SerializeField] PlayerFPPLocomotion m_Locomotion;
|
||||
[SerializeField] FPPCamera m_Camera;
|
||||
[SerializeField] Animator m_Animator;
|
||||
|
||||
[SerializeField] Transform m_DragGutStartPosition;
|
||||
|
||||
[SerializeField] Animator m_Animator;
|
||||
|
||||
[Header("Movement")]
|
||||
[SerializeField] PlayerFPPLocomotion m_Locomotion;
|
||||
|
||||
[Header("Camera")]
|
||||
[SerializeField] FPPCamera m_Camera;
|
||||
[SerializeField] CameraSpring m_CameraSpring;
|
||||
|
||||
[Header("Dragging")]
|
||||
[SerializeField] Transform m_DragGutStartPosition;
|
||||
[SerializeField] PhysicsObjectDragger m_PhysicsDragger;
|
||||
[SerializeField] FloatRange m_DragDistanceRange = new(1.0f, 5.0f);
|
||||
|
||||
[SerializeField] LineRenderer m_LineRenderer;
|
||||
|
||||
public override void OnBeginPlay() {
|
||||
m_CameraSpring.Initialize();
|
||||
}
|
||||
|
||||
public override void OnTick() {
|
||||
m_Locomotion.YawRotation = m_Camera.Yaw;
|
||||
m_Camera.Tick();
|
||||
|
||||
m_PhysicsDragger.TargetWorldPosition = m_Camera.Camera.transform.position + m_Camera.Camera.transform.forward * 2.0f;
|
||||
|
||||
if (m_PhysicsDragger.Current.OrNull() != null) {
|
||||
if (m_PhysicsDragger.Current.OrNull() is not null) {
|
||||
m_LineRenderer.enabled = true;
|
||||
m_LineRenderer.SetPosition(0, m_LineRenderer.transform.InverseTransformPoint(m_DragGutStartPosition.position));
|
||||
m_LineRenderer.SetPosition(1, m_LineRenderer.transform.InverseTransformPoint(m_PhysicsDragger.Current.position));
|
||||
@@ -32,8 +42,17 @@ namespace RealityReboot.jelycho.Player {
|
||||
m_LineRenderer.enabled = false;
|
||||
}
|
||||
|
||||
Vector3 actorUp = transform.up;
|
||||
Vector3 actorRight = transform.right;
|
||||
Vector3 actorForward = transform.forward;
|
||||
m_CameraSpring.UpdateSpring(Time.deltaTime, actorUp, actorRight, actorForward);
|
||||
|
||||
UpdateAnimator();
|
||||
}
|
||||
|
||||
public void SetSprint(bool isSprinting) {
|
||||
m_Locomotion.SetSprint(isSprinting);
|
||||
}
|
||||
|
||||
public void Jump() {
|
||||
m_Locomotion.Jump();
|
||||
@@ -44,7 +63,8 @@ namespace RealityReboot.jelycho.Player {
|
||||
}
|
||||
|
||||
public void SetMoveInput(Vector2 input) {
|
||||
float3 direction = Quaternion.AngleAxis(m_Camera.Yaw, Vector3.up) * new float3(input.x, 0.0f, input.y);
|
||||
float3 direction = Quaternion.AngleAxis(transform.eulerAngles.y, Vector3.up) *
|
||||
new float3(input.x, 0.0f, input.y);
|
||||
m_Locomotion.SetWishDirection(direction);
|
||||
}
|
||||
|
||||
@@ -66,12 +86,12 @@ namespace RealityReboot.jelycho.Player {
|
||||
}
|
||||
|
||||
void UpdateAnimator() {
|
||||
float forwardNormalized = m_Locomotion.LocalVelocity.z / m_Locomotion.runSpeed;
|
||||
|
||||
m_Animator.SetFloat(AnimatorParamHashes.s_VelocityForwardNormalized, forwardNormalized);
|
||||
m_Animator.SetFloat(AnimatorParamHashes.s_VelocityRightNormalized, 0.0f);
|
||||
|
||||
m_Animator.SetBool(AnimatorParamHashes.s_IsGrounded, m_Locomotion.IsGrounded);
|
||||
// float forwardNormalized = m_Locomotion.LocalVelocity.z / m_Locomotion.runSpeed;
|
||||
//
|
||||
// m_Animator.SetFloat(AnimatorParamHashes.s_VelocityForwardNormalized, forwardNormalized);
|
||||
// m_Animator.SetFloat(AnimatorParamHashes.s_VelocityRightNormalized, 0.0f);
|
||||
//
|
||||
// m_Animator.SetBool(AnimatorParamHashes.s_IsGrounded, m_Locomotion.IsGrounded);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ using RebootKit.Engine;
|
||||
using RebootKit.Engine.Foundation;
|
||||
using RebootKit.Engine.Main;
|
||||
using RebootKit.Engine.Services.Input;
|
||||
using RebootKit.FPPKit;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using UnityEngine.InputSystem;
|
||||
@@ -57,6 +57,8 @@ namespace RealityReboot.jelycho.Player {
|
||||
Vector2 moveInput = m_Config.moveActionReference.action.ReadValue<Vector2>();
|
||||
m_FPPActor.SetMoveInput(moveInput);
|
||||
|
||||
m_FPPActor.SetSprint(m_Config.sprintActionReference.action.IsPressed());
|
||||
|
||||
if (m_Config.jumpActionReference.action.WasPerformedThisFrame()) {
|
||||
m_FPPActor.Jump();
|
||||
}
|
||||
@@ -77,9 +79,40 @@ namespace RealityReboot.jelycho.Player {
|
||||
public InputActionReference moveActionReference;
|
||||
public InputActionReference lookActionReference;
|
||||
public InputActionReference jumpActionReference;
|
||||
public InputActionReference sprintActionReference;
|
||||
public InputActionReference dragObjectActionReference;
|
||||
public InputActionReference primaryActionReference;
|
||||
public InputActionReference secondaryActionReference;
|
||||
}
|
||||
}
|
||||
|
||||
public static class FPPConfig {
|
||||
[ConfigVar("fpp.mouse.sens", 0.25f, "Mouse look sensitivity")] public static ConfigVar s_MouseSensitivity;
|
||||
[ConfigVar("fpp.mouse.invert.y", 0, "Invert mouse look")] public static ConfigVar s_MouseInvertY;
|
||||
[ConfigVar("fpp.gamepad.sens", 4.0f, "Gamepad look sensitivity")] public static ConfigVar s_GamepadSensitivity;
|
||||
}
|
||||
|
||||
public static class SensitivityReader {
|
||||
public static float2 Read(InputAction action) {
|
||||
if (action == null || action.activeControl == null) {
|
||||
return float2.zero;
|
||||
}
|
||||
|
||||
float2 sensitivity = action.ReadValue<Vector2>();
|
||||
|
||||
bool isGamepad = action.activeControl.device is Gamepad;
|
||||
|
||||
if (isGamepad) {
|
||||
sensitivity *= FPPConfig.s_GamepadSensitivity.FloatValue;
|
||||
} else {
|
||||
sensitivity *= FPPConfig.s_MouseSensitivity.FloatValue;
|
||||
|
||||
if (FPPConfig.s_MouseInvertY.IndexValue == 1) {
|
||||
sensitivity.y *= -1;
|
||||
}
|
||||
}
|
||||
|
||||
return sensitivity;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,15 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using RebootKit.Engine.Extensions;
|
||||
using KinematicCharacterController;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
using Logger = RebootKit.Engine.Foundation.Logger;
|
||||
|
||||
namespace RealityReboot.jelycho.Player {
|
||||
[RequireComponent(typeof(Rigidbody), typeof(CapsuleCollider))]
|
||||
public class PlayerFPPLocomotion : MonoBehaviour {
|
||||
static readonly RaycastHit[] s_HitBuffer = new RaycastHit[32];
|
||||
|
||||
[SerializeField] Rigidbody m_Rigidbody;
|
||||
[SerializeField] CapsuleCollider m_CapsuleCollider;
|
||||
[SerializeField] LayerMask m_GroundLayerMask;
|
||||
public class PlayerFPPLocomotion : MonoBehaviour, ICharacterController {
|
||||
static readonly Logger s_logger = new(nameof(PlayerFPPLocomotion));
|
||||
|
||||
[SerializeField] KinematicCharacterMotor m_Motor;
|
||||
|
||||
public float runSpeed = 10.0f;
|
||||
public float sprintSpeed = 20.0f;
|
||||
@@ -19,146 +17,170 @@ namespace RealityReboot.jelycho.Player {
|
||||
public float groundAcceleration = 10.0f;
|
||||
public float groundFriction = 10.0f;
|
||||
|
||||
public float airFriction = 1.0f;
|
||||
|
||||
public float jumpHeight = 2.0f;
|
||||
|
||||
public float inAirAcceleration = 10.0f;
|
||||
public float jumpInputDuration = 0.2f;
|
||||
public float coyoteDuration = 0.2f;
|
||||
|
||||
[Range(0.0f, 1.0f)] public float airControl = 0.5f;
|
||||
public float gravity = 9.8f;
|
||||
|
||||
public float3 Velocity => m_Rigidbody.linearVelocity;
|
||||
public float3 LocalVelocity => m_Rigidbody.transform.InverseTransformDirection(m_Rigidbody.linearVelocity);
|
||||
|
||||
float3 m_WishDir;
|
||||
bool m_IsSprinting;
|
||||
|
||||
public bool IsGrounded { get; private set; }
|
||||
public float YawRotation { get; set; }
|
||||
|
||||
Vector3 m_WishDir;
|
||||
bool m_IsSprinting;
|
||||
bool m_IsJumpRequested;
|
||||
float m_JumpRequestedTime;
|
||||
float m_LastGroundedTime;
|
||||
|
||||
Vector3 m_LastVelocity;
|
||||
|
||||
public bool IsGrounded => m_Motor.GroundingStatus.IsStableOnGround;
|
||||
|
||||
void Awake() {
|
||||
m_Motor.CharacterController = this;
|
||||
}
|
||||
|
||||
void FixedUpdate() {
|
||||
IsGrounded = CheckGrounded();
|
||||
|
||||
float3 vel = m_Rigidbody.linearVelocity;
|
||||
|
||||
if (IsGrounded) {
|
||||
UpdateGroundVelocity(ref vel, Time.fixedDeltaTime);
|
||||
} else {
|
||||
UpdateAirVelocity(ref vel, Time.fixedDeltaTime);
|
||||
}
|
||||
|
||||
m_Rigidbody.linearVelocity = vel;
|
||||
}
|
||||
|
||||
void ApplyGroundFriction(ref float3 vel, float dt) {
|
||||
float speed = math.length(vel);
|
||||
if (speed < 0.1f) {
|
||||
vel = float3.zero;
|
||||
return;
|
||||
}
|
||||
|
||||
float drop = speed * groundFriction * dt;
|
||||
float newSpeed = speed - drop;
|
||||
if (newSpeed < 0.0f) {
|
||||
newSpeed = 0.0f;
|
||||
}
|
||||
|
||||
newSpeed /= speed;
|
||||
vel *= newSpeed;
|
||||
}
|
||||
|
||||
void ApplyGravity(ref float3 vel, float dt) {
|
||||
vel.y -= gravity * dt;
|
||||
}
|
||||
|
||||
void Accelerate(ref float3 vel, float3 wishDir, float wishSpeed, float acceleration, float dt) {
|
||||
float currentSpeed = math.dot(vel, wishDir);
|
||||
float addSpeed = wishSpeed - currentSpeed;
|
||||
if (addSpeed <= 0.0f) {
|
||||
return;
|
||||
}
|
||||
|
||||
float accelSpeed = acceleration * dt * wishSpeed;
|
||||
if (accelSpeed > addSpeed) {
|
||||
accelSpeed = addSpeed;
|
||||
}
|
||||
|
||||
vel += accelSpeed * wishDir;
|
||||
}
|
||||
|
||||
void UpdateGroundVelocity(ref float3 vel, float dt) {
|
||||
ApplyGroundFriction(ref vel, dt);
|
||||
|
||||
float wishSpeed;
|
||||
if (m_IsSprinting) {
|
||||
wishSpeed = sprintSpeed;
|
||||
} else {
|
||||
wishSpeed = runSpeed;
|
||||
}
|
||||
|
||||
Accelerate(ref vel, m_WishDir, wishSpeed, groundAcceleration, dt);
|
||||
ApplyGravity(ref vel, dt);
|
||||
}
|
||||
|
||||
void UpdateAirVelocity(ref float3 vel, float dt) {
|
||||
ApplyGravity(ref vel, dt);
|
||||
|
||||
Accelerate(ref vel, m_WishDir, runSpeed, inAirAcceleration, dt);
|
||||
}
|
||||
|
||||
void OnValidate() {
|
||||
if (m_Rigidbody == null) {
|
||||
m_Rigidbody = GetComponent<Rigidbody>();
|
||||
}
|
||||
}
|
||||
|
||||
void OnDrawGizmosSelected() {
|
||||
float3 origin = transform.position;
|
||||
|
||||
void OnDrawGizmos() {
|
||||
Gizmos.color = Color.blue;
|
||||
Gizmos.DrawLine(origin, origin + m_WishDir);
|
||||
|
||||
Gizmos.color = Color.green;
|
||||
Gizmos.DrawLine(origin, origin + Velocity);
|
||||
Gizmos.DrawLine(transform.position,
|
||||
transform.position + m_WishDir * 2.0f);
|
||||
}
|
||||
|
||||
bool CheckGrounded() {
|
||||
float3 origin = transform.position + Vector3.up * 0.1f;
|
||||
float3 direction = Vector3.down;
|
||||
float distance = 0.2f;
|
||||
|
||||
int hitCount = Physics.SphereCastNonAlloc(origin,
|
||||
m_CapsuleCollider.radius,
|
||||
direction,
|
||||
s_HitBuffer,
|
||||
distance,
|
||||
m_GroundLayerMask,
|
||||
QueryTriggerInteraction.Ignore);
|
||||
|
||||
for (int i = 0; i < hitCount; i++) {
|
||||
RaycastHit hit = s_HitBuffer[i];
|
||||
if (hit.collider is not null && hit.collider.gameObject != gameObject) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
void OnGUI() {
|
||||
GUI.Label(new Rect(0, 0, Screen.width, Screen.height),
|
||||
$"Wish Direction: {m_WishDir}\n" +
|
||||
$"Is Grounded: {m_Motor.GroundingStatus.IsStableOnGround}\n" +
|
||||
$"Is Jump Requested: {m_IsJumpRequested}\n" +
|
||||
$"Motor Velocity: {m_Motor.Velocity}, magnitude: {m_Motor.Velocity.magnitude}\n" +
|
||||
$"Motor Base Velocity: {m_Motor.BaseVelocity}, magnitude: {m_Motor.BaseVelocity.magnitude}\n" +
|
||||
$"Last Velocity: {m_LastVelocity}, magnitude: {math.length(m_LastVelocity)}\n");
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetWishDirection(float3 dir) {
|
||||
public void SetWishDirection(Vector3 dir) {
|
||||
m_WishDir = dir;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Jump() {
|
||||
if (IsGrounded) {
|
||||
m_Rigidbody.AddForce(Vector3.up * CalculateJumpVelocity(jumpHeight, gravity), ForceMode.VelocityChange);
|
||||
}
|
||||
m_IsJumpRequested = true;
|
||||
m_JumpRequestedTime = Time.time;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetSprint(bool isSprinting) {
|
||||
m_IsSprinting = isSprinting;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static float CalculateJumpVelocity(float jumpHeight, float gravity) {
|
||||
return math.sqrt(2.0f * gravity * jumpHeight);
|
||||
}
|
||||
|
||||
public void UpdateRotation(ref Quaternion currentRotation, float deltaTime) {
|
||||
currentRotation = Quaternion.AngleAxis(YawRotation, Vector3.up);
|
||||
}
|
||||
|
||||
void Accelerate(ref Vector3 currentVelocity,
|
||||
float wishSpeed,
|
||||
float control,
|
||||
float deltaTime) {
|
||||
float currentSpeed = Vector3.Dot(currentVelocity, m_WishDir);
|
||||
|
||||
float addSpeed = wishSpeed - currentSpeed;
|
||||
if (addSpeed <= 0.0f) {
|
||||
return;
|
||||
}
|
||||
|
||||
float accelSpeed = groundAcceleration * deltaTime * wishSpeed;
|
||||
if (accelSpeed > addSpeed) {
|
||||
accelSpeed = addSpeed;
|
||||
}
|
||||
|
||||
currentVelocity += accelSpeed * control * m_WishDir;
|
||||
}
|
||||
|
||||
void ApplyFriction(ref Vector3 currentVelocity, float friction, float deltaTime) {
|
||||
float speed = currentVelocity.magnitude;
|
||||
if (speed <= 0.0f) {
|
||||
return;
|
||||
}
|
||||
|
||||
float drop = speed * friction * deltaTime;
|
||||
if (drop < speed) {
|
||||
currentVelocity *= (speed - drop) / speed;
|
||||
} else {
|
||||
currentVelocity = Vector3.zero;
|
||||
}
|
||||
}
|
||||
|
||||
void GroundMovement(ref Vector3 currentVelocity, float deltaTime) {
|
||||
float wishSpeed = m_IsSprinting ? sprintSpeed : runSpeed;
|
||||
Accelerate(ref currentVelocity, wishSpeed, 1.0f, deltaTime);
|
||||
ApplyFriction(ref currentVelocity, groundFriction, deltaTime);
|
||||
}
|
||||
|
||||
void ApplyGravity(ref Vector3 currentVelocity, float deltaTime) {
|
||||
currentVelocity.y -= gravity * deltaTime;
|
||||
}
|
||||
|
||||
public void UpdateVelocity(ref Vector3 currentVelocity, float deltaTime) {
|
||||
if (Time.time > m_JumpRequestedTime + jumpInputDuration) {
|
||||
m_IsJumpRequested = false;
|
||||
}
|
||||
|
||||
if (m_Motor.GroundingStatus.IsStableOnGround) {
|
||||
m_LastGroundedTime = Time.time;
|
||||
}
|
||||
|
||||
bool canJump = m_Motor.GroundingStatus.IsStableOnGround ||
|
||||
Time.time < m_LastGroundedTime + coyoteDuration;
|
||||
|
||||
if (m_IsJumpRequested && canJump) {
|
||||
currentVelocity.y = CalculateJumpVelocity(jumpHeight, gravity);
|
||||
m_Motor.ForceUnground();
|
||||
|
||||
m_IsJumpRequested = false;
|
||||
m_LastGroundedTime = 0.0f;
|
||||
}
|
||||
|
||||
if (m_Motor.GroundingStatus.IsStableOnGround) {
|
||||
GroundMovement(ref currentVelocity, deltaTime);
|
||||
} else {
|
||||
Accelerate(ref currentVelocity, runSpeed, airControl, deltaTime);
|
||||
ApplyFriction(ref currentVelocity, airFriction, deltaTime);
|
||||
|
||||
ApplyGravity(ref currentVelocity, deltaTime);
|
||||
}
|
||||
|
||||
m_LastVelocity = currentVelocity;
|
||||
}
|
||||
|
||||
public void BeforeCharacterUpdate(float deltaTime) {
|
||||
}
|
||||
|
||||
public void PostGroundingUpdate(float deltaTime) {
|
||||
}
|
||||
|
||||
public void AfterCharacterUpdate(float deltaTime) {
|
||||
}
|
||||
|
||||
public bool IsColliderValidForCollisions(Collider coll) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void OnGroundHit(Collider hitCollider, Vector3 hitNormal, Vector3 hitPoint, ref HitStabilityReport hitStabilityReport) {
|
||||
}
|
||||
|
||||
public void OnMovementHit(Collider hitCollider, Vector3 hitNormal, Vector3 hitPoint, ref HitStabilityReport hitStabilityReport) {
|
||||
}
|
||||
|
||||
public void ProcessHitStabilityReport(Collider hitCollider, Vector3 hitNormal, Vector3 hitPoint, Vector3 atCharacterPosition, Quaternion atCharacterRotation, ref HitStabilityReport hitStabilityReport) {
|
||||
}
|
||||
|
||||
public void OnDiscreteCollisionDetected(Collider hitCollider) {
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user