3D遊戲鏈遊系統開發(原始碼)

搭建MrsFu123 發表於 2022-05-26

using 
UnityEngine;


public class Leg : MonoBehaviour
{
    //可編輯
   [SerializeField] public Transform leg;
   [SerializeField] private Transform foot;
   [SerializeField] private Transform legRoot;    
   [SerializeField] private Transform IK_Target;    
   [SerializeField] private float length;
   [SerializeField] private Leg otherLeg;
   [SerializeField] private Transform rayOrigin;
   [SerializeField] private LayerMask layer;

    //內部欄位
    private float legX;
    private float legY;
    private bool isLerp;
    private Vector2 target;
    private float shmTime;

    //公共欄位
   [HideInInspector] public bool Active;

    private void Update()
   {
        //腿的弧度
        float x_adj = legRoot.position.x - legX;
        float y_opp = legRoot.position.y - legY;
        float legRad = Mathf.Atan2(y_opp, x_adj);
        //腳的弧度
        float tempX = legRoot.position.x - Mathf.Cos(legRad) * length;
        float tempY = legRoot.position.y - Mathf.Sin(legRad) * length;
       x_adj = tempX - IK_Target.position.x;
       y_opp = tempY - IK_Target.position.y;
        float footRad = Mathf.Atan2(y_opp, x_adj);
        //腿的位置
       legX = IK_Target.position.x + Mathf.Cos(footRad) * length;
       legY = IK_Target.position.y + Mathf.Sin(footRad) * length;
        //最終定位
       leg.SetPositionAndRotation( new Vector2(legX, legY), Quaternion.Euler( new Vector3( 0, 0, legRad * Mathf.Rad2Deg)));
       foot.SetPositionAndRotation( new Vector2(IK_Target.position.x, IK_Target.position.y), Quaternion.Euler( new Vector3( 0, 0, footRad * Mathf.Rad2Deg)));

        //射線,尋找蜘蛛的下一次落腳點
       RaycastHit2D hit = Physics2D.Raycast(rayOrigin.position, -rayOrigin.up, 5, layer);
        if (hit)
       {
           target = hit.point;
           Debug.DrawRay(rayOrigin.position, rayOrigin.up * (hit.point - (Vector2)rayOrigin.position) - new Vector2( 0, 0.1f), Color.red, 0.05f);
       }

        //根據條件來控制蜘蛛的步伐
        if (Mathf.Abs(target.x - IK_Target.position.x) > 0.7f) { isLerp = true; }
        if (isLerp && Active)
       {
           IK_Target.position = Vector2.MoveTowards(IK_Target.position, target + new Vector2( 0.1f, 0), Time.deltaTime * 3);
           shmTime += Time.deltaTime;
           IK_Target.position += new Vector3( 0, Mathf.Cos(shmTime) * 1.5f) * Time.deltaTime; //簡諧運動做為蜘蛛抬腿運動的數學模型
       }
        if (Vector2.Distance(IK_Target.position, target + new Vector2( 0.1f, 0)) < 0.1f)
       {
           isLerp = false;
           shmTime = 0;
            //協調蜘蛛的另一支腿
           Active = false;
           otherLeg.Active = true;
       }
   }
}


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70009264/viewspace-2897080/,如需轉載,請註明出處,否則將追究法律責任。

相關文章