This commit is contained in:
Alexandre Coirier
2026-03-17 23:39:01 +01:00
parent 758a1d420c
commit 7058396d8e
5 changed files with 10477 additions and 23 deletions

View File

@@ -0,0 +1,145 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &-7645745693084663736
MonoBehaviour:
m_ObjectHideFlags: 11
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3}
m_Name:
m_EditorClassIdentifier: Unity.RenderPipelines.Universal.Editor::UnityEditor.Rendering.Universal.AssetVersion
version: 10
--- !u!21 &2100000
Material:
serializedVersion: 8
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: mat_particles
m_Shader: {fileID: 4800000, guid: 650dd9526735d5b46b79224bc6e94025, type: 3}
m_Parent: {fileID: 0}
m_ModifiedSerializedProperties: 0
m_ValidKeywords:
- _SURFACE_TYPE_TRANSPARENT
m_InvalidKeywords:
- _EMISSION
m_LightmapFlags: 2
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: 3000
stringTagMap:
RenderType: Transparent
disabledShaderPasses:
- MOTIONVECTORS
- DepthOnly
- SHADOWCASTER
m_LockedProperties:
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BaseMap:
m_Texture: {fileID: 10912, guid: 0000000000000000f000000000000000, type: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 10912, guid: 0000000000000000f000000000000000, type: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _SpecGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- unity_Lightmaps:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- unity_LightmapsInd:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- unity_ShadowMasks:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Ints: []
m_Floats:
- _AddPrecomputedVelocity: 0
- _AlphaClip: 0
- _AlphaToMask: 0
- _Blend: 2
- _BlendModePreserveSpecular: 1
- _BlendOp: 0
- _BumpScale: 1
- _ClearCoatMask: 0
- _ClearCoatSmoothness: 0
- _Cull: 2
- _Cutoff: 0.5
- _DetailAlbedoMapScale: 1
- _DetailNormalMapScale: 1
- _DstBlend: 1
- _DstBlendAlpha: 1
- _EnvironmentReflections: 1
- _GlossMapScale: 1
- _Glossiness: 0
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _QueueOffset: 0
- _ReceiveShadows: 1
- _SampleGI: 0
- _Smoothness: 0
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 5
- _SrcBlendAlpha: 1
- _Surface: 1
- _UVSec: 0
- _WorkflowMode: 1
- _XRMotionVectorsPass: 1
- _ZWrite: 0
m_Colors:
- _BaseColor: {r: 0.699921, g: 0.86611784, b: 0.9808553, a: 1}
- _Color: {r: 0.69992095, g: 0.86611784, b: 0.9808553, a: 1}
- _EmissionColor: {r: 0.30197895, g: 3.0626981, b: 4.9245777, a: 1}
- _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1}
m_BuildTextureStacks: []
m_AllowLocking: 1

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: ffa6df1368692e0408a69a3a03c931f2
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 2100000
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@@ -17,7 +17,8 @@ public class JetPack : MonoBehaviour
public BoxCollider Collider;
public SphereCollider Trigger;
public Rigidbody Rigidbody;
public ParticleSystem Particles;
public Transform Load;
public ParticleSystem[] Particles;
public InputActionAsset InputActions;
}
@@ -64,16 +65,24 @@ public class JetPack : MonoBehaviour
{
if (_jumpAction.IsPressed())
{
_wasPressed = true;
_duration -= Time.deltaTime;
_smooth = Mathf.Clamp01(_smooth + Time.deltaTime);
//Player.Instance.SetExtraForce(Vector3.up * _settings.Force * _smooth, true);
}
else if (_wasPressed)
else
{
_smooth = 0;
_wasPressed = false;
ResetExtraForce();
_smooth = Mathf.Clamp01(_smooth - 2 * Time.deltaTime);
}
Player.Instance.SetForce(Vector3.up * _settings.Force * _smooth);
_references.Load.localScale = new Vector3(1, _duration / _settings.Duration, 1);
foreach (ParticleSystem pSystem in _references.Particles)
{
if (!pSystem.isPlaying && _smooth >= .1f)
pSystem.Play();
if (pSystem.isPlaying && _smooth <= .05f)
pSystem.Stop();
}
}
else
@@ -106,7 +115,7 @@ public class JetPack : MonoBehaviour
if (!_state.Equiped)
return;
ResetExtraForce();
Player.Instance.SetForce(Vector3.zero);
_state.Equiped = false;
_duration = _settings.Duration;
@@ -119,11 +128,11 @@ public class JetPack : MonoBehaviour
_references.Trigger.enabled = false;
_references.Rigidbody.isKinematic = false;
_references.Rigidbody.AddRelativeTorque(random, ForceMode.Impulse);
}
_references.Load.gameObject.SetActive(false);
private void ResetExtraForce()
{
//Player.Instance.SetExtraForce(Vector3.zero, false);
foreach (ParticleSystem pSystem in _references.Particles)
if (pSystem.isPlaying)
pSystem.Stop();
}
}

View File

