Exemple #1
0
 // Update is called once per frame
 void Update()
 {
     if (m_Dead)
     {
         return;
     }
     Collider2D[] frontHits = Physics2D.OverlapPointAll(m_FrontCheck.position, m_LayerMask);
     if (frontHits.Length > 0)
     {
         m_Wander.Flip();
     }
 }
    // 实例化预设对象
    private void Generate()
    {
        // 随机选择要实例化的预设的下标
        int index = Random.Range(0, Prefabs.Length);

        // 确定生成位置的X坐标
        float x = transform.position.x;

        if (RandomGeneratePositionX)
        {
            x = Random.Range(MinGeneratePositionX, MaxGeneratePositionX);
        }

        // 确定生成位置的Y坐标
        float y = transform.position.y;

        if (RandomGeneratePositionY)
        {
            y = Random.Range(MinGeneratePositionY, MaxGeneratePositionY);
        }

        // 更新位置
        transform.position = new Vector3(x, y, transform.position.z);

        // 实例化预设对象
        GameObject prefab = Instantiate(Prefabs[index], transform.position, Quaternion.identity);

        // 播放粒子特效
        if (m_Particle != null)
        {
            m_Particle.Play();
        }

        // 不需要考虑朝向
        if (PrefabOrientation == Orientation.None)
        {
            return;
        }

        if (PrefabOrientation == Orientation.Left)
        {
            Wander wander = prefab.GetComponent <Wander>();
            if (wander.FacingRight)
            {
                wander.Flip();
            }
            return;
        }

        if (PrefabOrientation == Orientation.Right)
        {
            Wander wander = prefab.GetComponent <Wander>();
            if (!wander.FacingRight)
            {
                wander.Flip();
            }
            return;
        }

        if (PrefabOrientation == Orientation.Random)
        {
            Wander wander = prefab.GetComponent <Wander>();
            // 有一半的概率进行翻转
            if (Random.value <= 0.5)
            {
                wander.Flip();
            }
            return;
        }
    }