Import assets from store
This commit is contained in:
1 parent
a05f2e47a2
commit
376e52e323
875 files changed
+128037
No files matched your search
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3c64a9852ff643f5b36b4546ae151af6
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "CharacterMovement.Core.PlayModeTests",
|
||||
"rootNamespace": "CharacterMovement.Tests",
|
||||
"references": [
|
||||
"CharacterMovement.Core",
|
||||
"Unity.InputSystem"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": true,
|
||||
"precompiledReferences": [
|
||||
"nunit.framework.dll"
|
||||
],
|
||||
"autoReferenced": false,
|
||||
"defineConstraints": [
|
||||
"UNITY_INCLUDE_TESTS"
|
||||
],
|
||||
"optionalUnityReferences": [
|
||||
"TestAssemblies"
|
||||
],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a04008c3dfb248c6ae1f4bac939b75c0
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 367142
|
||||
packageName: "MotionCore \u2013 Physics Character Controller"
|
||||
packageVersion: 1.2
|
||||
assetPath: Assets/Character Controller/Core/Tests/PlayMode/CharacterMovement.Core.PlayModeTests.asmdef
|
||||
uploadId: 997018
|
||||
+591
@@ -0,0 +1,591 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using NUnit.Framework;
|
||||
using UnityEngine;
|
||||
using UnityEngine.TestTools;
|
||||
|
||||
namespace CharacterMovement.Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// Covers the contracts add-on modules depend on: movement modifiers, capability gates,
|
||||
/// locomotion state, observable events, the external force API and input source swapping.
|
||||
/// </summary>
|
||||
public class PlayerControllerExtensibilityPlayModeTests
|
||||
{
|
||||
private readonly List<UnityEngine.Object> _createdObjects = new List<UnityEngine.Object>();
|
||||
private float _previousFixedDeltaTime;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_previousFixedDeltaTime = Time.fixedDeltaTime;
|
||||
Time.fixedDeltaTime = 1f / 60f;
|
||||
Physics.gravity = new Vector3(0f, -9.81f, 0f);
|
||||
|
||||
GameObject cameraObject = new GameObject("Test Main Camera") { tag = "MainCamera" };
|
||||
cameraObject.transform.rotation = Quaternion.identity;
|
||||
cameraObject.AddComponent<Camera>();
|
||||
Track(cameraObject);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
Time.fixedDeltaTime = _previousFixedDeltaTime;
|
||||
for (int i = _createdObjects.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (_createdObjects[i])
|
||||
UnityEngine.Object.Destroy(_createdObjects[i]);
|
||||
}
|
||||
|
||||
_createdObjects.Clear();
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator SpeedModifiersCombineMultiplicatively()
|
||||
{
|
||||
Rig rig = CreateRig();
|
||||
yield return WaitForFixedSteps(8);
|
||||
|
||||
MovementModifierHandle armor = rig.Controller.CreateMovementModifier("armor");
|
||||
MovementModifierHandle curse = rig.Controller.CreateMovementModifier("curse");
|
||||
armor.SpeedMultiplier = 0.5f;
|
||||
curse.SpeedMultiplier = 0.5f;
|
||||
rig.Controller.EvaluateMovementModifiers();
|
||||
|
||||
Assert.AreEqual(0.25f, rig.Controller.EffectiveSpeedMultiplier, 0.0001f);
|
||||
|
||||
rig.Controller.RemoveMovementModifier(curse);
|
||||
Assert.AreEqual(0.5f, rig.Controller.EffectiveSpeedMultiplier, 0.0001f);
|
||||
|
||||
rig.Controller.RemoveMovementModifier(armor);
|
||||
Assert.AreEqual(1f, rig.Controller.EffectiveSpeedMultiplier, 0.0001f);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator SpeedModifierActuallyLimitsTravelSpeed()
|
||||
{
|
||||
Rig rig = CreateRig();
|
||||
yield return WaitForFixedSteps(8);
|
||||
|
||||
rig.Input.MoveDirection = Vector2.up;
|
||||
yield return WaitForFixedSteps(45);
|
||||
float unmodifiedSpeed = rig.Controller.GetPlanarSpeed();
|
||||
|
||||
MovementModifierHandle slow = rig.Controller.CreateMovementModifier("slow");
|
||||
slow.SpeedMultiplier = 0.4f;
|
||||
yield return WaitForFixedSteps(45);
|
||||
|
||||
Assert.Less(rig.Controller.GetPlanarSpeed(), unmodifiedSpeed * 0.6f);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator BlockingSprintDropsTheCharacterBackToRunSpeed()
|
||||
{
|
||||
Rig rig = CreateRig();
|
||||
yield return WaitForFixedSteps(8);
|
||||
|
||||
rig.Input.MoveDirection = Vector2.up;
|
||||
rig.Input.IsSprinting = true;
|
||||
yield return WaitForFixedSteps(70);
|
||||
|
||||
Assert.IsTrue(rig.Controller.IsSprintRequested);
|
||||
Assert.AreEqual(LocomotionState.Sprinting, rig.Controller.MovementState);
|
||||
|
||||
MovementModifierHandle exhausted = rig.Controller.CreateMovementModifier("exhausted");
|
||||
exhausted.BlockedCapabilities = MovementCapability.Sprint;
|
||||
yield return WaitForFixedSteps(70);
|
||||
|
||||
Assert.IsFalse(rig.Controller.IsSprintRequested);
|
||||
Assert.IsFalse(rig.Controller.CanSprint);
|
||||
Assert.AreEqual(LocomotionState.Running, rig.Controller.MovementState);
|
||||
Assert.That(
|
||||
rig.Controller.GetPlanarSpeed(),
|
||||
Is.InRange(rig.Controller.Speed - 0.75f, rig.Controller.Speed + 0.75f));
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator BlockingJumpPreventsJumpingWithoutTouchingTheController()
|
||||
{
|
||||
Rig rig = CreateRig();
|
||||
yield return WaitForFixedSteps(10);
|
||||
|
||||
MovementModifierHandle stunned = rig.Controller.CreateMovementModifier("stunned");
|
||||
stunned.BlockedCapabilities = MovementCapability.Jump;
|
||||
rig.Controller.EvaluateMovementModifiers();
|
||||
|
||||
Assert.IsFalse(rig.Controller.CanStartJump());
|
||||
Assert.IsFalse(rig.Controller.TryJump());
|
||||
|
||||
rig.Controller.BufferJumpIntent();
|
||||
yield return WaitForFixedSteps(6);
|
||||
Assert.IsNotInstanceOf<JumpingState>(rig.Controller.CurrentState);
|
||||
|
||||
rig.Controller.RemoveMovementModifier(stunned);
|
||||
rig.Controller.BufferJumpIntent();
|
||||
yield return WaitForFixedSteps(3);
|
||||
Assert.IsInstanceOf<JumpingState>(rig.Controller.CurrentState);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator BlockingMovementStopsTheCharacter()
|
||||
{
|
||||
Rig rig = CreateRig();
|
||||
yield return WaitForFixedSteps(8);
|
||||
|
||||
rig.Input.MoveDirection = Vector2.up;
|
||||
yield return WaitForFixedSteps(40);
|
||||
Assert.Greater(rig.Controller.GetPlanarSpeed(), 1f);
|
||||
|
||||
MovementModifierHandle rooted = rig.Controller.CreateMovementModifier("rooted");
|
||||
rooted.BlockedCapabilities = MovementCapability.Move;
|
||||
yield return WaitForFixedSteps(40);
|
||||
|
||||
Assert.IsFalse(rig.Controller.CanMove);
|
||||
Assert.Less(rig.Controller.GetPlanarSpeed(), 0.2f);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator MovementStateChangedReportsTransitions()
|
||||
{
|
||||
Rig rig = CreateRig();
|
||||
yield return WaitForFixedSteps(10);
|
||||
|
||||
List<LocomotionState> observed = new List<LocomotionState>();
|
||||
rig.Controller.MovementStateChanged += (previous, next) => observed.Add(next);
|
||||
|
||||
rig.Input.MoveDirection = Vector2.up;
|
||||
yield return WaitForFixedSteps(20);
|
||||
rig.Controller.BufferJumpIntent();
|
||||
yield return WaitForFixedSteps(40);
|
||||
|
||||
CollectionAssert.Contains(observed, LocomotionState.Running);
|
||||
CollectionAssert.Contains(observed, LocomotionState.Jumping);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator GroundedAndLeftGroundFireOncePerTransition()
|
||||
{
|
||||
Rig rig = CreateRig();
|
||||
yield return WaitForFixedSteps(12);
|
||||
|
||||
int groundedCount = 0;
|
||||
int leftGroundCount = 0;
|
||||
rig.Controller.Grounded += () => groundedCount++;
|
||||
rig.Controller.LeftGround += () => leftGroundCount++;
|
||||
|
||||
rig.Controller.BufferJumpIntent();
|
||||
yield return WaitForFixedSteps(120);
|
||||
|
||||
Assert.AreEqual(1, leftGroundCount, "Exactly one takeoff expected.");
|
||||
Assert.AreEqual(1, groundedCount, "Exactly one touchdown expected.");
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator StartedAndStoppedMovingFireOncePerTransition()
|
||||
{
|
||||
Rig rig = CreateRig();
|
||||
yield return WaitForFixedSteps(12);
|
||||
|
||||
int started = 0;
|
||||
int stopped = 0;
|
||||
rig.Controller.StartedMoving += () => started++;
|
||||
rig.Controller.StoppedMoving += () => stopped++;
|
||||
|
||||
rig.Input.MoveDirection = Vector2.up;
|
||||
yield return WaitForFixedSteps(40);
|
||||
Assert.AreEqual(1, started);
|
||||
Assert.AreEqual(0, stopped);
|
||||
|
||||
rig.Input.MoveDirection = Vector2.zero;
|
||||
yield return WaitForFixedSteps(50);
|
||||
Assert.AreEqual(1, started);
|
||||
Assert.AreEqual(1, stopped);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator AddImpulseSurvivesLocomotionCorrection()
|
||||
{
|
||||
Rig rig = CreateRig();
|
||||
yield return WaitForFixedSteps(12);
|
||||
|
||||
float startZ = rig.Rigidbody.position.z;
|
||||
rig.Controller.AddImpulse(Vector3.forward * 10f);
|
||||
yield return WaitForFixedSteps(20);
|
||||
|
||||
Assert.Greater(
|
||||
rig.Rigidbody.position.z - startZ,
|
||||
0.8f,
|
||||
"A knockback impulse must not be cancelled by ground locomotion.");
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator UpwardImpulseLeavesTheGroundAndEntersAnAirborneState()
|
||||
{
|
||||
Rig rig = CreateRig();
|
||||
yield return WaitForFixedSteps(12);
|
||||
|
||||
rig.Controller.AddImpulse(Vector3.up * 7f);
|
||||
yield return WaitForFixedSteps(12);
|
||||
|
||||
Assert.IsTrue(rig.Controller.IsAirborne, rig.Controller.MovementState.ToString());
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator ExternalVelocityCanBeSetAndCleared()
|
||||
{
|
||||
Rig rig = CreateRig();
|
||||
yield return WaitForFixedSteps(12);
|
||||
|
||||
rig.Controller.SetExternalVelocity(Vector3.right * 5f);
|
||||
Assert.AreEqual(5f, rig.Controller.ExternalVelocity.x, 0.001f);
|
||||
|
||||
rig.Controller.ClearExternalVelocity();
|
||||
Assert.AreEqual(Vector3.zero, rig.Controller.ExternalVelocity);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator ControllerCanBeDrivenByACustomInputSource()
|
||||
{
|
||||
Rig rig = CreateRig();
|
||||
yield return WaitForFixedSteps(10);
|
||||
|
||||
ScriptedInputSource scripted = new ScriptedInputSource();
|
||||
rig.Controller.SetInputSource(scripted);
|
||||
scripted.MoveDirection = Vector2.up;
|
||||
yield return WaitForFixedSteps(40);
|
||||
|
||||
Assert.Greater(rig.Controller.GetPlanarSpeed(), 1f);
|
||||
|
||||
scripted.RaiseJump();
|
||||
yield return WaitForFixedSteps(3);
|
||||
Assert.IsInstanceOf<JumpingState>(rig.Controller.CurrentState);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator ToggleSprintStaysOnAfterTheInputIsReleased()
|
||||
{
|
||||
Rig rig = CreateRig();
|
||||
SetAutoProperty(rig.Controller, nameof(PlayerController.SprintActivation), SprintActivationMode.Toggle);
|
||||
yield return WaitForFixedSteps(10);
|
||||
|
||||
rig.Input.MoveDirection = Vector2.up;
|
||||
rig.Input.IsSprinting = true;
|
||||
yield return null;
|
||||
rig.Input.IsSprinting = false;
|
||||
yield return WaitForFixedSteps(40);
|
||||
|
||||
Assert.IsTrue(rig.Controller.IsSprintRequested, "A toggled sprint should persist after release.");
|
||||
|
||||
// Releasing the movement input ends the toggled sprint.
|
||||
rig.Input.MoveDirection = Vector2.zero;
|
||||
yield return WaitForFixedSteps(4);
|
||||
Assert.IsFalse(rig.Controller.IsSprintRequested);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator ReleasingJumpEarlyProducesALowerJump()
|
||||
{
|
||||
Rig tallRig = CreateRig();
|
||||
yield return WaitForFixedSteps(12);
|
||||
tallRig.Input.IsJumpHeld = true;
|
||||
float fullHeight = 0f;
|
||||
yield return MeasureJumpApex(tallRig, holdJump: true, apex: value => fullHeight = value);
|
||||
|
||||
Rig shortRig = CreateRig(new Vector3(6f, 0.08f, 0f));
|
||||
yield return WaitForFixedSteps(12);
|
||||
float cutHeight = 0f;
|
||||
yield return MeasureJumpApex(shortRig, holdJump: false, apex: value => cutHeight = value);
|
||||
|
||||
Assert.Less(
|
||||
cutHeight,
|
||||
fullHeight * 0.8f,
|
||||
$"Cut jump {cutHeight:F2}m should be clearly lower than held jump {fullHeight:F2}m.");
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator DisablingJumpInTheInspectorBlocksJumping()
|
||||
{
|
||||
Rig rig = CreateRig();
|
||||
SetAutoProperty(rig.Controller, nameof(PlayerController.AllowJump), false);
|
||||
yield return WaitForFixedSteps(10);
|
||||
|
||||
Assert.IsFalse(rig.Controller.IsCapabilityAllowed(MovementCapability.Jump));
|
||||
Assert.IsFalse(rig.Controller.CanStartJump());
|
||||
|
||||
rig.Controller.BufferJumpIntent();
|
||||
yield return WaitForFixedSteps(6);
|
||||
Assert.IsNotInstanceOf<JumpingState>(rig.Controller.CurrentState);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator DisablingSprintInTheInspectorCapsSpeedAtRunSpeed()
|
||||
{
|
||||
Rig rig = CreateRig();
|
||||
SetAutoProperty(rig.Controller, nameof(PlayerController.AllowSprint), false);
|
||||
yield return WaitForFixedSteps(10);
|
||||
|
||||
rig.Input.MoveDirection = Vector2.up;
|
||||
rig.Input.IsSprinting = true;
|
||||
yield return WaitForFixedSteps(70);
|
||||
|
||||
Assert.IsFalse(rig.Controller.CanSprint);
|
||||
Assert.AreEqual(LocomotionState.Running, rig.Controller.MovementState);
|
||||
Assert.That(
|
||||
rig.Controller.GetPlanarSpeed(),
|
||||
Is.InRange(rig.Controller.Speed - 0.75f, rig.Controller.Speed + 0.75f));
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator DisablingCrouchInTheInspectorKeepsTheCharacterStanding()
|
||||
{
|
||||
Rig rig = CreateRig();
|
||||
SetAutoProperty(rig.Controller, nameof(PlayerController.AllowCrouch), false);
|
||||
yield return WaitForFixedSteps(10);
|
||||
|
||||
Assert.IsFalse(rig.Controller.TryCrouch());
|
||||
Assert.AreEqual(PlayerStance.Standing, rig.Controller.CurrentStance);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator DisabledAbilitiesReportThroughTheSameCapabilityGate()
|
||||
{
|
||||
Rig rig = CreateRig();
|
||||
SetAutoProperty(rig.Controller, nameof(PlayerController.AllowDash), false);
|
||||
SetAutoProperty(rig.Controller, nameof(PlayerController.AllowVault), false);
|
||||
yield return WaitForFixedSteps(10);
|
||||
|
||||
// Design-time toggles and runtime modifiers must resolve through one gate, so a caller
|
||||
// never has to ask two different questions.
|
||||
Assert.IsFalse(rig.Controller.IsCapabilityAllowed(MovementCapability.Dash));
|
||||
Assert.IsFalse(rig.Controller.IsCapabilityAllowed(MovementCapability.Vault));
|
||||
Assert.IsFalse(rig.Controller.CanStartDash());
|
||||
Assert.IsTrue(rig.Controller.IsCapabilityAllowed(MovementCapability.Jump));
|
||||
|
||||
MovementCapability disabled = rig.Controller.DisabledAbilities;
|
||||
Assert.IsTrue((disabled & MovementCapability.Dash) != 0);
|
||||
Assert.IsTrue((disabled & MovementCapability.Vault) != 0);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator GroundProbeStillWorksWithSupplementaryRaysDisabled()
|
||||
{
|
||||
Rig rig = CreateRig();
|
||||
SetAutoProperty(rig.Controller, nameof(PlayerController.UseSupplementaryGroundRays), false);
|
||||
yield return WaitForFixedSteps(15);
|
||||
|
||||
Assert.IsTrue(rig.Controller.IsGrounded(), rig.Controller.GetDebugSummary());
|
||||
Assert.AreEqual(LocomotionState.Idle, rig.Controller.MovementState);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator FallGravityMultiplierShortensTimeToFall()
|
||||
{
|
||||
Rig slowRig = CreateRig(new Vector3(0f, 6f, 0f));
|
||||
yield return WaitForFixedSteps(4);
|
||||
float slowSpeed = 0f;
|
||||
yield return MeasureFallSpeed(slowRig, value => slowSpeed = value);
|
||||
|
||||
Rig fastRig = CreateRig(new Vector3(12f, 6f, 0f));
|
||||
SetAutoProperty(fastRig.Controller, nameof(PlayerController.FallGravityMultiplier), 2f);
|
||||
yield return WaitForFixedSteps(4);
|
||||
float fastSpeed = 0f;
|
||||
yield return MeasureFallSpeed(fastRig, value => fastSpeed = value);
|
||||
|
||||
Assert.Greater(
|
||||
fastSpeed,
|
||||
slowSpeed * 1.4f,
|
||||
$"Fall multiplier 2 reached {fastSpeed:F2} m/s where 1 reached {slowSpeed:F2} m/s.");
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator AssigningAPresetDoesNotOverrideAbilityToggles()
|
||||
{
|
||||
Rig rig = CreateRig();
|
||||
SetAutoProperty(rig.Controller, nameof(PlayerController.AllowDash), false);
|
||||
SetAutoProperty(rig.Controller, nameof(PlayerController.AllowSprint), false);
|
||||
|
||||
CharacterMotorSettings preset = ScriptableObject.CreateInstance<CharacterMotorSettings>();
|
||||
preset.name = "Test Preset";
|
||||
Track(preset);
|
||||
SetAutoProperty(rig.Controller, nameof(PlayerController.MotorSettings), preset);
|
||||
|
||||
// Start applies the preset. Feel values come from it; ability toggles must survive,
|
||||
// otherwise a designer could never turn an ability off on a character using a preset.
|
||||
yield return WaitForFixedSteps(12);
|
||||
|
||||
Assert.IsFalse(
|
||||
rig.Controller.AllowDash,
|
||||
"A preset must not switch abilities back on.");
|
||||
Assert.IsFalse(rig.Controller.AllowSprint);
|
||||
Assert.IsFalse(rig.Controller.IsCapabilityAllowed(MovementCapability.Dash));
|
||||
Assert.IsFalse(rig.Controller.CanSprint);
|
||||
|
||||
// The preset did drive the feel values.
|
||||
Assert.AreEqual(preset.RunSpeed, rig.Controller.Speed, 0.001f);
|
||||
}
|
||||
|
||||
private IEnumerator MeasureFallSpeed(Rig rig, Action<float> result)
|
||||
{
|
||||
yield return WaitForFixedSteps(18);
|
||||
result(-rig.Rigidbody.linearVelocity.y);
|
||||
}
|
||||
|
||||
private IEnumerator MeasureJumpApex(Rig rig, bool holdJump, Action<float> apex)
|
||||
{
|
||||
rig.Input.IsJumpHeld = true;
|
||||
float startY = rig.Rigidbody.position.y;
|
||||
rig.Controller.BufferJumpIntent();
|
||||
yield return WaitForFixedSteps(2);
|
||||
|
||||
if (!holdJump)
|
||||
rig.Input.IsJumpHeld = false;
|
||||
|
||||
float peak = startY;
|
||||
for (int i = 0; i < 90; i++)
|
||||
{
|
||||
yield return new WaitForFixedUpdate();
|
||||
peak = Mathf.Max(peak, rig.Rigidbody.position.y);
|
||||
if (rig.Rigidbody.linearVelocity.y < 0f && i > 3)
|
||||
break;
|
||||
}
|
||||
|
||||
rig.Input.IsJumpHeld = false;
|
||||
apex(peak - startY);
|
||||
}
|
||||
|
||||
private Rig CreateRig(Vector3? position = null)
|
||||
{
|
||||
CreateGround();
|
||||
|
||||
GameObject root = new GameObject("Extensibility Rig");
|
||||
root.transform.position = position ?? new Vector3(0f, 0.08f, 0f);
|
||||
Track(root);
|
||||
|
||||
Rigidbody rigidbody = root.AddComponent<Rigidbody>();
|
||||
rigidbody.freezeRotation = true;
|
||||
rigidbody.interpolation = RigidbodyInterpolation.None;
|
||||
|
||||
CapsuleCollider capsule = root.AddComponent<CapsuleCollider>();
|
||||
capsule.height = 1.8f;
|
||||
capsule.radius = 0.32f;
|
||||
capsule.center = new Vector3(0f, 0.9f, 0f);
|
||||
|
||||
ScriptedInputBehaviour input = root.AddComponent<ScriptedInputBehaviour>();
|
||||
|
||||
PlayerController controller = root.AddComponent<PlayerController>();
|
||||
SetAutoProperty(controller, nameof(PlayerController.RigidBody), rigidbody);
|
||||
SetAutoProperty(controller, nameof(PlayerController.CapsuleCollider), capsule);
|
||||
SetAutoProperty(controller, nameof(PlayerController.Speed), 5f);
|
||||
SetAutoProperty(controller, nameof(PlayerController.SprintSpeed), 8f);
|
||||
SetAutoProperty(controller, nameof(PlayerController.WalkSpeed), 2.2f);
|
||||
SetAutoProperty(controller, nameof(PlayerController.GroundAcceleration), 45f);
|
||||
SetAutoProperty(controller, nameof(PlayerController.GroundDeceleration), 55f);
|
||||
SetAutoProperty(controller, nameof(PlayerController.JumpingForce), 8f);
|
||||
SetAutoProperty(controller, nameof(PlayerController.CoyoteTime), 0.15f);
|
||||
SetAutoProperty(controller, nameof(PlayerController.JumpBufferTime), 0.2f);
|
||||
SetAutoProperty(controller, nameof(PlayerController.GroundLayerMask), (LayerMask)LayerMask.GetMask("Default"));
|
||||
SetAutoProperty(controller, nameof(PlayerController.StairsLayerMask), (LayerMask)LayerMask.GetMask("Default"));
|
||||
SetAutoProperty(controller, nameof(PlayerController.AlignToSlopeNormal), false);
|
||||
SetPrivateField(controller, "inputSourceBehaviour", input);
|
||||
SetPrivateField(controller, "lockCursorOnStart", false);
|
||||
|
||||
return new Rig(root, controller, input, rigidbody);
|
||||
}
|
||||
|
||||
private void CreateGround()
|
||||
{
|
||||
GameObject ground = GameObject.CreatePrimitive(PrimitiveType.Cube);
|
||||
ground.name = "Extensibility Ground";
|
||||
ground.transform.position = new Vector3(0f, -0.05f, 0f);
|
||||
ground.transform.localScale = new Vector3(40f, 0.1f, 40f);
|
||||
Track(ground);
|
||||
}
|
||||
|
||||
private IEnumerator WaitForFixedSteps(int steps)
|
||||
{
|
||||
for (int i = 0; i < steps; i++)
|
||||
yield return new WaitForFixedUpdate();
|
||||
}
|
||||
|
||||
private T Track<T>(T createdObject) where T : UnityEngine.Object
|
||||
{
|
||||
_createdObjects.Add(createdObject);
|
||||
return createdObject;
|
||||
}
|
||||
|
||||
private static void SetAutoProperty<T>(object target, string propertyName, T value)
|
||||
{
|
||||
SetPrivateField(target, $"<{propertyName}>k__BackingField", value);
|
||||
}
|
||||
|
||||
private static void SetPrivateField<T>(object target, string fieldName, T value)
|
||||
{
|
||||
FieldInfo field = target.GetType().GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
Assert.NotNull(field, $"Missing field '{fieldName}' on {target.GetType().Name}.");
|
||||
field.SetValue(target, value);
|
||||
}
|
||||
|
||||
private readonly struct Rig
|
||||
{
|
||||
public Rig(GameObject root, PlayerController controller, ScriptedInputBehaviour input, Rigidbody rigidbody)
|
||||
{
|
||||
Root = root;
|
||||
Controller = controller;
|
||||
Input = input;
|
||||
Rigidbody = rigidbody;
|
||||
}
|
||||
|
||||
public GameObject Root { get; }
|
||||
public PlayerController Controller { get; }
|
||||
public ScriptedInputBehaviour Input { get; }
|
||||
public Rigidbody Rigidbody { get; }
|
||||
}
|
||||
|
||||
/// <summary>A component input source, proving the serialized override path works.</summary>
|
||||
private sealed class ScriptedInputBehaviour : MonoBehaviour, ICharacterInputSource
|
||||
{
|
||||
public Vector2 MoveDirection { get; set; }
|
||||
public bool IsMoving => MoveDirection.sqrMagnitude > 0.0001f;
|
||||
public bool IsSprinting { get; set; }
|
||||
public bool IsWalking { get; set; }
|
||||
public bool IsCrouching { get; set; }
|
||||
public bool IsProne { get; set; }
|
||||
public bool IsJumpHeld { get; set; }
|
||||
|
||||
public event Action JumpPerformed;
|
||||
public event Action DashPerformed;
|
||||
public event Action CrouchPerformed;
|
||||
public event Action PronePerformed;
|
||||
|
||||
public void RaiseJump() => JumpPerformed?.Invoke();
|
||||
public void RaiseDash() => DashPerformed?.Invoke();
|
||||
public void RaiseCrouch() => CrouchPerformed?.Invoke();
|
||||
public void RaiseProne() => PronePerformed?.Invoke();
|
||||
}
|
||||
|
||||
/// <summary>A plain C# input source, proving SetInputSource works without a component.</summary>
|
||||
private sealed class ScriptedInputSource : ICharacterInputSource
|
||||
{
|
||||
public Vector2 MoveDirection { get; set; }
|
||||
public bool IsMoving => MoveDirection.sqrMagnitude > 0.0001f;
|
||||
public bool IsSprinting { get; set; }
|
||||
public bool IsWalking { get; set; }
|
||||
public bool IsCrouching { get; set; }
|
||||
public bool IsProne { get; set; }
|
||||
public bool IsJumpHeld { get; set; }
|
||||
|
||||
public event Action JumpPerformed;
|
||||
public event Action DashPerformed;
|
||||
public event Action CrouchPerformed;
|
||||
public event Action PronePerformed;
|
||||
|
||||
public void RaiseJump() => JumpPerformed?.Invoke();
|
||||
public void RaiseDash() => DashPerformed?.Invoke();
|
||||
public void RaiseCrouch() => CrouchPerformed?.Invoke();
|
||||
public void RaiseProne() => PronePerformed?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bc48394a7dd54e9d9a80d1288d251e3e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 367142
|
||||
packageName: "MotionCore \u2013 Physics Character Controller"
|
||||
packageVersion: 1.2
|
||||
assetPath: Assets/Character Controller/Core/Tests/PlayMode/PlayerControllerExtensibilityPlayModeTests.cs
|
||||
uploadId: 997018
|
||||
+641
@@ -0,0 +1,641 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using NUnit.Framework;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.TestTools;
|
||||
|
||||
namespace CharacterMovement.Tests
|
||||
{
|
||||
public class PlayerControllerMovementPlayModeTests
|
||||
{
|
||||
private readonly List<Object> _createdObjects = new List<Object>();
|
||||
private float _previousFixedDeltaTime;
|
||||
private GameObject _cameraObject;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_previousFixedDeltaTime = Time.fixedDeltaTime;
|
||||
Time.fixedDeltaTime = 1f / 60f;
|
||||
Physics.gravity = new Vector3(0f, -9.81f, 0f);
|
||||
|
||||
_cameraObject = new GameObject("Test Main Camera");
|
||||
_cameraObject.tag = "MainCamera";
|
||||
_cameraObject.transform.rotation = Quaternion.identity;
|
||||
_cameraObject.AddComponent<Camera>();
|
||||
Track(_cameraObject);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
Time.fixedDeltaTime = _previousFixedDeltaTime;
|
||||
|
||||
for (int i = _createdObjects.Count - 1; i >= 0; i--)
|
||||
{
|
||||
Object createdObject = _createdObjects[i];
|
||||
if (createdObject)
|
||||
Object.Destroy(createdObject);
|
||||
}
|
||||
|
||||
_createdObjects.Clear();
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator AcceleratesAndDeceleratesOnGround()
|
||||
{
|
||||
CreateGround();
|
||||
TestRig rig = CreateController();
|
||||
yield return WaitForFixedSteps(8);
|
||||
|
||||
SetMoveInput(rig.InputHandler, Vector2.up, false, false, false);
|
||||
yield return WaitForFixedSteps(45);
|
||||
float movingSpeed = rig.Controller.GetPlanarSpeed();
|
||||
|
||||
SetMoveInput(rig.InputHandler, Vector2.zero, false, false, false);
|
||||
yield return WaitForFixedSteps(35);
|
||||
|
||||
Assert.Greater(movingSpeed, rig.Controller.Speed * 0.65f);
|
||||
Assert.Less(rig.Controller.GetPlanarSpeed(), movingSpeed * 0.45f);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator SprintUsesConfiguredSprintSpeedAsTarget()
|
||||
{
|
||||
CreateGround();
|
||||
TestRig rig = CreateController();
|
||||
yield return WaitForFixedSteps(8);
|
||||
|
||||
SetMoveInput(rig.InputHandler, Vector2.up, true, false, false);
|
||||
yield return WaitForFixedSteps(80);
|
||||
|
||||
float speed = rig.Controller.GetPlanarSpeed();
|
||||
Assert.Greater(speed, rig.Controller.Speed + 1f);
|
||||
Assert.That(speed, Is.InRange(rig.Controller.SprintSpeed - 0.75f, rig.Controller.SprintSpeed + 0.75f));
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator BufferedJumpStartsJumpAndAppliesVerticalVelocity()
|
||||
{
|
||||
CreateGround();
|
||||
TestRig rig = CreateController();
|
||||
yield return WaitForFixedSteps(8);
|
||||
|
||||
rig.Controller.BufferJumpIntent();
|
||||
yield return WaitForFixedSteps(2);
|
||||
|
||||
Assert.IsInstanceOf<JumpingState>(rig.Controller.CurrentState);
|
||||
Assert.Greater(rig.Rigidbody.linearVelocity.y, rig.Controller.JumpingForce * 0.75f);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator CoyoteTimeAllowsJumpAfterLeavingGround()
|
||||
{
|
||||
GameObject ground = CreateGround();
|
||||
TestRig rig = CreateController();
|
||||
yield return WaitForFixedSteps(8);
|
||||
|
||||
Object.Destroy(ground);
|
||||
yield return null;
|
||||
rig.Controller.BufferJumpIntent();
|
||||
yield return WaitForFixedSteps(2);
|
||||
|
||||
Assert.IsInstanceOf<JumpingState>(rig.Controller.CurrentState);
|
||||
Assert.Greater(rig.Rigidbody.linearVelocity.y, rig.Controller.JumpingForce * 0.75f);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator JumpBufferExecutesWhenLanding()
|
||||
{
|
||||
CreateGround();
|
||||
TestRig rig = CreateController(new Vector3(0f, 1.65f, 0f));
|
||||
SetAutoProperty(rig.Controller, nameof(PlayerController.JumpBufferTime), 0.35f);
|
||||
yield return WaitForFixedSteps(6);
|
||||
|
||||
rig.Rigidbody.linearVelocity = new Vector3(0f, -7.5f, 0f);
|
||||
|
||||
while (rig.Rigidbody.position.y > 0.45f)
|
||||
yield return new WaitForFixedUpdate();
|
||||
|
||||
rig.Controller.BufferJumpIntent();
|
||||
yield return WaitForFixedSteps(18);
|
||||
|
||||
Assert.IsInstanceOf<JumpingState>(rig.Controller.CurrentState);
|
||||
Assert.Greater(rig.Rigidbody.linearVelocity.y, 0.5f);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator CrouchAndProneCannotStandIntoCeiling()
|
||||
{
|
||||
CreateGround();
|
||||
TestRig rig = CreateController();
|
||||
yield return WaitForFixedSteps(8);
|
||||
|
||||
Assert.IsTrue(rig.Controller.TryCrouch());
|
||||
SetMoveInput(rig.InputHandler, Vector2.zero, false, false, true);
|
||||
yield return WaitForFixedSteps(8);
|
||||
CreateBox("Low Ceiling", new Vector3(0f, 1.55f, 0f), new Vector3(2f, 0.2f, 2f));
|
||||
yield return WaitForFixedSteps(2);
|
||||
|
||||
Assert.IsFalse(rig.Controller.TryStand());
|
||||
Assert.AreEqual(PlayerStance.Crouching, rig.Controller.CurrentStance);
|
||||
|
||||
Assert.IsTrue(rig.Controller.TryProne());
|
||||
SetMoveInput(rig.InputHandler, Vector2.zero, false, false, false);
|
||||
SetAutoProperty(rig.InputHandler, nameof(InputHandler.IsProne), true);
|
||||
yield return WaitForFixedSteps(8);
|
||||
Assert.IsFalse(rig.Controller.TryStand());
|
||||
Assert.AreEqual(PlayerStance.Prone, rig.Controller.CurrentStance);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator SlopeWalkabilityRespectsConfiguredSlopeAngle()
|
||||
{
|
||||
CreateRamp(30f, new Vector3(0f, 0f, 0f));
|
||||
TestRig rig = CreateController(new Vector3(0f, 0.15f, 0f));
|
||||
SetAutoProperty(rig.Controller, nameof(PlayerController.MaxSlopeAngle), 45f);
|
||||
yield return WaitForFixedSteps(15);
|
||||
|
||||
Assert.IsTrue(rig.Controller.IsGrounded(), "A 30 degree slope should remain walkable.");
|
||||
|
||||
Object.Destroy(rig.Root);
|
||||
yield return null;
|
||||
CreateRamp(60f, new Vector3(5f, 0f, 0f));
|
||||
TestRig steepRig = CreateController(new Vector3(5f, 0.35f, 0f));
|
||||
SetAutoProperty(steepRig.Controller, nameof(PlayerController.MaxSlopeAngle), 45f);
|
||||
yield return WaitForFixedSteps(15);
|
||||
|
||||
Assert.IsFalse(steepRig.Controller.IsGrounded(), "A 60 degree slope should not be reported as grounded with a 45 degree limit.");
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator StepUpClimbsValidStepHeight()
|
||||
{
|
||||
CreateGround();
|
||||
CreateBox("Step", new Vector3(0f, 0.1f, 0.55f), new Vector3(1.2f, 0.2f, 0.35f));
|
||||
TestRig rig = CreateController();
|
||||
yield return WaitForFixedSteps(8);
|
||||
|
||||
float startY = rig.Rigidbody.position.y;
|
||||
rig.Controller.StepUp(Vector3.forward);
|
||||
yield return WaitForFixedSteps(2);
|
||||
|
||||
Assert.Greater(rig.Rigidbody.position.y, startY + 0.12f, rig.Controller.GetDebugSummary());
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator StepUpClimbsValidStepHeightAt30Hz()
|
||||
{
|
||||
yield return AssertStepUpClimbsAtFixedDelta(1f / 30f);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator StepUpClimbsValidStepHeightAt120Hz()
|
||||
{
|
||||
yield return AssertStepUpClimbsAtFixedDelta(1f / 120f);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator StepUpRejectsObstacleAboveConfiguredStepHeight()
|
||||
{
|
||||
CreateGround();
|
||||
CreateBox("Tall Step", new Vector3(0f, 0.35f, 0.55f), new Vector3(1.2f, 0.7f, 0.35f));
|
||||
TestRig rig = CreateController();
|
||||
SetAutoProperty(rig.Controller, nameof(PlayerController.StepHeight), 0.3f);
|
||||
yield return WaitForFixedSteps(8);
|
||||
|
||||
float startY = rig.Rigidbody.position.y;
|
||||
rig.Controller.StepUp(Vector3.forward);
|
||||
yield return WaitForFixedSteps(2);
|
||||
|
||||
Assert.Less(rig.Rigidbody.position.y, startY + 0.08f);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator UnevenTerrainRemainsGroundedDuringSlowTraversal()
|
||||
{
|
||||
CreateUnevenTerrain();
|
||||
TestRig rig = CreateController(new Vector3(-0.9f, 0.18f, 0f));
|
||||
yield return WaitForFixedSteps(10);
|
||||
|
||||
SetMoveInput(rig.InputHandler, Vector2.right, false, true, false);
|
||||
yield return WaitForFixedSteps(75);
|
||||
|
||||
Assert.IsTrue(rig.Controller.IsGrounded(), "Controller should remain grounded while traversing shallow uneven terrain.");
|
||||
Assert.IsNotInstanceOf<FallingState>(rig.Controller.CurrentState);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator MovingPlatformCarriesIdleCharacter()
|
||||
{
|
||||
GameObject platform = CreateBox("Moving Platform", new Vector3(0f, -0.05f, 0f), new Vector3(3f, 0.1f, 3f));
|
||||
Rigidbody platformBody = platform.AddComponent<Rigidbody>();
|
||||
platformBody.useGravity = false;
|
||||
platformBody.linearVelocity = new Vector3(1.75f, 0f, 0f);
|
||||
platform.AddComponent<MovingPlatform>();
|
||||
|
||||
TestRig rig = CreateController(new Vector3(0f, 0.08f, 0f));
|
||||
yield return WaitForFixedSteps(8);
|
||||
|
||||
float startX = rig.Rigidbody.position.x;
|
||||
yield return WaitForFixedSteps(45);
|
||||
|
||||
Assert.Greater(rig.Rigidbody.position.x, startX + 0.3f);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator RotatingMovingPlatformCarriesRelativeMotion()
|
||||
{
|
||||
GameObject platform = CreateBox("Rotating Platform", Vector3.zero, new Vector3(4f, 0.1f, 4f));
|
||||
platform.AddComponent<MovingPlatform>();
|
||||
platform.AddComponent<TestRotatingPlatform>().DegreesPerSecond = 90f;
|
||||
|
||||
TestRig rig = CreateController(new Vector3(0.9f, 0.08f, 0f));
|
||||
yield return WaitForFixedSteps(8);
|
||||
|
||||
Vector3 startPosition = rig.Rigidbody.position;
|
||||
yield return WaitForFixedSteps(60);
|
||||
|
||||
Assert.Greater(Vector3.ProjectOnPlane(rig.Rigidbody.position - startPosition, Vector3.up).magnitude, 0.35f);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator JumpFromMovingPlatformPreservesInheritedVelocity()
|
||||
{
|
||||
GameObject platform = CreateBox("Jump Platform", new Vector3(0f, -0.05f, 0f), new Vector3(3f, 0.1f, 3f));
|
||||
Rigidbody platformBody = platform.AddComponent<Rigidbody>();
|
||||
platformBody.useGravity = false;
|
||||
platformBody.linearVelocity = new Vector3(2f, 0f, 0f);
|
||||
platform.AddComponent<MovingPlatform>();
|
||||
|
||||
TestRig rig = CreateController(new Vector3(0f, 0.08f, 0f));
|
||||
yield return WaitForFixedSteps(12);
|
||||
|
||||
rig.Controller.BufferJumpIntent();
|
||||
yield return WaitForFixedSteps(2);
|
||||
|
||||
Assert.IsInstanceOf<JumpingState>(rig.Controller.CurrentState);
|
||||
Assert.Greater(rig.Rigidbody.linearVelocity.x, 1f);
|
||||
Assert.Greater(rig.Rigidbody.linearVelocity.y, 0.5f);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator JumpReachesBallisticApexHeight()
|
||||
{
|
||||
CreateGround();
|
||||
TestRig rig = CreateController();
|
||||
yield return WaitForFixedSteps(12);
|
||||
|
||||
float startY = rig.Rigidbody.position.y;
|
||||
rig.Controller.BufferJumpIntent();
|
||||
|
||||
float peakY = startY;
|
||||
for (int i = 0; i < 70; i++)
|
||||
{
|
||||
yield return new WaitForFixedUpdate();
|
||||
peakY = Mathf.Max(peakY, rig.Rigidbody.position.y);
|
||||
if (rig.Rigidbody.linearVelocity.y < 0f && i > 5)
|
||||
break;
|
||||
}
|
||||
|
||||
float jumpingForce = rig.Controller.JumpingForce;
|
||||
float expectedApex = jumpingForce * jumpingForce / (2f * Mathf.Abs(Physics.gravity.y));
|
||||
float measuredApex = peakY - startY;
|
||||
|
||||
Assert.Greater(
|
||||
measuredApex,
|
||||
expectedApex * 0.85f,
|
||||
$"Jump apex {measuredApex:F3}m fell far short of the ballistic {expectedApex:F3}m, "
|
||||
+ "which means downward assistance is being applied during the ascent.");
|
||||
Assert.Less(measuredApex, expectedApex * 1.15f);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator GroundedClearsOnceTheCapsuleLeavesTheSurface()
|
||||
{
|
||||
CreateGround();
|
||||
TestRig rig = CreateController();
|
||||
yield return WaitForFixedSteps(12);
|
||||
|
||||
Assert.IsTrue(rig.Controller.IsGrounded());
|
||||
|
||||
rig.Controller.Teleport(rig.Rigidbody.position + Vector3.up * 0.6f);
|
||||
yield return new WaitForFixedUpdate();
|
||||
|
||||
Assert.IsFalse(
|
||||
rig.Controller.IsGrounded(),
|
||||
"A capsule held well above the surface must not report as grounded.");
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator LandedFiresOnTouchdownAndNotWhenTheJumpStartsToFall()
|
||||
{
|
||||
CreateGround();
|
||||
TestRig rig = CreateController();
|
||||
yield return WaitForFixedSteps(12);
|
||||
|
||||
int landedCount = 0;
|
||||
rig.Controller.Landed += () => landedCount++;
|
||||
|
||||
rig.Controller.BufferJumpIntent();
|
||||
yield return WaitForFixedSteps(2);
|
||||
Assert.IsInstanceOf<JumpingState>(rig.Controller.CurrentState);
|
||||
|
||||
while (rig.Rigidbody.linearVelocity.y > 0f)
|
||||
yield return new WaitForFixedUpdate();
|
||||
|
||||
yield return WaitForFixedSteps(4);
|
||||
Assert.AreEqual(0, landedCount, "Starting to fall is not a landing.");
|
||||
|
||||
yield return WaitForFixedSteps(80);
|
||||
|
||||
Assert.IsInstanceOf<IGroundLocomotionState>(rig.Controller.CurrentState);
|
||||
Assert.AreEqual(1, landedCount, "Touching down should report exactly one landing.");
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator AirborneStatesAreNotGroundLocomotionStates()
|
||||
{
|
||||
CreateGround();
|
||||
TestRig rig = CreateController();
|
||||
yield return WaitForFixedSteps(8);
|
||||
|
||||
Assert.IsNotInstanceOf<IGroundLocomotionState>(new JumpingState(rig.Controller));
|
||||
Assert.IsNotInstanceOf<IGroundLocomotionState>(new FallingState(rig.Controller));
|
||||
Assert.IsNotInstanceOf<IGroundLocomotionState>(new DashingState(rig.Controller));
|
||||
Assert.IsInstanceOf<IGroundLocomotionState>(new IdleState(rig.Controller));
|
||||
Assert.IsInstanceOf<IGroundLocomotionState>(new MovingState(rig.Controller));
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator WalkingJumpKeepsWalkSpeedInTheAir()
|
||||
{
|
||||
CreateGround();
|
||||
TestRig rig = CreateController();
|
||||
yield return WaitForFixedSteps(8);
|
||||
|
||||
SetMoveInput(rig.InputHandler, Vector2.up, false, true, false);
|
||||
yield return WaitForFixedSteps(40);
|
||||
|
||||
float walkSpeed = rig.Controller.GetPlanarSpeed();
|
||||
Assert.That(walkSpeed, Is.InRange(rig.Controller.WalkSpeed - 0.6f, rig.Controller.WalkSpeed + 0.6f));
|
||||
|
||||
rig.Controller.BufferJumpIntent();
|
||||
yield return WaitForFixedSteps(16);
|
||||
|
||||
Assert.Less(
|
||||
rig.Controller.GetPlanarSpeed(),
|
||||
rig.Controller.Speed * 0.75f,
|
||||
"Jumping while walking must not accelerate the character towards run speed.");
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator ClimbingASlopeGainsHeightWithoutLeavingTheSurface()
|
||||
{
|
||||
const float rampAngle = 25f;
|
||||
GameObject ramp = CreateBox("Climb Ramp", Vector3.zero, new Vector3(4f, 0.12f, 8f));
|
||||
// Negative pitch so that walking towards +Z climbs the ramp.
|
||||
ramp.transform.rotation = Quaternion.Euler(-rampAngle, 0f, 0f);
|
||||
|
||||
// Drop the capsule onto the lower half of the ramp and let it settle before climbing.
|
||||
float startZ = -2f;
|
||||
float surfaceY = startZ * Mathf.Tan(rampAngle * Mathf.Deg2Rad) + 0.07f;
|
||||
TestRig rig = CreateController(new Vector3(0f, surfaceY + 0.05f, startZ));
|
||||
SetAutoProperty(rig.Controller, nameof(PlayerController.MaxSlopeAngle), 45f);
|
||||
yield return WaitForFixedSteps(25);
|
||||
|
||||
float startY = rig.Rigidbody.position.y;
|
||||
SetMoveInput(rig.InputHandler, Vector2.up, false, false, false);
|
||||
|
||||
for (int i = 0; i < 45; i++)
|
||||
{
|
||||
yield return new WaitForFixedUpdate();
|
||||
Assert.IsNotInstanceOf<FallingState>(
|
||||
rig.Controller.CurrentState,
|
||||
"Walking up a walkable ramp must never hand over to the falling state.");
|
||||
}
|
||||
|
||||
Assert.Greater(
|
||||
rig.Rigidbody.position.y,
|
||||
startY + 0.4f,
|
||||
rig.Controller.GetDebugSummary());
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator DoubleJumpModuleGrantsOneExtraJumpInTheAir()
|
||||
{
|
||||
CreateGround();
|
||||
TestRig rig = CreateController();
|
||||
DoubleJumpModule doubleJump = rig.Root.AddComponent<DoubleJumpModule>();
|
||||
SetPrivateField(doubleJump, "maxExtraJumps", 1);
|
||||
SetPrivateField(doubleJump, "doubleJumpForce", 8f);
|
||||
yield return WaitForFixedSteps(10);
|
||||
|
||||
rig.Controller.RegisterModule(doubleJump);
|
||||
|
||||
// A ground state entry is what refills the extra jump budget.
|
||||
SetMoveInput(rig.InputHandler, Vector2.up, false, false, false);
|
||||
yield return WaitForFixedSteps(10);
|
||||
SetMoveInput(rig.InputHandler, Vector2.zero, false, false, false);
|
||||
yield return WaitForFixedSteps(10);
|
||||
|
||||
rig.Controller.BufferJumpIntent();
|
||||
yield return WaitForFixedSteps(2);
|
||||
Assert.IsInstanceOf<JumpingState>(rig.Controller.CurrentState);
|
||||
|
||||
while (rig.Controller.RigidBody.linearVelocity.y > 1f)
|
||||
yield return new WaitForFixedUpdate();
|
||||
|
||||
float beforeExtraJump = rig.Rigidbody.linearVelocity.y;
|
||||
rig.Controller.BufferJumpIntent();
|
||||
yield return WaitForFixedSteps(3);
|
||||
|
||||
Assert.Greater(
|
||||
rig.Rigidbody.linearVelocity.y,
|
||||
beforeExtraJump + 3f,
|
||||
"The extra jump should relaunch the character while airborne.");
|
||||
Assert.IsInstanceOf<JumpingState>(rig.Controller.CurrentState);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator FallingTransitionStartsWhenGroundIsMissing()
|
||||
{
|
||||
TestRig rig = CreateController(new Vector3(0f, 2.5f, 0f));
|
||||
yield return WaitForFixedSteps(20);
|
||||
|
||||
Assert.IsInstanceOf<FallingState>(rig.Controller.CurrentState);
|
||||
}
|
||||
|
||||
private TestRig CreateController(Vector3? position = null)
|
||||
{
|
||||
GameObject root = new GameObject("Test Player");
|
||||
root.transform.position = position ?? new Vector3(0f, 0.08f, 0f);
|
||||
Track(root);
|
||||
|
||||
Rigidbody rigidbody = root.AddComponent<Rigidbody>();
|
||||
rigidbody.freezeRotation = true;
|
||||
rigidbody.interpolation = RigidbodyInterpolation.None;
|
||||
|
||||
CapsuleCollider capsule = root.AddComponent<CapsuleCollider>();
|
||||
capsule.height = 1.8f;
|
||||
capsule.radius = 0.32f;
|
||||
capsule.center = new Vector3(0f, 0.9f, 0f);
|
||||
|
||||
InputHandler inputHandler = root.AddComponent<InputHandler>();
|
||||
SetPrivateField(inputHandler, "movementActionsAsset", CreateMovementActions());
|
||||
SetPrivateField(inputHandler, "movementActionMapName", "Control");
|
||||
|
||||
PlayerController controller = root.AddComponent<PlayerController>();
|
||||
SetAutoProperty(controller, nameof(PlayerController.RigidBody), rigidbody);
|
||||
SetAutoProperty(controller, nameof(PlayerController.InputHandler), inputHandler);
|
||||
SetAutoProperty(controller, nameof(PlayerController.CapsuleCollider), capsule);
|
||||
SetAutoProperty(controller, nameof(PlayerController.Speed), 5f);
|
||||
SetAutoProperty(controller, nameof(PlayerController.SprintSpeed), 8f);
|
||||
SetAutoProperty(controller, nameof(PlayerController.WalkSpeed), 2.2f);
|
||||
SetAutoProperty(controller, nameof(PlayerController.GroundAcceleration), 45f);
|
||||
SetAutoProperty(controller, nameof(PlayerController.GroundDeceleration), 55f);
|
||||
SetAutoProperty(controller, nameof(PlayerController.AirAcceleration), 12f);
|
||||
SetAutoProperty(controller, nameof(PlayerController.AirDeceleration), 2f);
|
||||
SetAutoProperty(controller, nameof(PlayerController.JumpingForce), 8f);
|
||||
SetAutoProperty(controller, nameof(PlayerController.CoyoteTime), 0.2f);
|
||||
SetAutoProperty(controller, nameof(PlayerController.JumpBufferTime), 0.2f);
|
||||
SetAutoProperty(controller, nameof(PlayerController.CrouchCapsuleHeight), 1.15f);
|
||||
SetAutoProperty(controller, nameof(PlayerController.ProneCapsuleHeight), 0.55f);
|
||||
SetAutoProperty(controller, nameof(PlayerController.StanceTransitionSpeed), 100f);
|
||||
SetAutoProperty(controller, nameof(PlayerController.StepHeight), 0.35f);
|
||||
SetAutoProperty(controller, nameof(PlayerController.StepCheckDistance), 0.28f);
|
||||
SetAutoProperty(controller, nameof(PlayerController.GroundSnapDistance), 0.18f);
|
||||
SetAutoProperty(controller, nameof(PlayerController.GroundStickForce), 20f);
|
||||
SetAutoProperty(controller, nameof(PlayerController.GroundLayerMask), (LayerMask)LayerMask.GetMask("Default"));
|
||||
SetAutoProperty(controller, nameof(PlayerController.StairsLayerMask), (LayerMask)LayerMask.GetMask("Default"));
|
||||
SetAutoProperty(controller, nameof(PlayerController.AlignToSlopeNormal), false);
|
||||
|
||||
return new TestRig(root, controller, inputHandler, rigidbody);
|
||||
}
|
||||
|
||||
private GameObject CreateGround()
|
||||
{
|
||||
return CreateBox("Ground", new Vector3(0f, -0.05f, 0f), new Vector3(20f, 0.1f, 20f));
|
||||
}
|
||||
|
||||
private GameObject CreateRamp(float angle, Vector3 position)
|
||||
{
|
||||
GameObject ramp = CreateBox($"Ramp {angle}", position, new Vector3(4f, 0.12f, 4f));
|
||||
ramp.transform.rotation = Quaternion.Euler(angle, 0f, 0f);
|
||||
return ramp;
|
||||
}
|
||||
|
||||
private void CreateUnevenTerrain()
|
||||
{
|
||||
CreateBox("Uneven Base", new Vector3(0f, -0.08f, 0f), new Vector3(5f, 0.1f, 3f));
|
||||
for (int i = 0; i < 7; i++)
|
||||
{
|
||||
float x = -1.2f + i * 0.4f;
|
||||
float height = 0.04f + (i % 3) * 0.025f;
|
||||
CreateBox($"Uneven Tile {i}", new Vector3(x, height * 0.5f, 0f), new Vector3(0.45f, height, 1.4f));
|
||||
}
|
||||
}
|
||||
|
||||
private GameObject CreateBox(string name, Vector3 position, Vector3 scale)
|
||||
{
|
||||
GameObject box = GameObject.CreatePrimitive(PrimitiveType.Cube);
|
||||
box.name = name;
|
||||
box.transform.position = position;
|
||||
box.transform.localScale = scale;
|
||||
Track(box);
|
||||
return box;
|
||||
}
|
||||
|
||||
private InputActionAsset CreateMovementActions()
|
||||
{
|
||||
InputActionAsset asset = ScriptableObject.CreateInstance<InputActionAsset>();
|
||||
asset.name = "Test Movement Actions";
|
||||
InputActionMap map = asset.AddActionMap("Control");
|
||||
map.AddAction("Move", InputActionType.Value);
|
||||
map.AddAction("Jump", InputActionType.Button);
|
||||
map.AddAction("Crouch", InputActionType.Button);
|
||||
map.AddAction("Prone", InputActionType.Button);
|
||||
map.AddAction("Dash", InputActionType.Button);
|
||||
map.AddAction("Walk", InputActionType.Button);
|
||||
map.AddAction("Sprint", InputActionType.Button);
|
||||
Track(asset);
|
||||
return asset;
|
||||
}
|
||||
|
||||
private IEnumerator WaitForFixedSteps(int steps)
|
||||
{
|
||||
for (int i = 0; i < steps; i++)
|
||||
yield return new WaitForFixedUpdate();
|
||||
}
|
||||
|
||||
private IEnumerator AssertStepUpClimbsAtFixedDelta(float fixedDeltaTime)
|
||||
{
|
||||
Time.fixedDeltaTime = fixedDeltaTime;
|
||||
CreateGround();
|
||||
CreateBox($"Step {fixedDeltaTime}", new Vector3(0f, 0.1f, 0.55f), new Vector3(1.2f, 0.2f, 0.35f));
|
||||
TestRig rig = CreateController();
|
||||
yield return WaitForFixedSteps(8);
|
||||
|
||||
float startY = rig.Rigidbody.position.y;
|
||||
rig.Controller.StepUp(Vector3.forward);
|
||||
yield return WaitForFixedSteps(2);
|
||||
|
||||
Assert.Greater(rig.Rigidbody.position.y, startY + 0.12f, rig.Controller.GetDebugSummary());
|
||||
}
|
||||
|
||||
private void SetMoveInput(InputHandler inputHandler, Vector2 moveDirection, bool sprint, bool walk, bool crouch)
|
||||
{
|
||||
SetAutoProperty(inputHandler, nameof(InputHandler.MoveDirection), moveDirection);
|
||||
SetAutoProperty(inputHandler, nameof(InputHandler.IsMoving), moveDirection.sqrMagnitude > 0.0001f);
|
||||
SetAutoProperty(inputHandler, nameof(InputHandler.IsSprinting), sprint);
|
||||
SetAutoProperty(inputHandler, nameof(InputHandler.IsWalking), walk);
|
||||
SetAutoProperty(inputHandler, nameof(InputHandler.IsCrouching), crouch);
|
||||
}
|
||||
|
||||
private T Track<T>(T createdObject) where T : Object
|
||||
{
|
||||
_createdObjects.Add(createdObject);
|
||||
return createdObject;
|
||||
}
|
||||
|
||||
private static void SetAutoProperty<T>(object target, string propertyName, T value)
|
||||
{
|
||||
SetPrivateField(target, $"<{propertyName}>k__BackingField", value);
|
||||
}
|
||||
|
||||
private static void SetPrivateField<T>(object target, string fieldName, T value)
|
||||
{
|
||||
FieldInfo field = target.GetType().GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
Assert.NotNull(field, $"Missing field '{fieldName}' on {target.GetType().Name}.");
|
||||
field.SetValue(target, value);
|
||||
}
|
||||
|
||||
private readonly struct TestRig
|
||||
{
|
||||
public TestRig(GameObject root, PlayerController controller, InputHandler inputHandler, Rigidbody rigidbody)
|
||||
{
|
||||
Root = root;
|
||||
Controller = controller;
|
||||
InputHandler = inputHandler;
|
||||
Rigidbody = rigidbody;
|
||||
}
|
||||
|
||||
public GameObject Root { get; }
|
||||
public PlayerController Controller { get; }
|
||||
public InputHandler InputHandler { get; }
|
||||
public Rigidbody Rigidbody { get; }
|
||||
}
|
||||
|
||||
private sealed class TestRotatingPlatform : MonoBehaviour
|
||||
{
|
||||
public float DegreesPerSecond { get; set; }
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
transform.Rotate(Vector3.up, DegreesPerSecond * Time.fixedDeltaTime, Space.World);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 10956db09fec4d35876485d86b59c974
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 367142
|
||||
packageName: "MotionCore \u2013 Physics Character Controller"
|
||||
packageVersion: 1.2
|
||||
assetPath: Assets/Character Controller/Core/Tests/PlayMode/PlayerControllerMovementPlayModeTests.cs
|
||||
uploadId: 997018
|
||||
Reference in new issue
Block a user