我如何让敌人的AI面对玩家

时间:2019-04-27 18:47:07

标签: c# unity3d

我正在尝试找到一种方法来使敌人向玩家旋转。目前,它跟随/停止并射击玩家。还没有弄清楚如何使其旋转。如果可以的话请帮忙。我在这里尝试了几种不同的方法,但是它们似乎没有用。

我尝试使用isFacingRight创建Flip函数。

private bool facingRight;
public float speed;
public float stoppingDistance;
public float retreatDistance;
private Animator enemyAnimation;
private bool isDead;
private float direction;
public static bool enemyShoot = false;

private float timeBtwShots;
public float startTimeBtwShots;

public GameObject projectile;
private Transform player;

[SerializeField]
private Stat health;

private void Awake()
{
    health.Initialize();
}


// Start is called before the first frame update
void Start()
{
    //Player tag
    player = GameObject.FindGameObjectWithTag("Player").transform;

    enemyAnimation = GetComponent<Animator>();


    isDead = false;
}

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

    if (Vector2.Distance(transform.position, player.position) > stoppingDistance)
    {
        transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
        timeBtwShots = startTimeBtwShots;


  if(player.position.x > transform.position.x && !facingRight) //if the target is to the right of enemy and the enemy is not facing right
    Flip();
 if(player.position.x < transform.position.x && facingRight)
    Flip();
    }
   else if (Vector2.Distance(transform.position,player.position) <= stoppingDistance && Vector2.Distance(transform.position,player.position)>retreatDistance)
    {
        transform.position = this.transform.position;


    }
    else if (Vector2.Distance(transform.position, player.position) > retreatDistance)
    {
        transform.localScale = new Vector2(-1f, 1f);
        transform.position = Vector2.MoveTowards(transform.position, player.position, -speed * Time.deltaTime);
        timeBtwShots = 100;
        enemyAnimation.SetTrigger("Attack");
    }

    if (enemyShoot == true)
    {
        //Stops animation Loop
        enemyShoot = false;
        enemyAnimation.SetTrigger("Shoot");


    }

    if (timeBtwShots <= 0)
    {
        Instantiate(projectile, transform.position, Quaternion.identity);
        timeBtwShots = startTimeBtwShots;
    }
    else
    {
        timeBtwShots -= Time.deltaTime;
    }


    if (health.CurrentVal == 0)
    {

        timeBtwShots = 100;
        FindObjectOfType<SoundsScript>().Play("SaibaDeath");
        isDead = true;
        enemyAnimation.SetBool("Dead", isDead);
        Destroy(gameObject, 2f);
    }
}

private void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "ProjectileEnem")
    {
        health.CurrentVal -= 50;
        enemyAnimation.SetTrigger("Hurt");
    }

}

//Disables enemeies when off screen
private void OnBecameInvisible()
{
    GetComponent <Enemy> ().enabled = false;
}
//Re enables enemies once they are visible
private void OnBecameVisible()
{
    GetComponent<Enemy>().enabled = true;
}

void Flip(){
      Vector3 scale = transform.localScale;
      scale.x *= -1;
      transform.localScale = scale;
      facingRight = !facingRight;
}

}

1 个答案:

答案 0 :(得分:0)

尝试一下:

spriteRenderer.flipX = transform.position.x < player.position.x;

SpriteRenderer包含flipXflipY属性,您无需创建自己的Flip()函数。

您需要引用GameObject上的SpriteRenderer

上面的代码假设您的精灵在默认情况下仍面朝左。如果您的精灵正常面对,请将<更改为>