Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity中游戏对象的状态机不';我好像不工作。而不是从;错开;至;闲置;,它一直在移动_C#_Unity3d - Fatal编程技术网

C# Unity中游戏对象的状态机不';我好像不工作。而不是从;错开;至;闲置;,它一直在移动

C# Unity中游戏对象的状态机不';我好像不工作。而不是从;错开;至;闲置;,它一直在移动,c#,unity3d,C#,Unity3d,因此,我目前正在开发我在Unity中的第一款游戏(继Taft先生在YouTube上的一个系列游戏之后,该系列游戏名为“使用Unity和C#制作一个类似于Zelda的游戏”,刚刚完成了第19个视频) 我目前有一个“原木”敌人,正在开发击退功能。我在本系列中遵循的方法是创建一个状态机,将日志的状态更改为“交错”几秒钟,然后将其返回到“空闲”,这样它就可以将自身更改为“行走” 然而,每当我撞到圆木时,它都会卡在交错状态,不停地漂移,直到撞到对撞机为止(注意,即使撞到对撞机,它也不会变回空闲/行走)。如

因此,我目前正在开发我在Unity中的第一款游戏(继Taft先生在YouTube上的一个系列游戏之后,该系列游戏名为“使用Unity和C#制作一个类似于Zelda的游戏”,刚刚完成了第19个视频)

我目前有一个“原木”敌人,正在开发击退功能。我在本系列中遵循的方法是创建一个状态机,将日志的状态更改为“交错”几秒钟,然后将其返回到“空闲”,这样它就可以将自身更改为“行走”

然而,每当我撞到圆木时,它都会卡在交错状态,不停地漂移,直到撞到对撞机为止(注意,即使撞到对撞机,它也不会变回空闲/行走)。如果我能正确击中圆木,有时,玩家实际上也会朝相反的方向漂移

相关代码:

public class Log : Enemy {

    private Rigidbody2D myRigidBody;
    public Transform target;
    public float chaseRadius;
    public float attackRadius;
    public Transform homePosition;
    public Animator anim;

    // Use this for initialization
    void Start () {
        currentState = EnemyState.idle;
        myRigidBody = GetComponent<Rigidbody2D> ();
        anim = GetComponent<Animator> ();
        target = GameObject.FindWithTag ("Player").transform;

    }
    
    // FixedUpdate is called by physics.
    void FixedUpdate () {
        CheckDistance ();
    }

    //Log finds and walks towards Player.
    void CheckDistance(){
        if (Vector3.Distance (target.position, transform.position) <= chaseRadius && Vector3.Distance (target.position, transform.position) > attackRadius
&& currentState != EnemyState.stagger) 
        {
            Vector3 temp = Vector3.MoveTowards (transform.position, target.position, moveSpeed * Time.deltaTime);
            myRigidBody.MovePosition (temp);
            ChangeState (EnemyState.walk);
        } 
    }

    private void ChangeState(EnemyState newState){
        if (currentState != newState) 
        {
            currentState = newState;
        
        }
    }
}

我试图在Log.cs中的void CheckDistance之后添加一个else语句,但这导致日志出现抖动,并沿其路径跳回。我不知所措。任何帮助都将不胜感激

你确定击退不会一直触发,只会触发一次吗?@Zokka如果我将Knockback.cs附加到玩家身上,问题实际上就消失了,但是为了使它能够正确地与敌人的伤害箱一起工作,它需要附加到玩家的伤害箱上(上、下、左、右)。我同意@Zokka,击退脚本不会阻止为同一敌人多次运行协同程序。尝试添加额外的日志记录以查看到底发生了什么。当敌人!=null,并指向KnockCo的开始和结束。协同程序可能会多次触发,将currentState设置为交错。作为补充说明,创建一个enemyknockback脚本可能更有意义,该脚本执行触发器和KnockCo所执行的操作。如果敌人没有该脚本,则将该脚本添加到敌人。
public class Knockback : MonoBehaviour {

    public float thrust;
    public float knockTime;

    private void OnTriggerEnter2D(Collider2D other){

        if (other.gameObject.CompareTag ("breakable")) 
        {
            other.GetComponent<Pot>().Smash();
        }

        if(other.gameObject.CompareTag("enemy"))
        {
            Rigidbody2D enemy = other.GetComponent<Rigidbody2D>();
            if (enemy != null) 
            {
                enemy.GetComponent<Enemy> ().currentState = EnemyState.stagger;
                Vector2 difference = enemy.transform.position - transform.position;
                difference = difference.normalized * thrust;
                enemy.AddForce (difference, ForceMode2D.Impulse);
                StartCoroutine (KnockCo (enemy));
            }
        }
    }

    private IEnumerator KnockCo(Rigidbody2D enemy){
        if (enemy != null) 
        {
            yield return new WaitForSeconds (knockTime);
            enemy.velocity = Vector2.zero;
            enemy.GetComponent<Enemy>().currentState = EnemyState.idle;
        }
    }
}
public enum EnemyState{
    idle,
    walk,
    attack,
    stagger
}

public class Enemy : MonoBehaviour {

    public EnemyState currentState;
    public int enemyHealth;
    public string enemyName;
    public int baseAttack;
    public float moveSpeed;
}