如何让敌方 AI 面对玩家



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

我尝试使用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;
}

}

试试这个:

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

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

您需要对游戏对象上的SpriteRenderer的引用。

上面的代码假设你的精灵面子默认左边。如果你的精灵正常朝向右,请将<更改为 >

最新更新