使用Unity:编写脚本敌人AI-关注玩家

时间:2019-11-24 21:38:07

标签: c# unity3d

我试图让我的敌人在射程内跟随我的玩家,否则敌人在徘徊。我对C-Sharp很陌生,因此将其他教程代码拼凑在一起。

目前,敌人在我设置的触发盒对撞机之间来回徘徊。当玩家在射程内时,敌人会将状态切换为“跟随”,但是只有当玩家在敌人的左侧时,敌人才会向玩家移动,如果我在敌人的右侧,则卡住直到我超出范围,则恢复“徘徊”。跟随时,敌人也不会翻转以面对敌人。

任何帮助将不胜感激。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum EnemyState
   {
      Wander,   
      Follow,
      Die,
   };


public class EnemyController : MonoBehaviour
   {
     GameObject player;    
     public EnemyState currState = EnemyState.Wander; 
     public Transform target;
     Rigidbody2D myRigidbody;

     public float range = 2f;    
     public float moveSpeed = 2f;



void Start()
{
    player = GameObject.FindGameObjectWithTag("Player");
    myRigidbody = GetComponent<Rigidbody2D>();
    target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
}

void Update()
{

    switch (currState)
    {
        case (EnemyState.Wander):
            Wander();
            break;
        case (EnemyState.Follow):
            Follow();
            break;
        case (EnemyState.Die):
           // Die();
            break;
    }    

    if(IsPlayerInRange(range) && currState != EnemyState.Die)
    {
        currState = EnemyState.Follow;
    }
    else if(!IsPlayerInRange(range)&& currState != EnemyState.Die)
    {
        currState = EnemyState.Wander;
    }
}

private bool IsPlayerInRange(float range)
{
    return Vector3.Distance(transform.position, player.transform.position) <= range;
}

bool isFacingRight()
{
    return transform.localScale.x > 0;
}

void Wander()
{
    {
        if (isFacingRight())
        {
            myRigidbody.velocity = new Vector2(moveSpeed, 0f);
        }
        else
        {
            myRigidbody.velocity = new Vector2(-moveSpeed, 0f);
        }
    }

}

void OnTriggerExit2D(Collider2D collision) //this is to flip the sprite when it reaches the end of its path - a box 2d collider trigger
{
    transform.localScale = new Vector2(-(Mathf.Sign(myRigidbody.velocity.x)), 1f);

}

void Follow()
{
    if (isFacingRight())
    {
        myRigidbody.velocity = new Vector2(-moveSpeed, 0f);
        transform.position = Vector2.MoveTowards(transform.position, new Vector2(target.position.x, transform.position.y), moveSpeed * Time.deltaTime);

    }
   /* else //this seems to have no effect on the code
    {
        myRigidbody.velocity = new Vector2(moveSpeed, 0f);
        transform.position = Vector2.MoveTowards(transform.position, new Vector2(target.position.x, transform.position.y), -moveSpeed * Time.deltaTime);
    }*/

}

}

1 个答案:

答案 0 :(得分:0)

好的,我发现了自己的问题,这是工作脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum EnemyState
{
  Wander,
  Follow,
  Die,
};

public class EnemyController : MonoBehaviour
{
  GameObject player;
  public EnemyState currState = EnemyState.Wander;

  public Transform target;

  Rigidbody2D myRigidbody;

  public float range = 2f;
  public float moveSpeed = 2f;






// Start is called before the first frame update
void Start()
{
    player = GameObject.FindGameObjectWithTag("Player");
    myRigidbody = GetComponent<Rigidbody2D>();
    target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();



}

// Update is called once per frame
void Update()
{

    switch (currState)
    {
        case (EnemyState.Wander):
            Wander();
            break;
        case (EnemyState.Follow):
            Follow();
            break;
        case (EnemyState.Die):
           // Die();
            break;
    }    

    if(IsPlayerInRange(range) && currState != EnemyState.Die)
    {
        currState = EnemyState.Follow;
    }
    else if(!IsPlayerInRange(range)&& currState != EnemyState.Die)
    {
        currState = EnemyState.Wander;
    }
}

private bool IsPlayerInRange(float range)
{
    return Vector3.Distance(transform.position, player.transform.position) <= range;
}

bool isFacingRight()
{
    return transform.localScale.x > 0;
}

void Wander()
{
    {
        if (isFacingRight())
        {
            myRigidbody.velocity = new Vector2(moveSpeed, 0f);
        }
        else
        {
            myRigidbody.velocity = new Vector2(-moveSpeed, 0f);
        }
    }


}

void OnTriggerExit2D(Collider2D collision)
{
    transform.localScale = new Vector2(-(Mathf.Sign(myRigidbody.velocity.x)), 1f);

}


void Follow()
{

    if (transform.position.x > target.position.x)
    {
        //target is left
        transform.localScale = new Vector2(-1, 1);
        myRigidbody.velocity = new Vector2(-moveSpeed, 0f);
        transform.position = Vector2.MoveTowards(transform.position, new Vector2(target.position.x, transform.position.y), moveSpeed * Time.deltaTime);
    }
    else if (transform.position.x < target.position.x)
    {
        //target is right
        transform.localScale = new Vector2(1, 1);
        myRigidbody.velocity = new Vector2(moveSpeed, 0f);
        transform.position = Vector2.MoveTowards(transform.position, new Vector2(target.position.x, transform.position.y), moveSpeed * Time.deltaTime);
    }

}

}