| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- public class Foot : MonoBehaviour
- {
- [FoldoutGroup("×é¼þ")] public Rigidbody rb;
- [FoldoutGroup("×é¼þ")] public MoveCharacter moveCharacter;
- public bool haveGravity = true;
- public bool TrigGround
- {
- get
- {
- return trigGroundList.Count > 0;
- }
- }
- public List<GameObject> trigGroundList;
- private void Awake()
- {
- rb = GetComponentInParent<Rigidbody>();
- moveCharacter = rb.transform.GetComponent<MoveCharacter>();
- }
- private void FixedUpdate()
- {
- if (trigGroundList.Count == 0)
- {
- if (!haveGravity|| moveCharacter.platformY != 0)
- {
- rb.useGravity = true;
- haveGravity = true;
- moveCharacter.velocityAddition = 0;
- moveCharacter.platformY = 0;
- }
- }
- else
- {
- Platform platform = null;
- foreach (var item in trigGroundList)
- {
- platform = item.GetComponent<Platform>();
- if (platform!=null)
- {
- if (moveCharacter.transform.position.x < platform.right.position.x
- && moveCharacter.transform.position.x > platform.left.position.x)
- {
- break;
- }
- }
- else
- {
- break;
- }
- }
-
- if (platform != null)
- {
- if (haveGravity)
- {
- if (rb.velocity.y <= 0)
- {
- rb.useGravity = false;
- rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
- moveCharacter.platformY = platform.GetPosY(rb.transform.position);
- rb.transform.position = new Vector3(rb.transform.position.x, moveCharacter.platformY, rb.transform.position.z);
- haveGravity = false;
- }
- }
- else
- {
- if (platform.rb != null)
- {
- moveCharacter.velocityAddition = platform.rb.velocity.x;
- }
- moveCharacter.platformY = platform.GetPosY(rb.transform.position);
- }
- }
- else
- {
- if (!haveGravity)
- {
- rb.useGravity = true;
- haveGravity = true;
- }
- }
- }
- }
- private void OnTriggerEnter(Collider other)
- {
- if (other.CompareTag("Ground")|| other.CompareTag("Platform"))
- {
- if (!trigGroundList.Exists(t => t == other.gameObject))
- {
- trigGroundList.Add(other.gameObject);
- }
- }
- }
- private void OnTriggerExit(Collider other)
- {
- for (int i = 0; i < trigGroundList.Count; i++)
- {
- if (trigGroundList[i] == other.gameObject)
- {
- trigGroundList.RemoveAt(i);
- i--;
- }
- }
- }
- }
|