namespace Fusion.Addons.KCC
{
using System;
using UnityEngine;
// This file contains API which manipulates with KCCData or KCCSettings.
public partial class KCC
{
// PUBLIC METHODS
///
/// Controls execution of the KCC.
///
public void SetActive(bool isActive)
{
if (CheckSpawned() == false)
return;
_renderData.IsActive = isActive;
if (IsInFixedUpdate == true)
{
_fixedData.IsActive = isActive;
}
RefreshCollider(isActive);
}
///
/// Set non-interpolated world space input direction. Vector with magnitude greater than 1.0f is normalized.
/// Changes done in render will vanish with next fixed update.
///
public void SetInputDirection(Vector3 direction, bool clampToNormalized = true)
{
if (clampToNormalized == true)
{
direction.ClampToNormalized();
}
_renderData.InputDirection = direction;
if (IsInFixedUpdate == true)
{
_fixedData.InputDirection = direction;
}
}
///
/// Returns current look rotation.
///
public Vector2 GetLookRotation(bool pitch = true, bool yaw = true)
{
return Data.GetLookRotation(pitch, yaw);
}
///
/// Add pitch and yaw look rotation. Resulting values are clamped to <-90, 90> (pitch) and <-180, 180> (yaw).
/// Changes done in render will vanish with next fixed update.
///
public void AddLookRotation(float pitchDelta, float yawDelta)
{
KCCData data = _renderData;
data.AddLookRotation(pitchDelta, yawDelta);
if (IsInFixedUpdate == true)
{
data = _fixedData;
data.AddLookRotation(pitchDelta, yawDelta);
}
SynchronizeTransform(data, false, true, false, false);
}
///
/// Add pitch and yaw look rotation. Resulting values are clamped to <minPitch, maxPitch> (pitch) and <-180, 180> (yaw).
/// Changes done in render will vanish with next fixed update.
///
public void AddLookRotation(float pitchDelta, float yawDelta, float minPitch, float maxPitch)
{
KCCData data = _renderData;
data.AddLookRotation(pitchDelta, yawDelta, minPitch, maxPitch);
if (IsInFixedUpdate == true)
{
data = _fixedData;
data.AddLookRotation(pitchDelta, yawDelta, minPitch, maxPitch);
}
SynchronizeTransform(data, false, true, false, false);
}
///
/// Add pitch (x) and yaw (y) look rotation. Resulting values are clamped to <-90, 90> (pitch) and <-180, 180> (yaw).
/// Changes done in render will vanish with next fixed update.
///
public void AddLookRotation(Vector2 lookRotationDelta)
{
AddLookRotation(lookRotationDelta.x, lookRotationDelta.y);
}
///
/// Add pitch (x) and yaw (y) look rotation. Resulting values are clamped to <minPitch, maxPitch> (pitch) and <-180, 180> (yaw).
/// Changes done in render will vanish with next fixed update.
///
public void AddLookRotation(Vector2 lookRotationDelta, float minPitch, float maxPitch)
{
AddLookRotation(lookRotationDelta.x, lookRotationDelta.y, minPitch, maxPitch);
}
///
/// Set pitch and yaw look rotation. Values are clamped to <-90, 90> (pitch) and <-180, 180> (yaw).
/// Changes done in render will vanish with next fixed update.
///
public void SetLookRotation(float pitch, float yaw)
{
KCCData data = _renderData;
data.SetLookRotation(pitch, yaw);
if (IsInFixedUpdate == true)
{
data = _fixedData;
data.SetLookRotation(pitch, yaw);
}
SynchronizeTransform(data, false, true, false, false);
}
///
/// Set pitch and yaw look rotation. Values are clamped to <minPitch, maxPitch> (pitch) and <-180, 180> (yaw).
/// Changes done in render will vanish with next fixed update.
///
public void SetLookRotation(float pitch, float yaw, float minPitch, float maxPitch)
{
KCCData data = _renderData;
data.SetLookRotation(pitch, yaw, minPitch, maxPitch);
if (IsInFixedUpdate == true)
{
data = _fixedData;
data.SetLookRotation(pitch, yaw, minPitch, maxPitch);
}
SynchronizeTransform(data, false, true, false, false);
}
///
/// Set pitch and yaw look rotation. Values are clamped to <-90, 90> (pitch) and <-180, 180> (yaw).
/// Changes done in render will vanish with next fixed update.
///
public void SetLookRotation(Vector2 lookRotation)
{
SetLookRotation(lookRotation.x, lookRotation.y);
}
///
/// Set pitch and yaw look rotation. Values are clamped to <minPitch, maxPitch> (pitch) and <-180, 180> (yaw).
/// Changes done in render will vanish with next fixed update.
///
public void SetLookRotation(Vector2 lookRotation, float minPitch, float maxPitch)
{
SetLookRotation(lookRotation.x, lookRotation.y, minPitch, maxPitch);
}
///
/// Set pitch and yaw look rotation. Roll is ignored (not supported). Values are clamped to <-90, 90> (pitch) and <-180, 180> (yaw).
/// Changes done in render will vanish with next fixed update.
///
public void SetLookRotation(Quaternion lookRotation, bool preservePitch = false, bool preserveYaw = false)
{
KCCData data = _renderData;
data.SetLookRotation(lookRotation, preservePitch, preserveYaw);
if (IsInFixedUpdate == true)
{
data = _fixedData;
data.SetLookRotation(lookRotation, preservePitch, preserveYaw);
}
SynchronizeTransform(data, false, true, false, false);
}
///
/// Add jump impulse, which should be propagated by processors to KCCData.DynamicVelocity.
/// Changes done in render will vanish with next fixed update.
///
public void Jump(Vector3 impulse)
{
_renderData.JumpImpulse += impulse;
if (IsInFixedUpdate == true)
{
_fixedData.JumpImpulse += impulse;
}
}
///
/// Add velocity from external sources. Should propagate in processors to KCCData.DynamicVelocity.
/// Changes done in render will vanish with next fixed update.
///
public void AddExternalVelocity(Vector3 velocity)
{
_renderData.ExternalVelocity += velocity;
if (IsInFixedUpdate == true)
{
_fixedData.ExternalVelocity += velocity;
}
}
///
/// Set velocity from external sources. Should propagate in processors to KCCData.DynamicVelocity.
/// Changes done in render will vanish with next fixed update.
///
public void SetExternalVelocity(Vector3 velocity)
{
_renderData.ExternalVelocity = velocity;
if (IsInFixedUpdate == true)
{
_fixedData.ExternalVelocity = velocity;
}
}
///
/// Add acceleration from external sources. Should propagate in processors to KCCData.DynamicVelocity.
/// Changes done in render will vanish with next fixed update.
///
public void AddExternalAcceleration(Vector3 acceleration)
{
_renderData.ExternalAcceleration += acceleration;
if (IsInFixedUpdate == true)
{
_fixedData.ExternalAcceleration += acceleration;
}
}
///
/// Set acceleration from external sources. Should propagate in processors to KCCData.DynamicVelocity.
/// Changes done in render will vanish with next fixed update.
///
public void SetExternalAcceleration(Vector3 acceleration)
{
_renderData.ExternalAcceleration = acceleration;
if (IsInFixedUpdate == true)
{
_fixedData.ExternalAcceleration = acceleration;
}
}
///
/// Add impulse from external sources. Should propagate in processors to KCCData.DynamicVelocity.
/// Changes done in render will vanish with next fixed update.
///
public void AddExternalImpulse(Vector3 impulse)
{
_renderData.ExternalImpulse += impulse;
if (IsInFixedUpdate == true)
{
_fixedData.ExternalImpulse += impulse;
}
}
///
/// Set impulse from external sources. Should propagate in processors to KCCData.DynamicVelocity.
/// Changes done in render will vanish with next fixed update.
///
public void SetExternalImpulse(Vector3 impulse)
{
_renderData.ExternalImpulse = impulse;
if (IsInFixedUpdate == true)
{
_fixedData.ExternalImpulse = impulse;
}
}
///
/// Add force from external sources. Should propagate in processors to KCCData.DynamicVelocity.
/// Changes done in render will vanish with next fixed update.
///
public void AddExternalForce(Vector3 force)
{
_renderData.ExternalForce += force;
if (IsInFixedUpdate == true)
{
_fixedData.ExternalForce += force;
}
}
///
/// Set force from external sources. Should propagate in processors to KCCData.DynamicVelocity.
/// Changes done in render will vanish with next fixed update.
///
public void SetExternalForce(Vector3 force)
{
_renderData.ExternalForce = force;
if (IsInFixedUpdate == true)
{
_fixedData.ExternalForce = force;
}
}
///
/// Add position delta from external sources. Will be consumed by following update.
/// Changes done in render will vanish with next fixed update.
///
public void AddExternalDelta(Vector3 delta)
{
_renderData.ExternalDelta += delta;
if (IsInFixedUpdate == true)
{
_fixedData.ExternalDelta += delta;
}
}
///
/// Set position delta from external sources. Will be consumed by following update.
/// Changes done in render will vanish with next fixed update.
///
public void SetExternalDelta(Vector3 delta)
{
_renderData.ExternalDelta = delta;
if (IsInFixedUpdate == true)
{
_fixedData.ExternalDelta = delta;
}
}
///
/// Set KCCData.DynamicVelocity.
/// Changes done in render will vanish with next fixed update.
///
public void SetDynamicVelocity(Vector3 velocity)
{
_renderData.DynamicVelocity = velocity;
if (IsInFixedUpdate == true)
{
_fixedData.DynamicVelocity = velocity;
}
}
///
/// Set KCCData.KinematicVelocity.
/// Changes done in render will vanish with next fixed update.
///
public void SetKinematicVelocity(Vector3 velocity)
{
_renderData.KinematicVelocity = velocity;
if (IsInFixedUpdate == true)
{
_fixedData.KinematicVelocity = velocity;
}
}
///
/// Sets positions in current KCCData and immediately synchronizes Transform and Rigidbody components.
/// Teleporting from within a processor stage effectively stops any pending move steps and forces KCC to update hits with new overlap query.
/// Changes done in render will vanish with next fixed update.
///
/// New position, propagates to KCCData.BasePosition, KCCData.DesiredPosition, KCCData.TargetPosition.
/// Teleporting sets KCCData.HasTeleported and clears KCCData.IsSteppingUp and KCCData.IsSnappingToGround.
/// Allows anti-jitter feature. This has effect only in render.
/// Use Rigidbody.MovePosition() instead of setting Rigidbody.position directly.
public void SetPosition(Vector3 position, bool teleport = true, bool allowAntiJitter = false, bool moveRigidbody = false)
{
KCCData data = _renderData;
data.BasePosition = position;
data.DesiredPosition = position;
data.TargetPosition = position;
if (teleport == true)
{
data.HasTeleported = true;
data.IsSteppingUp = false;
data.IsSnappingToGround = false;
}
if (IsInFixedUpdate == true)
{
data = _fixedData;
data.BasePosition = position;
data.DesiredPosition = position;
data.TargetPosition = position;
if (teleport == true)
{
data.HasTeleported = true;
data.IsSteppingUp = false;
data.IsSnappingToGround = false;
}
allowAntiJitter = false;
}
SynchronizeTransform(data, true, false, allowAntiJitter, moveRigidbody);
}
///
/// Update Shape, Radius (optional), Height (optional) in settings and immediately synchronize with Collider.
///
/// - None - Skips internal physics query, collider is despawned.
/// - Capsule - Full physics processing, Capsule collider spawned.
///
///
public void SetShape(EKCCShape shape, float radius = 0.0f, float height = 0.0f)
{
_settings.Shape = shape;
if (radius > 0.0f) { _settings.Radius = radius; }
if (height > 0.0f) { _settings.Height = height; }
RefreshCollider(Data.IsActive);
}
///
/// Update IsTrigger flag in settings and immediately synchronize with Collider.
///
public void SetTrigger(bool isTrigger)
{
_settings.IsTrigger = isTrigger;
RefreshCollider(Data.IsActive);
}
///
/// Update Radius in settings and immediately synchronize with Collider.
///
public void SetRadius(float radius)
{
if (radius <= 0.0f)
return;
_settings.Radius = radius;
RefreshCollider(Data.IsActive);
}
///
/// Update Height in settings and immediately synchronize with Collider.
///
public void SetHeight(float height)
{
if (height <= 0.0f)
return;
_settings.Height = height;
RefreshCollider(Data.IsActive);
}
///
/// Update ColliderLayer in settings and immediately synchronize with Collider.
///
public void SetColliderLayer(int layer)
{
_settings.ColliderLayer = layer;
RefreshCollider(Data.IsActive);
}
///
/// Update CollisionLayerMask in settings.
///
public void SetCollisionLayerMask(LayerMask layerMask)
{
_settings.CollisionLayerMask = layerMask;
}
}
}