2Dプラットフォーム・ロジック・モンスター

前後に2Dパトロール

ピット遭遇は自動的にオンになります

旅行時間の設定可能な単一方向、時間のステアリングを待っています

使用してシステムを。
使用にSystem.Collectionsを。
使用してSystem.Collections.Genericを。
使用してSystem.Threading.Tasksを。
使用してUnityEngineを。
使用してUnityEngine.Serializationを。

[RequireComponent(typeof演算(Rigidbody2D))]
 パブリック クラスAI2dPatrol:MonoBehaviour 
{ 
    公共Rigidbody2D body2D。
    国民は rayPointを変換します。// 射线发射点
    公共 フロート rayLength = 3 ; // 射线长度
    公共 フロートスピード= 4 ; //モンスタースピード
    公共 int型の turnWaitTime; // ステアリング時間
    公共 フロートワンタイム; // ために一つの方向に行く
    公共 BOOL = AutoTurn trueに ; // 場合には自動操舵ピット

    プライベート フロート nowtime = 0 ;
     プライベート int型 MOVEDIR = 1 ; // 方向1に移動左
    公共のVector3 curSpeed、
     プライベート BOOL isPause = falseに

    プライベート ボイドFixedUpdate()
    { 
        IF (isPause)
        {
            返します
        } 
        
        移動()。
        もし(ワンタイム> 0 
        { 
            場合(nowTime> = ワンタイム)
            { 
                nowTime = 0 
                TurnFace(); 
            } 
            nowTime + = Time.deltaTime。
        } 
        であれば(autoTurn)
        { 
            RaycastHit2D groundInfo = Physics2D.Raycast(rayPoint.position、Vector2.down、rayLength)。
            もし(groundInfo.collider ==  
            {
                TurnFace();
            } 
            // シーン模式下显示射线 
            Debug.DrawRay(rayPoint.position、Vector2.down * rayLength、Color.blue)。
        } 
    } 

    // 转向
    非同期 ボイドTurnFace()
    { 
        isPause = 待つ Task.Delay(turnWaitTime * 1000 ); 
        MOVEDIR = - MOVEDIR。
        VaRの起源= transform.localScale。
        transform.localScale = 新しいのVector3( -  origin.x、origin.y、origin.z)。
        isPause = ; 
    } 
    
    //移动
    ボイド移動()
    { 
        // transform.Translate(Vector2.left倍速* Time.deltaTime * MOVEDIR)。
        Vector3 targetVelocity = 新しいベクトル2(-moveDir * スピード、body2D.velocity.y)。
        body2D.velocity = Vector3.SmoothDamp(body2D.velocity、targetVelocity、REF curSpeed、0.1F )。
    } 
}

 

おすすめ

転載: www.cnblogs.com/sanyejun/p/11442445.html