JetPack
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user