Integration of the octo input system and centralization of player values
This commit is contained in:
@@ -0,0 +1,330 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class OctoIkArmsImproved : MonoBehaviour
|
||||
{
|
||||
// Stores raycast hit information
|
||||
private class RaycastInfos
|
||||
{
|
||||
public bool HasHit;
|
||||
public float Distance;
|
||||
public float MaxDistance;
|
||||
public Vector3 Position;
|
||||
public Vector3 Normal;
|
||||
public Transform Transform;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
private class RaycastRig
|
||||
{
|
||||
public Transform Root;
|
||||
public float Distance = 1f;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
private class ArmRig
|
||||
{
|
||||
public bool Enabled = true;
|
||||
public OctoIkLimb Arm;
|
||||
public Vector3 RotationOffset;
|
||||
public RaycastRig[] Raycasts;
|
||||
|
||||
[HideInInspector] public Vector3 TargetOriginPos;
|
||||
[HideInInspector] public Quaternion TargetOriginRot;
|
||||
[HideInInspector] public Vector3 PoleOriginPos;
|
||||
|
||||
public void Init()
|
||||
{
|
||||
if (Arm)
|
||||
{
|
||||
if (Arm.Target)
|
||||
{
|
||||
TargetOriginPos = Arm.Target.localPosition;
|
||||
TargetOriginRot = Arm.Target.localRotation;
|
||||
}
|
||||
|
||||
if (Arm.Pole)
|
||||
{
|
||||
PoleOriginPos = Arm.Pole.localPosition;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
private class Settings
|
||||
{
|
||||
[Header("Raycast")]
|
||||
public LayerMask LayerMask;
|
||||
|
||||
[Header("Stickiness")]
|
||||
[Tooltip("Distance to start sticking (meters)")]
|
||||
[Range(0.1f, 2f)]
|
||||
public float StickStartDistance = 0.8f;
|
||||
|
||||
[Tooltip("Distance to fully release (meters)")]
|
||||
[Range(0.5f, 3f)]
|
||||
public float StickReleaseDistance = 1.2f;
|
||||
|
||||
[Header("Smoothing")]
|
||||
[Tooltip("Stick speed (units/sec)")]
|
||||
[Range(1f, 20f)]
|
||||
public float StickSpeed = 10f;
|
||||
|
||||
[Tooltip("Release speed (units/sec)")]
|
||||
[Range(1f, 20f)]
|
||||
public float ReleaseSpeed = 6f;
|
||||
|
||||
[Tooltip("Rotation speed (deg/sec)")]
|
||||
[Range(90f, 720f)]
|
||||
public float RotationSpeed = 360f;
|
||||
|
||||
[Header("Arm Selection")]
|
||||
[Tooltip("Can both arms stick at the same time?")]
|
||||
public bool AllowBothArms = true;
|
||||
|
||||
[Tooltip("Max surface angle to accept sticking (degrees)")]
|
||||
[Range(0f, 90f)]
|
||||
public float MaxSurfaceAngle = 60f;
|
||||
|
||||
[Header("Temporal Smoothing")]
|
||||
[Tooltip("Frames before releasing (prevents flickering)")]
|
||||
[Range(0, 10)]
|
||||
public int StabilityFrames = 3;
|
||||
}
|
||||
|
||||
[SerializeField] private ArmRig _leftArm;
|
||||
[SerializeField] private ArmRig _rightArm;
|
||||
[SerializeField] private Settings _settings;
|
||||
|
||||
private ArmRig[] _arms;
|
||||
private RaycastInfos[][] _raycasts = new RaycastInfos[2][];
|
||||
private RaycastInfos[] _nearestHitRaycasts = new RaycastInfos[2];
|
||||
private int[] _noHitFrameCount = new int[2];
|
||||
private float[] _currentStickiness = new float[2];
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_arms = new ArmRig[2] { _leftArm, _rightArm };
|
||||
foreach (ArmRig armRig in _arms)
|
||||
armRig.Init();
|
||||
|
||||
// Initialize raycast arrays for both arms
|
||||
_raycasts = new RaycastInfos[_arms.Length][];
|
||||
for (int i = 0; i < _arms.Length; i++)
|
||||
{
|
||||
_raycasts[i] = new RaycastInfos[_arms[i].Raycasts.Length];
|
||||
for (int u = 0; u < _raycasts[i].Length; u++)
|
||||
_raycasts[i][u] = new RaycastInfos() { MaxDistance = 1 };
|
||||
}
|
||||
|
||||
_nearestHitRaycasts = new RaycastInfos[_arms.Length];
|
||||
for (int i = 0; i < _nearestHitRaycasts.Length; i++)
|
||||
_nearestHitRaycasts[i] = new RaycastInfos() { MaxDistance = 1 };
|
||||
|
||||
_noHitFrameCount = new int[_arms.Length];
|
||||
_currentStickiness = new float[_arms.Length];
|
||||
}
|
||||
|
||||
void OnDrawGizmos()
|
||||
{
|
||||
if (Application.isPlaying || !enabled) return;
|
||||
|
||||
foreach (ArmRig armRig in new ArmRig[2] { _leftArm, _rightArm })
|
||||
{
|
||||
if (armRig.Raycasts == null) continue;
|
||||
|
||||
for (int i = 0; i < armRig.Raycasts.Length; i++)
|
||||
{
|
||||
RaycastRig raycastRig = armRig.Raycasts[i];
|
||||
if (raycastRig.Root == null) continue;
|
||||
|
||||
Debug.DrawRay(raycastRig.Root.position, raycastRig.Root.TransformDirection(Vector3.forward) * raycastRig.Distance, Color.cyan);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
PerformRaycasts();
|
||||
FindNearestHits();
|
||||
UpdateArmPositions();
|
||||
}
|
||||
|
||||
// Cast all configured raycasts and store results
|
||||
private void PerformRaycasts()
|
||||
{
|
||||
for (int i = 0; i < _arms.Length; i++)
|
||||
{
|
||||
ArmRig armRig = _arms[i];
|
||||
|
||||
if (!armRig.Enabled || armRig.Raycasts == null)
|
||||
continue;
|
||||
|
||||
for (int u = 0; u < armRig.Raycasts.Length; u++)
|
||||
{
|
||||
RaycastRig raycastRig = armRig.Raycasts[u];
|
||||
if (raycastRig.Root == null) continue;
|
||||
|
||||
RaycastInfos raycastInfos = _raycasts[i][u];
|
||||
RaycastHit hit;
|
||||
|
||||
Vector3 origin = raycastRig.Root.position;
|
||||
Vector3 direction = raycastRig.Root.TransformDirection(Vector3.forward);
|
||||
|
||||
if (Physics.Raycast(origin, direction, out hit, raycastRig.Distance, _settings.LayerMask))
|
||||
{
|
||||
Debug.DrawRay(origin, direction * hit.distance, Color.green);
|
||||
|
||||
raycastInfos.HasHit = true;
|
||||
raycastInfos.Distance = hit.distance;
|
||||
raycastInfos.MaxDistance = raycastRig.Distance;
|
||||
raycastInfos.Position = hit.point;
|
||||
raycastInfos.Normal = hit.normal;
|
||||
raycastInfos.Transform = hit.transform;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.DrawRay(origin, direction * raycastRig.Distance, Color.red);
|
||||
|
||||
raycastInfos.HasHit = false;
|
||||
raycastInfos.Distance = raycastRig.Distance;
|
||||
raycastInfos.MaxDistance = raycastRig.Distance;
|
||||
raycastInfos.Position = Vector3.zero;
|
||||
raycastInfos.Normal = Vector3.zero;
|
||||
raycastInfos.Transform = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Find the closest raycast hit for each arm
|
||||
private void FindNearestHits()
|
||||
{
|
||||
for (int i = 0; i < _arms.Length; i++)
|
||||
{
|
||||
ArmRig armRig = _arms[i];
|
||||
RaycastInfos nearest = _nearestHitRaycasts[i];
|
||||
|
||||
nearest.HasHit = false;
|
||||
float closestDistance = float.MaxValue;
|
||||
|
||||
if (armRig.Raycasts == null) continue;
|
||||
|
||||
// Find the closest hit among all raycasts for this arm
|
||||
for (int u = 0; u < armRig.Raycasts.Length; u++)
|
||||
{
|
||||
RaycastInfos raycastInfos = _raycasts[i][u];
|
||||
|
||||
if (raycastInfos.HasHit && raycastInfos.Distance < closestDistance)
|
||||
{
|
||||
closestDistance = raycastInfos.Distance;
|
||||
|
||||
nearest.HasHit = true;
|
||||
nearest.Distance = raycastInfos.Distance;
|
||||
nearest.MaxDistance = raycastInfos.MaxDistance;
|
||||
nearest.Position = raycastInfos.Position;
|
||||
nearest.Normal = raycastInfos.Normal;
|
||||
nearest.Transform = raycastInfos.Transform;
|
||||
}
|
||||
}
|
||||
|
||||
// Track frames without hits for temporal smoothing
|
||||
if (!nearest.HasHit)
|
||||
{
|
||||
_noHitFrameCount[i]++;
|
||||
}
|
||||
else
|
||||
{
|
||||
_noHitFrameCount[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update arm positions based on raycast hits
|
||||
private void UpdateArmPositions()
|
||||
{
|
||||
for (int i = 0; i < _arms.Length; i++)
|
||||
{
|
||||
ArmRig armRig = _arms[i];
|
||||
if (armRig.Arm == null || armRig.Arm.Target == null) continue;
|
||||
|
||||
RaycastInfos raycastInfos = _nearestHitRaycasts[i];
|
||||
|
||||
// Default to origin position/rotation
|
||||
Vector3 targetPos = armRig.TargetOriginPos;
|
||||
Quaternion targetRot = armRig.TargetOriginRot;
|
||||
|
||||
bool shouldStick = false;
|
||||
|
||||
// Check if we should stick to surface
|
||||
if (raycastInfos.HasHit && _noHitFrameCount[i] < _settings.StabilityFrames)
|
||||
{
|
||||
if (IsValidSurface(raycastInfos, i))
|
||||
{
|
||||
// Calculate target position on surface
|
||||
targetPos = armRig.Arm.Target.parent.InverseTransformPoint(raycastInfos.Position);
|
||||
|
||||
// Calculate target rotation aligned to surface
|
||||
targetRot = Quaternion.LookRotation(
|
||||
-armRig.Arm.Target.parent.InverseTransformDirection(raycastInfos.Normal),
|
||||
transform.up
|
||||
) * Quaternion.Euler(armRig.RotationOffset);
|
||||
|
||||
shouldStick = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Smooth stickiness transition
|
||||
float targetStickiness = shouldStick ? 1f : 0f;
|
||||
float stickinessSpeed = shouldStick ? _settings.StickSpeed : _settings.ReleaseSpeed;
|
||||
_currentStickiness[i] = Mathf.MoveTowards(_currentStickiness[i], targetStickiness, stickinessSpeed * Time.deltaTime);
|
||||
|
||||
// Apply distance-based stickiness factor (hysteresis)
|
||||
float distanceFactor = 0f;
|
||||
if (shouldStick && raycastInfos.HasHit)
|
||||
{
|
||||
distanceFactor = 1f - Mathf.InverseLerp(_settings.StickStartDistance, _settings.StickReleaseDistance, raycastInfos.Distance);
|
||||
distanceFactor = Mathf.Clamp01(distanceFactor);
|
||||
}
|
||||
|
||||
float finalStickiness = _currentStickiness[i] * distanceFactor;
|
||||
|
||||
// Calculate interpolation speeds
|
||||
float posSpeed = Mathf.Lerp(_settings.ReleaseSpeed, _settings.StickSpeed, finalStickiness);
|
||||
float rotSpeed = _settings.RotationSpeed * Mathf.Deg2Rad;
|
||||
|
||||
// Exponential smoothing for natural movement
|
||||
float tPos = 1f - Mathf.Exp(-posSpeed * Time.deltaTime);
|
||||
float tRot = 1f - Mathf.Exp(-rotSpeed * Time.deltaTime);
|
||||
|
||||
// Apply smooth interpolation
|
||||
armRig.Arm.Target.localPosition = Vector3.Lerp(armRig.Arm.Target.localPosition, targetPos, tPos);
|
||||
armRig.Arm.Target.localRotation = Quaternion.Slerp(armRig.Arm.Target.localRotation, targetRot, tRot);
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the surface is valid for sticking
|
||||
private bool IsValidSurface(RaycastInfos raycast, int armIndex)
|
||||
{
|
||||
if (raycast.Transform == null)
|
||||
return false;
|
||||
|
||||
// Check surface angle (reject surfaces too angled)
|
||||
float surfaceAngle = Vector3.Angle(-raycast.Normal, transform.forward);
|
||||
if (surfaceAngle > _settings.MaxSurfaceAngle)
|
||||
return false;
|
||||
|
||||
// If not allowing both arms, check if arm is on correct side
|
||||
if (!_settings.AllowBothArms)
|
||||
{
|
||||
Vector3 hitLocalPos = transform.InverseTransformPoint(raycast.Position);
|
||||
bool isLeftSide = hitLocalPos.x < 0;
|
||||
bool isLeftArm = armIndex == 0;
|
||||
|
||||
if (isLeftSide != isLeftArm)
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 97777a4e863d974428be79c0d8a40883
|
||||
Reference in New Issue
Block a user