@@ -43,6 +43,11 @@ public class Player : MonoBehaviour
[Tooltip("Layers considered as ground")]
public LayerMask GroundLayer = 1;
[Header("Forces")]
[Tooltip("Decay rate of extra forces (m/s²)")]
public float ExtraForcesDrag = 8f;
[Header("Debug")]
[Tooltip("GUI logs of current state")]
@@ -102,6 +107,11 @@ public class Player : MonoBehaviour
private Vector3 _lastPlatformPosition;
private Quaternion _lastPlatformRotation;
// External forces
private Vector3 _impulseForce; // AddForce: decays automatically each frame
private Vector3 _persistentForce; // SetForce: maintained by caller
private Vector3 _platformVelocity; // Inherited on leaving platform: decays automatically
// Events
public event Action<PlayerState, PlayerState> OnStateChanged;
#endregion
@@ -164,7 +174,6 @@ public class Player : MonoBehaviour
_references.Controller.stepOffset = 0.4f;
CharacterController cc = _references.Controller;
_groundCheckRayOffset = cc.center + Vector3.up * (-cc.height * .5f - cc.skinWidth + _settings.GroundTolerance);
_groundCheckSphereOffset = cc.center + Vector3.up * (-cc.height * .5f + cc.radius - cc.skinWidth - _settings.GroundTolerance);
_groundCheckRadius = cc.radius;
@@ -198,7 +207,28 @@ public class Player : MonoBehaviour
#endregion
#region Physics
/// <summary>
/// Applies a one-shot impulse in any direction.
/// Decays automatically at ExtraForcesDrag rate.
/// Suitable for trampolines, explosions, knockbacks.
/// </summary>
public void AddForce(Vector3 force)
{
_impulseForce += force;
}
/// <summary>
/// Sets a persistent force applied every frame until cleared.
/// Caller is responsible for resetting to Vector3.zero.
/// Suitable for conveyor belts, jetpacks, wind zones.
/// </summary>
public void SetForce(Vector3 force)
{
_persistentForce = force;
if (force.y > 0)
_state.Velocity.y = 0;
}
#endregion
#region Public Methods
@@ -250,13 +280,16 @@ public class Player : MonoBehaviour
{
// Raycast for center contact
Vector3 rayOrigin = transform.position + _groundCheckRayOffset;
bool rayHit = Physics.Raycast(rayOrigin, Vector3.down, out RaycastHit rayInfo, _settings.GroundTolerance * 2f, _settings.GroundLayer);
bool rayHit = Physics.Raycast(rayOrigin, Vector3.down, out RaycastHit rayInfo,
_settings.GroundTolerance * 2f, _settings.GroundLayer);
// OverlapSphere for edge contact
// OverlapSphere for edge contact — works even when already intersecting
Vector3 sphereOrigin = transform.position + _groundCheckSphereOffset;
int overlapCount = Physics.OverlapSphereNonAlloc(sphereOrigin, _groundCheckRadius, _overlapResults, _settings.GroundLayer);
int overlapCount = Physics.OverlapSphereNonAlloc(sphereOrigin, _groundCheckRadius,
_overlapResults, _settings.GroundLayer);
bool sphereHit = overlapCount > 0;
bool wasGrounded = _state.IsGrounded;
_state.IsGrounded = rayHit || sphereHit;
if (_state.IsGrounded)
@@ -285,18 +318,23 @@ public class Player : MonoBehaviour
}
// Translation delta
transform.position += _state.Ground.position - _lastPlatformPosition;
Vector3 platformDelta = _state.Ground.position - _lastPlatformPosition;
transform.position += platformDelta;
// Store current state for next frame
_lastPlatformPosition = _state.Ground.position;
_lastPlatformRotation = _state.Ground.rotation;
// Sync physics broadphase to the new transform, the CharacterController
// doesn't see a stale overlap and generate a corrective push
// Sync physics broadphase — prevents CC from seeing stale overlap
Physics.SyncTransforms();
}
else
{
// Just left the ground: inherit platform velocity so the player
// doesn't stop dead in the air when jumping off a moving platform
if (wasGrounded && _state.Ground != null)
_platformVelocity = (_state.Ground.position - _lastPlatformPosition) / Time.deltaTime;
_state.Ground = null;
}
}
@@ -305,17 +343,36 @@ public class Player : MonoBehaviour
{
if (_state.IsGrounded && _state.Velocity.y < 0)
_state.Velocity.y = STICK_FORCE;
else
else if (_persistentForce.y <= 0)
_state.Velocity.y = Mathf.Max(_state.Velocity.y + GRAVITY * deltaTime, MAX_GRAVITY);
}
private void SetVelocity(float deltaTime)
{
// Movement attenuation based on state
float moveAtten;
switch (_state.CurrentState)
{
case PlayerState.Idle:
case PlayerState.Moving:
moveAtten = 1f;
break;
case PlayerState.Jumping:
moveAtten = .8f;
break;
case PlayerState.Falling:
moveAtten = .6f;
break;
default:
moveAtten = 0f;
break;
}
Vector2 input = _moveAction.ReadValue<Vector2>();
Vector3 forward = Vector3.ProjectOnPlane(_camera.transform.forward, Vector3.up).normalized;
Vector3 right = Vector3.ProjectOnPlane(_camera.transform.right, Vector3.up).normalized;
float speed = _settings.Speed * KMH_TO_MS;
float speed = _settings.Speed * KMH_TO_MS * moveAtten;
Vector3 moveInput = (forward * input.y + right * input.x) * speed;
_state.Velocity.x = moveInput.x;
@@ -341,7 +398,19 @@ public class Player : MonoBehaviour
private void SetMovement(float deltaTime)
{
_references.Controller.Move(_state.Velocity * deltaTime);
Vector3 totalVelocity = _state.Velocity + _impulseForce + _persistentForce + _platformVelocity;
_references.Controller.Move(totalVelocity * deltaTime);
// Decay impulse force
_impulseForce = Vector3.MoveTowards(_impulseForce, Vector3.zero,
_settings.ExtraForcesDrag * deltaTime);
// Decay inherited platform velocity — reset when back on ground
if (_state.IsGrounded)
_platformVelocity = Vector3.zero;
else
_platformVelocity = Vector3.MoveTowards(_platformVelocity, Vector3.zero,
_settings.ExtraForcesDrag * deltaTime);
}
private void SetState()