0

我在让机制在我的游戏中正常运行时遇到了一些麻烦。基本上我有两个区域: Collidersight 和 Colliderfatal,由连接到由大炮发射的炮弹上的对撞机球体定义。本质上,我希望在 Collidersight 区域内的任何对象只要在该特定区域内就可以显示出来,如果它们在该区域之外,它们应该恢复为不可见。如果一个对象与 Colliderfatal 发生碰撞,那么即使在外壳被破坏之后,该对象也应该永久显示。我曾尝试单独初始化游戏对象,但这似乎非常令人困惑并且不适用于这种情况。游戏对象已经存在于场景中,我没有在其中实例化它们。有人告诉我,

这是代码。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class zone : MonoBehaviour {

 public LayerMask m_SightLayers;
public LayerMask  s_FatalityLayers;
    public Vector3 m_Position;
    public float m_Radius;
    public float m_Force;
    public float s_Radius;
    public Vector3 s_Position;
    public Invisible1 revealenemy1;
    public Invisible1 enemykill1; 

    public float s_Force;

    public InvisibleKill makeDead;

    public GameObject EnemyCube1; 

    //public GameObject enemydead; 
    bool hasExploded;
    public bool inZone;
    public bool inKillzone;
    //public float removeTime = 5.0f;

    bool hasBeenhit;

    void Awake(){

    GameObject[] objects = GameObject.FindGameObjectsWithTag("Enemy");  

    }


    void start (){

    makeDead = (InvisibleKill) EnemyCube1.GetComponent (typeof(InvisibleKill));

    revealenemy1 = (Invisible1) EnemyCube1.GetComponent(typeof(Invisible1));    

    }

    void OnCollisionEnter(Collision explode){

        if(explode.gameObject.name == "Terrain" || explode.gameObject.name == "EnemyCube" || explode.gameObject.name == "EnemyCube1" ){
            hasExploded = true;
        }

    }
    void FixedUpdate ()
    {
        if (hasExploded == true){

        Collider[] collidersight;
        Collider[] colliderfatal;
        Rigidbody rigidbody;


        collidersight = Physics.OverlapSphere (transform.position + m_Position, m_Radius, m_SightLayers);
        foreach (Collider collider in collidersight)
        {
            if(collider.tag == "Enemy"){

                Debug.Log("stuff");


                }


            rigidbody = (Rigidbody) collider.gameObject.GetComponent (typeof (Rigidbody));
            if (rigidbody == null)
            {
                    continue;
                    }

            if(rigidbody.gameObject.tag == "Enemy"){
                inZone = true;




            if(inZone == true){

            revealenemy1.Reveal1(); 
          //  revealenemy2.Reveal2();
            //revealenemy3.Reveal3();


                Debug.Log ("hit");

                    hasBeenhit = true;


                    if(hasBeenhit == false){
                        hasBeenhit = false;
                    //  revealenemy1.Hidden();
                    ////    revealenemy2.Hidden2();
                //      revealenemy3.Hidden3();

                    }

            }else{

                }
                }

            }   
        //Debug.Log (hasExploded);
        colliderfatal = Physics.OverlapSphere (transform.position + s_Position,s_Radius,s_FatalityLayers);
        foreach (Collider collider in colliderfatal)
        {

            inKillzone = true; 
        rigidbody = (Rigidbody) collider.gameObject.GetComponent (typeof (Rigidbody));
            if (rigidbody ==null)
            {
            continue;   


            }
            rigidbody.AddExplosionForce (s_Force * -1, transform.position + s_Position,s_Radius);
            Debug.Log("hasbeenkilled");

                }


        }

        if(hasBeenhit == false){

            revealenemy1.Hidden();
            //revealenemy2.Hidden2();
            //revealenemy3.Hidden3 ();
        hasBeenhit = false;
        }

        if(inKillzone == true){

            //EnemyCube1.GetComponentInChildren (typeof(Invisible1));
            hasBeenhit = true;
            //revealenemy.renderer.enabled = true;
        //  makeDead.isdead = true; 
                //(typeof(Invisible1));

        }

        if(makeDead.isdead == true){

            revealenemy1.Reveal1();

        }


    }

  void OnDrawGizmosSelected ()  {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere (transform.position + m_Position, m_Radius);
        //Gizmos.DrawWireSphere (transform.position + s_Position, s_Radius);
    }

    void OnDrawGizmos()
    {

        Gizmos.color = Color.blue;

        Gizmos.DrawWireSphere (transform.position + s_Position, s_Radius);

    }
}
4

1 回答 1

0

为什么不简单地在对象渲染器进入区域时激活/停用它们?您规定进入“致命”区域的对象是永久可见的,我认为这是指即使它们离开该区域也是可见的。

要放置在要更改其可见性的任何对象上的可见性类:

public class Visibility : MonoBehaviour {
    public bool LockVisibility = false;

    public void SetVisible(bool state){
        if(LockVisibility)return;
        renderer.enabled = state;
    }
}

和碰撞检测类。

public class CollisionRender : MonoBehaviour {
//example collision types
public enum CollisionTypes { Fatal,InSight };
public CollisionTypes CollisionType = CollisionTypes.InSight;

//turn renderer on.  
void OnTriggerEnter(Collider other) {
    Visibility vis = other.gameObject.GetComponent<Visibility>();

    if(CollisionType == CollisionTypes.InSight){
        vis.SetVisible(true);
    }else if(CollisionType == CollisionTypes.Fatal){

        //if in 'fatal' zone, make visible and lock visibility.
        vis.SetVisible(true);
        vis.LockVisibility = true;
    }
}
void OnTriggerExit(Collider other) {
    Visibility vis = other.gameObject.GetComponent<Visibility>();
    vis.SetVisible(false);
}
}

我正在使用触发器,因此您的对撞机需要检查“IsTrigger”才能工作。

于 2013-03-06T21:13:48.337 回答