Unity:线形和球形检测

Destroy线性操作:伤害门。

生成游戏对象

publicclassCreateCubes : MonoBehaviour

    {

        // Use this for initialization

        void Start()

        {

        }

        float timer = 0.0f;

        // Update is called once per frame

        void Update()

        {

            timer += Time.deltaTime;

            if (timer > 3)

            {

                timer -= 3;

                GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);

                obj.transform.position = newVector3(0, 0.5f, 0);

                obj.AddComponent<MoveDoor>();

            }

        }

}

对象移动

publicclassMoveDoor : MonoBehaviour

{

    // Use this for initialization

    void Start()

    {

    }

    // Update is called once per frame

    void Update()

    {

       gameObject.transform.Translate(gameObject.transform.forward * Time.deltaTime);

    }

}

判断是否受到伤害

publicclassPhysicsDemo : MonoBehaviour

{

    publicGameObject startObj;

    publicGameObject endObj;

    // Use this for initialization

    void Start()

    {

    }

    // Update is called once per frame

    void Update()

    {

        RaycastHit hit;

        if (Physics.Linecast(startObj.transform.position,endObj.transform.position, out hit))

        {

            if (!hit.collider.gameObject.CompareTag("Door"))

            {

               Destroy(hit.collider.gameObject);

            }

        }

    }

}

球形检测:吸铁石效果。

玩家移动

publicclassCoinMove : MonoBehaviour

{

    publicTransform target;

    publicbool isMove = false;

    // Use this for initialization

    void Start()

    {

 

    }

    // Update is called once per frame

    void Update()

    {

        if (isMove)

        {

            transform.position = Vector3.Lerp(transform.position, target.position,0.2f);

        }

    }

    void OnTriggerEnter(Collider other)

    {

        if (other.gameObject.CompareTag("Player"))

        {

            Destroy(gameObject);

        }

    }

}

金币移动和消失的条件

publicclassCubeMove : MonoBehaviour

{

    bool isMagnet = false;

    publicfloat speed;

    // Use this for initialization

    void Start()

    {

    }

    // Update is called once per frame

    void Update()

    {

        transform.Translate(transform.forward *speed * Time.deltaTime);

        if (isMagnet)

        {

            Collider[] cols = Physics.OverlapSphere(transform.position, 10);

            foreach (var item in cols)

            {

                if (item.gameObject.CompareTag("Coin"))

                {

                   item.GetComponent().isMove = true;

                }

            }

        }

}

void OnTriggerEnter(Collider other)

    {

        if (other.gameObject.tag == "Magnet")

        {

            Destroy(other.gameObject);

            isMagnet = true;

        }

}

Unity:线形和球形检测_第1张图片

Unity:线形和球形检测_第2张图片

你可能感兴趣的:(Unity,c#,脚本,unity,对象)