| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- 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;
- [DisplayOnly] public Platform platform;
- private void Awake()
- {
- rb = GetComponentInParent<Rigidbody>();
- moveCharacter = rb.transform.GetComponent<MoveCharacter>();
- }
- private void FixedUpdate()
- {
- if (trigGroundList.Count == 0)
- {
- if (!haveGravity|| moveCharacter.platformPosY != 0 || moveCharacter.platformRotZ!=0)
- {
- rb.useGravity = true;
- haveGravity = true;
- moveCharacter.velocityAddition = 0;
- moveCharacter.platformPosY = 0;
- moveCharacter.platformRotZ = 0;
- }
- }
- else
- {
- platform = null;
- foreach (var item in trigGroundList)
- {
- Platform newPlatform = item.GetComponent<Platform>();
- if (newPlatform != null)
- {
- if (moveCharacter.transform.position.x < newPlatform.right.position.x
- && moveCharacter.transform.position.x > newPlatform.left.position.x)
- {
- if(platform == null)
- {
- platform = newPlatform;
- }
- else if (newPlatform.haveMagnetic)
- {
- platform = newPlatform;
- }
-
- }
- }
- }
-
- 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.platformPosY = platform.GetPosY(rb.transform.position);
- float targetRotZ = platform.GetRotZ();
- if (targetRotZ != moveCharacter.platformRotZ)
- {
- moveCharacter.platformRotZ = targetRotZ;
- moveCharacter.RotLerpTime = 0;
- }
-
- rb.transform.position = new Vector3(rb.transform.position.x, moveCharacter.platformPosY, rb.transform.position.z);
-
- //rb.transform.rotation = Quaternion.Euler(0, 0, moveCharacter.platformRotZ);
- haveGravity = false;
- }
- }
- else
- {
- if (platform.rb != null)
- {
- moveCharacter.velocityAddition = platform.rb.velocity.x;
- }
- moveCharacter.platformPosY = platform.GetPosY(rb.transform.position);
- float targetRotZ = platform.GetRotZ();
- if (targetRotZ != moveCharacter.platformRotZ)
- {
- moveCharacter.platformRotZ = targetRotZ;
- moveCharacter.RotLerpTime = 0;
- }
- }
- }
- 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);
- Platform platform = other.GetComponent<Platform>();
- if (platform!=null)
- {
- platform.feet.Add(this);
- }
- }
- }
- }
- private void OnTriggerExit(Collider other)
- {
- for (int i = 0; i < trigGroundList.Count; i++)
- {
- if (trigGroundList[i] == other.gameObject)
- {
- trigGroundList.RemoveAt(i);
- i--;
- Platform platform = other.GetComponent<Platform>();
- if (platform != null)
- {
- platform.feet.Remove(this);
- }
- }
- }
- }
- }
|