Unity 2D旋转AI敌人看玩家

时间:2018-11-26 20:25:19

标签: c# unity3d 2d

我正在制作左右两侧的2D游戏,因此玩家只能向左或向右移动或跳跃。我把AI作为敌人。 AI运行正常。他可以去杀死他。

问题是敌人去杀死玩家时无法旋转或面对玩家。我希望AI看玩家。当玩家在敌人的左边时,敌人应该向左旋转看玩家。我搜索了许多网站,但没有得到正确的解决方案。这是我的敌人脚本:

public class enemy : MonoBehaviour
{
    public float speed;
    private Animator animvar;
    private Transform target;
    public GameObject effect;
    public float distance;
    private bool movingRight = true;
    public Transform groundedDetection;
    public float moveInput;
    public bool facingRight = true;

    void Start()
    {
        animvar = GetComponent<Animator>();
        target = GameObject.FindGameObjectWithTag("Player").transform;
    }

    // Update is called once per frame
    void Update()
    {
        if (Vector3.Distance(target.position, transform.position) < 20)
        {
            transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
        }
        else
        {
            transform.Translate(Vector2.right * speed * Time.deltaTime);
        }
        RaycastHit2D groundInfo = Physics2D.Raycast(groundedDetection.position, Vector2.down, distance);
    }

    void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.tag.Equals("danger"))
        {
            Instantiate(effect, transform.position, Quaternion.identity);
            Destroy(gameObject);
        }
        if (col.gameObject.tag.Equals("Player"))
        {
            Instantiate(effect, transform.position, Quaternion.identity);
            Destroy(gameObject);
        }
    }
}

使用Unity3D,游戏是向左,向右2D跳跃的游戏。

2 个答案:

答案 0 :(得分:1)

You need a function that would flip your sprite, you can do that by changing the scale of the transform, and keep a boolean to check where it's facing bool facingRight;, so something like this

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

and in your Update check if it needs to be flipped or not

if (Vector3.Distance(target.position,transform.position)<20)
    {

     transform.position=Vector2.MoveTowards(transform.position, target.position,speed*Time.deltaTime);
     if(target.position.x > transform.position.x && !facingRight) //if the target is to the right of enemy and the enemy is not facing right
        Flip();
     if(target.position.x < transform.position.x && facingRight)
        Flip();
   }

答案 1 :(得分:0)

非常简单,只需获得玩家x位置并与敌人的x位置进行比较,然后相应地翻转敌人的精灵即可即可。

这是我的敌人脚本中的Update()方法。这可以处理敌人所面临的移动以及精灵所面对的改变方向:

if (moveRight )
        {
            transform.Translate(2 * Time.deltaTime * moveSpeed, 0, 0);
            transform.localScale = new Vector2(6, 6); //6,6 is just a size that suits my sprite
            
        }

        else if (!moveRight)
        {
            transform.Translate(-2 * Time.deltaTime * moveSpeed, 0, 0);
            transform.localScale = new Vector2(-6, 6);
            
        }

这是我的敌人Attack()方法,我将Player X位置与敌人X位置进行比较。如果玩家X少(位于敌人的左侧),我会将敌人的精灵翻转为负数

            if (transform.position.x > Player.position.x)
            {
                transform.localScale = new Vector2(-6, 6);
            }
            else if (transform.position.x < Player.position.x)
            {
                transform.localScale = new Vector2(6, 6);
            }
相关问题