示例#1
0
        // Update is called once per frame
        void Update()
        {
            // 提示玩家按键
            if (toNotify)
            {
                Vector3    point = new Vector3(Room1.Util.GetCamera().pixelWidth / 2, Room1.Util.GetCamera().pixelHeight / 2, 0); //屏幕中心
                Ray        ray   = Room1.Util.GetCamera().ScreenPointToRay(point);                                                //在摄像机所在位置创建射线
                RaycastHit hit;                                                                                                   //射线交叉信息的包装
                                                                                                                                  //Raycast给引用的变量填充信息
                if (Physics.Raycast(ray, out hit))                                                                                //out确保在函数内外是同一个变量
                {
                    //hit.point:射线击中的坐标
                    GameObject hitObject = hit.transform.gameObject;//获取射中的对象
                    if (Vector3.Distance(this.transform.position, hitObject.transform.position) <= distanceQuota)
                    {
                        HintableObject target = hitObject.GetComponent <HintableObject>();
                        if (target != null && !state)
                        {
                            toDisplayHint = true;
                            hintToDisplay = "^w按^yF^w与物品交互";
                            usingGrammar  = true;
                            hintFontSize  = 12;
                            goto secondIf;
                        }
                    }
                    else
                    {
                        toDisplayHint = false;
                    }
                }
            }
secondIf:
            // 处理玩家的物品交互按键
            if (Input.GetKeyDown(KeyCode.F) && SceneController.state == SceneController.ElevatorState.Initing)
            {
                Vector3    point = new Vector3(Room1.Util.GetCamera().pixelWidth / 2, Room1.Util.GetCamera().pixelHeight / 2, 0); //屏幕中心
                Ray        ray   = Room1.Util.GetCamera().ScreenPointToRay(point);                                                //在摄像机所在位置创建射线
                RaycastHit hit;                                                                                                   //射线交叉信息的包装
                                                                                                                                  //Raycast给引用的变量填充信息
                if (Physics.Raycast(ray, out hit))                                                                                //out确保在函数内外是同一个变量
                {
                    //hit.point:射线击中的坐标
                    GameObject hitObject = hit.transform.gameObject;//获取射中的对象
                    Debug.Log("物体" + hitObject.name);
                    Debug.Log("距离: " + Vector3.Distance(this.transform.position, hitObject.transform.position));
                    if (Vector3.Distance(this.transform.position, hitObject.transform.position) > distanceQuota)
                    {
                        return;
                    }
                    hitObject.SendMessage("Operate", SendMessageOptions.DontRequireReceiver);
                }
            }
        }
        // Update is called once per frame
        void Update()
        {
            // 提示玩家按键
            if (toNotify)
            {
                Vector3    point = new Vector3(Util.GetCamera().pixelWidth / 2, Util.GetCamera().pixelHeight / 2, 0); //屏幕中心
                Ray        ray   = Util.GetCamera().ScreenPointToRay(point);                                          //在摄像机所在位置创建射线
                RaycastHit hit;                                                                                       //射线交叉信息的包装
                                                                                                                      //Raycast给引用的变量填充信息
                if (Physics.Raycast(ray, out hit))                                                                    //out确保在函数内外是同一个变量
                {
                    //hit.point:射线击中的坐标
                    GameObject hitObject = hit.transform.gameObject;//获取射中的对象
                    if (Vector3.Distance(ray.origin, hitObject.transform.position) <= distanceQuota)
                    {
                        HintableObject target = hitObject.GetComponent <HintableObject>();
                        if (target != null)
                        {
                            toDisplayHint = true;
                            hintToDisplay = target.WhatToHint;
                            usingGrammar  = target.UsingGrammar;
                            hintFontSize  = target.FontSize;
                            goto secondIf;
                        }
                    }
                    else
                    {
                        toDisplayHint = false;
                    }
                }
            }
secondIf:
            // 处理玩家的物品交互按键
            if (Input.GetKeyDown(KeyCode.F))
            {
                Vector3    point = new Vector3(Util.GetCamera().pixelWidth / 2, Util.GetCamera().pixelHeight / 2, 0); //屏幕中心
                Ray        ray   = Util.GetCamera().ScreenPointToRay(point);                                          //在摄像机所在位置创建射线
                RaycastHit hit;                                                                                       //射线交叉信息的包装
                                                                                                                      //Raycast给引用的变量填充信息
                if (Physics.Raycast(ray, out hit))                                                                    //out确保在函数内外是同一个变量
                {
                    //hit.point:射线击中的坐标
                    GameObject hitObject = hit.transform.gameObject;//获取射中的对象
                    Debug.Log("物体" + hitObject.name);
                    //Debug.DrawLine(ray.origin, hitObject.transform.position, Color.red);
                    Debug.Log("距离: " + Vector3.Distance(ray.origin, hitObject.transform.position));
                    if (Vector3.Distance(ray.origin, hitObject.transform.position) > distanceQuota)
                    {
                        return;
                    }
                    KeyScript target =
                        hitObject.GetComponent <KeyScript>();
                    if (target != null)   //检查对象上是否有KeyScript组件
                    {
                        Messenger <int> .Broadcast(GameEvent.KEY_GOT, target.ThisId);

                        return;
                    }
                    DoorScript target1 =
                        hitObject.GetComponent <DoorScript>();
                    if (target1 != null)
                    {
                        Messenger <int> .Broadcast(GameEvent.DOOR_OPEN, target1.ThisId);

                        return;
                    }
                    if (hitObject.CompareTag("Corpse"))
                    {
                        Messenger.Broadcast(GameEvent.CROPSE_TRY);
                        return;
                    }
                    if (hitObject.CompareTag("Elevator"))
                    {
                        Messenger.Broadcast(GameEvent.ELEVATOR_OPEN);
                        return;
                    }
                    //InteractiveThing target2 =
                    //    hitObject.GetComponent<InteractiveThing>();
                    //if (target2 != null)
                    //{
                    //    Messenger.Broadcast(GameEvent.CROPSE_TRY);
                    //    return;
                    //}
                }
            }
        }