summaryrefslogtreecommitdiff
path: root/GameCode/LegRaycasters.cs
diff options
context:
space:
mode:
authorchai <215380520@qq.com>2023-10-27 11:05:14 +0800
committerchai <215380520@qq.com>2023-10-27 11:05:14 +0800
commit766cdff5ffa72b65d7f106658d1603f47739b2ba (patch)
tree34d7799a94dfa9be182825577583c0fa6dc935f7 /GameCode/LegRaycasters.cs
+ init
Diffstat (limited to 'GameCode/LegRaycasters.cs')
-rw-r--r--GameCode/LegRaycasters.cs77
1 files changed, 77 insertions, 0 deletions
diff --git a/GameCode/LegRaycasters.cs b/GameCode/LegRaycasters.cs
new file mode 100644
index 0000000..db676b0
--- /dev/null
+++ b/GameCode/LegRaycasters.cs
@@ -0,0 +1,77 @@
+using UnityEngine;
+
+public class LegRaycasters : MonoBehaviour
+{
+ public LayerMask mask;
+
+ public float force;
+
+ public float drag;
+
+ public Transform[] legCastPositions;
+
+ public AnimationCurve animationCurve;
+
+ private PlayerVelocity rig;
+
+ private CharacterData data;
+
+ public AnimationCurve wobbleCurve;
+
+ public AnimationCurve forceCurve;
+
+ private IkLeg[] legs;
+
+ private float totalStepTime;
+
+ private void Awake()
+ {
+ legs = base.transform.root.GetComponentsInChildren<IkLeg>();
+ }
+
+ private void Start()
+ {
+ rig = GetComponentInParent<PlayerVelocity>();
+ data = GetComponentInParent<CharacterData>();
+ }
+
+ private void FixedUpdate()
+ {
+ totalStepTime = 0f;
+ for (int i = 0; i < legs.Length; i++)
+ {
+ if (!legs[i].footDown)
+ {
+ totalStepTime += legs[i].stepTime;
+ }
+ }
+ for (int j = 0; j < legCastPositions.Length; j++)
+ {
+ RaycastHit2D[] array = Physics2D.RaycastAll(legCastPositions[j].transform.position + Vector3.up * 0.5f, Vector2.down, 1f * base.transform.root.localScale.x, mask);
+ for (int k = 0; k < array.Length; k++)
+ {
+ if ((bool)array[k].transform && array[k].transform.root != base.transform.root)
+ {
+ HitGround(legCastPositions[j], array[k]);
+ break;
+ }
+ }
+ }
+ }
+
+ private void HitGround(Transform leg, RaycastHit2D hit)
+ {
+ if (!(data.sinceJump < 0.2f) && !(Vector3.Angle(Vector3.up, hit.normal) > 70f))
+ {
+ data.TouchGround(hit.point, hit.normal, hit.rigidbody);
+ Vector3 vector = ((Vector3)hit.point - leg.transform.position) / base.transform.root.localScale.x;
+ if (data.input.direction.x != 0f)
+ {
+ vector.y += wobbleCurve.Evaluate(totalStepTime) * base.transform.root.localScale.x;
+ rig.AddForce(Vector3.up * forceCurve.Evaluate(totalStepTime) * rig.mass);
+ }
+ rig.AddForce(animationCurve.Evaluate(Mathf.Abs(vector.y)) * Vector3.up * rig.mass * force);
+ rig.AddForce(animationCurve.Evaluate(Mathf.Abs(vector.y)) * (0f - rig.velocity.y) * Vector2.up * rig.mass * drag);
+ }
+ }
+}