unity 企鹅砸小猪 笔记2

2.脚本

  1)弹弓脚本

    1-input.GetButton函数测试

      说明

        input.GetButton(“...”)是根据按钮名称返回true当对应的虚拟按钮被按住时

        input.GetButton(“Fire1”):判断是否点击鼠标或触摸屏

      实测:

void Update () {

        bool bIsButtonDown = Input.GetButton("Fire1");

        if (!Clicked){

            if (bIsButtonDown){

                print("点击屏幕");

             }

        }

}

      效果:

       

      结果:

        函数无误

  2-collider.Raycast()函数测试

    说明:

       collider.Raycast():返回布尔(bool)值,当光线和任何碰撞器相交时,返回true,否则为false。也就是说,当光线碰触到任何碰撞器时返回真,否则返回假。

       具体说明:http://www.ceeger.com/Script/Collider/Collider.Raycast.html

    实测

     

      错误:UnityEngine.Component”不包含“Raycast”的定义,并且找不到可接受类型为“UnityEngine.Component”的第一个参数的扩展方法“Raycast(是否缺少 using 指令或程序集引用?) D:\Unity\unity01\qiezaxiaozhu\Assets\Scripts\DanGongS.cs

    结果:

      该函数无法在vs2010中运行,推测为unity 5.x移除该函数。寻找替补函数为:Physics.Raycast

 

  3-Physics.Raycast()函数测试

    说明:

      Physics.Raycast():光线投射,当光线投射与任何碰撞器交叉时为真,否则为假。

      具体说明:http://www.ceeger.com/Script/Physics/Physics.Raycast.html

    实测1

      1.当光线投射与任何碰撞器交叉时为真,否则为假。

void Update () {

        bool bIsButtonDown = Input.GetButton("Fire1");

        if (!Clicked)

        {

            if (bIsButtonDown){

                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

                RaycastHit rch;

                if (Physics.Raycast(ray, out rch, 1000.0f)){

                    print("碰撞");

                }

            }

}

}

    结果1

      当光线投射与任何碰撞器交叉时为真,否则为假。成立

 

    实测2

      由于本项目要锁定点击弹弓为才发生反应,要求与实测1不符,故采用他的重载函数。

      1.为弹弓建立新的Layer层,(Edit - Project Settings - Tags ands Layers - Layers - User Layer 8 - “fasheqi”

      2.修改弹弓预设体的Layer层,

         

void Update () {

        bool bIsButtonDown = Input.GetButton("Fire1");

        if (!Clicked)

        {

            if (bIsButtonDown){

                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

                RaycastHit rch;

                if (Physics.Raycast(ray, out rch, 1000.0f, 1 << 8)){

                    print("碰撞");

                }

            }

}

}

    结果2

      光线投射只与fasheqi”层的碰撞器交叉时为真,其他均为假。

 

    参考网页:

      [Unity3D]射线碰撞检测+LayerMask的使用:http://blog.sina.com.cn/s/blog_947a2cdd0102uz1k.html

      【风宇冲】Unity3D教程宝典之Raycasthttp://www.xuebuyuan.com/2074615.html

posted @ 2016-08-30 22:33  黑眼眶  阅读(361)  评论(0编辑  收藏  举报