コード例 #1
0
    void MouseOverUpdate()
    {
        ray      = Camera.main.ScreenPointToRay(Input.mousePosition);
        VecMouse = ray.origin;
        hit      = Physics2D.GetRayIntersection(ray, Mathf.Infinity);
        if (hit.collider != null)
        {
//			Debug.Log (hit.collider.name);
        }
    }
コード例 #2
0
    public static GameObject GetObjectAtPosition2D(Vector3 position)
    {
        var hit = Physics2D.GetRayIntersection(new Ray(new Vector3(position.x, position.y, -100), Vector3.forward), float.MaxValue);

        if (hit.collider)
        {
            return(hit.collider.gameObject);
        }
        return(null);
    }
コード例 #3
0
        public override void OnClick(Vector2 position, Player player)
        {
            RaycastHit2D hit = Physics2D.GetRayIntersection(player.GetCursorRay, 20f, LayerUtils.GetLayer(Layers.GRAB).Mask);

            if (hit && this.objects.Contains(hit.collider))
            {
                this.dragging = hit.collider;
                this.startPos = this.dragging.transform.position;
            }
        }
コード例 #4
0
    // Update is called once per frame
    void Update()
    {
        if (flag)
        {
            PurchaseItem();
        }

        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        hit = Physics2D.GetRayIntersection(ray, Mathf.Infinity);
    }
コード例 #5
0
    private void MoveCurrentPlaceableObjectToMouse()
    {
        // TODO: check if placing on ground
        RaycastHit2D hitInfo = Physics2D.GetRayIntersection(Camera.main.ScreenPointToRay(Input.mousePosition));

        if (hitInfo)
        {
            currentPlaceableObject.transform.position = hitInfo.point;
        }
    }
コード例 #6
0
        private RaycastHit2D CastRayFromMouseToWorld()
        {
            Ray ray = cameraVariable.Value.ScreenPointToRay(playerPosition.Value);

            return(Physics2D.GetRayIntersection(
                       ray,
                       Mathf.Abs(cameraVariable.Value.transform.position.z * 2),
                       LayerMask.GetMask("NPC")
                       ));
        }
コード例 #7
0
ファイル: ClickDetect.cs プロジェクト: YoriKv/TrainingJam
    private void ClickScreenPos(Vector3 screenPos)
    {
        Ray          ray = _cam.ScreenPointToRay(screenPos);
        RaycastHit2D hit = Physics2D.GetRayIntersection(ray, Mathf.Infinity);

        if (hit.collider != null)
        {
            hit.collider.SendMessage("Click");
        }
    }
コード例 #8
0
ファイル: TBUtil.cs プロジェクト: teambrookvale/SaboteurDiver
 public static bool IsTouchOnWater(TouchScreenPosition touchScreenPosition)
 {
     return
         (Physics2D.GetRayIntersection(
              new Ray(
                  (Vector3)touchScreenPosition.world - Vector3.forward,
                  Vector3.forward),
              Mathf.Infinity
              ).transform == null);
 }
コード例 #9
0
    void SelectTile()
    {
        Ray          ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit2D hit = Physics2D.GetRayIntersection(ray, 50f, Tiles);

        if (hit)
        {
            activeTile = hit.collider.gameObject;
        }
    }
コード例 #10
0
    void rayCasting(Ray ray)
    {
        hit = Physics2D.GetRayIntersection(ray, Mathf.Infinity);

        if (hit.collider != null && hit.transform.gameObject.name == "Skip" && Input.GetMouseButtonDown(0))
        {
            s_TopManager.curState = TopManager.ManagerActiveState.STAGE;
            StopCoroutine(DelayTime());
        }
    }
コード例 #11
0
    void ColorPickTile()
    {
        RaycastHit2D hit = Physics2D.GetRayIntersection(HandleUtility.GUIPointToWorldRay(Event.current.mousePosition), Mathf.Infinity);

        if (hit)
        {
            GameObject newPrefab = hit.collider.gameObject;
            ((AutoTileSetManager)serializedObject.targetObject).currentTile = (GameObject)PrefabUtility.GetPrefabParent(newPrefab);
        }
    }
コード例 #12
0
    public void FindSpot()
    {
        hit = Physics2D.GetRayIntersection(Camera.main.ScreenPointToRay(Input.mousePosition), cullingMask);

        if (hit.collider.tag == "GrapplePoint")
        {
            isFlying   = true;
            LR.enabled = true;
        }
    }
コード例 #13
0
        /// <summary>
        /// マウス下にオブジェクトがあるかどうかを確認
        /// </summary>
        private void HitTestByRaycast()
        {
            var position = Input.mousePosition;

            // // uGUIの上か否かを判定
            var raycastResults = new List <RaycastResult>();

            pointerEventData.position = position;
            EventSystem.current.RaycastAll(pointerEventData, raycastResults);
            foreach (var result in raycastResults)
            {
                // レイヤーマスクを考慮(Ignore Raycast 以外ならヒット)
                if (((1 << result.gameObject.layer) & hitTestLayerMask) > 0)
                {
                    onObject = true;
                    return;
                }
            }
            // レイヤーに関わらずヒットさせる場合は下記でよい
            // // uGUIの上と判定されれば、終了
            // if (EventSystem.current.IsPointerOverGameObject())
            // {
            //     onObject = true;
            //     return;
            // }

            if (currentCamera && currentCamera.isActiveAndEnabled)
            {
                Ray ray = currentCamera.ScreenPointToRay(position);

                // 3Dオブジェクトの上か否かを判定
                if (Physics.Raycast(ray, out _, raycastMaxDepth))
                {
                    onObject = true;
                    return;
                }

                // 2Dオブジェクトの上か判定
                var rayHit2D = Physics2D.GetRayIntersection(ray);
                Debug.DrawRay(ray.origin, ray.direction, Color.blue, 2f, false);
                if (rayHit2D.collider != null)
                {
                    onObject = true;
                    return;
                }
            }
            else
            {
                // カメラが有効でなければメインカメラを取得
                currentCamera = Camera.main;
            }

            // いずれもヒットしなければオブジェクト上ではないと判断
            onObject = false;
        }
コード例 #14
0
ファイル: CreateGame.cs プロジェクト: MeKyleH/Beans
    // Update is called once per frame
    void Update()
    {
        CheckGrid();
        if (Input.GetMouseButtonDown(0))
        {
            Ray          ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit2D hit = Physics2D.GetRayIntersection(ray, 1000);
            if (hit)
            {
                tile1 = hit.collider.gameObject;
            }
        }

        // if finger up is detected after an initial tiel has been chosen
        else if (Input.GetMouseButtonUp(0) && tile1)
        {
            Ray          ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit2D hit = Physics2D.GetRayIntersection(ray, 1000);
            if (hit)
            {
                tile2 = hit.collider.gameObject;
            }
            if (tile1 && tile2)
            {
                // check tile distances between selected tiles
                int horzDist = (int)Mathf.Abs(tile1.transform.position.x - tile2.transform.position.x);
                int vertDist = (int)Mathf.Abs(tile1.transform.position.y - tile2.transform.position.y);

                // only swap if they are 1 away
                //TODO FIX THIS SO YOU CAN'T SWAP 1,2 OR 2,1, ETC
                if (horzDist == 1 ^ vertDist == 1)
                {
                    // swap the tiles in memory (tiles[,]) for matching logic
                    Tile temp = tiles[(int)tile1.transform.position.x,
                                      (int)tile1.transform.position.y];
                    tiles[(int)tile1.transform.position.x, (int)tile1.transform.position.y] =
                        tiles[(int)tile2.transform.position.x, (int)tile2.transform.position.y];
                    tiles[(int)tile2.transform.position.x, (int)tile2.transform.position.y] = temp;

                    // swap the tiles in the game on screen
                    Vector3 tempPos = tile1.transform.position;
                    tile1.transform.position = tile2.transform.position;
                    tile2.transform.position = tempPos;

                    // reset touched tiles
                    tile1 = null;
                    tile2 = null;
                }
                else
                {
                    GetComponent <AudioSource>().Play();
                }
            }
        }
    }
コード例 #15
0
    void checkHitObject()
    {
        RaycastHit2D hit2D = Physics2D.GetRayIntersection(Camera.main.ScreenPointToRay(Input.mousePosition));

        if (hit2D.collider != null && hit2D.collider.GetComponent <DeckCardController>() != null)
        {
            Debug.Log(hit2D.collider.name);
            hit2D.collider.gameObject.GetComponent <DeckCardController>().clicado = true;
            ManagerGame.Instance.LockPlayerActive = true;
        }
    }
コード例 #16
0
    public static GameObject IntersectObject(Vector3 position)
    {
        Ray          ray = Camera.main.ScreenPointToRay(position);
        RaycastHit2D hit = Physics2D.GetRayIntersection(ray, Mathf.Infinity);

        if (hit.collider != null)
        {
            return(hit.collider.gameObject);
        }
        return(null);
    }
コード例 #17
0
    void Update()
    {
        var mousePos = Input.mousePosition;

        mousePos.z = 10;

        var worldPosition = Camera.main.ScreenToWorldPoint(mousePos);

        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit2D rayHit = Physics2D.GetRayIntersection(Camera.main.ScreenPointToRay(Input.mousePosition));

            var collider = rayHit.collider;
            if (!collider)
            {
                return;
            }

            var body = collider.attachedRigidbody;
            if (!body)
            {
                return;
            }

            if (collider.gameObject.tag != "Robot")
            {
                return;
            }

            // stop the robot from pathfinding/following while grabbed
            grabbedRobot         = collider.gameObject.GetComponent <Robot> ();
            grabbedRobot.grabbed = true;

            targetJoint = body.gameObject.AddComponent <TargetJoint2D>();
            targetJoint.dampingRatio = damping;
            targetJoint.frequency    = frequency;

            targetJoint.anchor = targetJoint.transform.InverseTransformPoint(worldPosition);
        }
        else if (Input.GetMouseButtonUp(0))
        {
            //	if (grabbedRobot != null) {
            Destroy(targetJoint);
            targetJoint          = null;
            grabbedRobot.grabbed = false;
            return;
            //	}
        }

        if (targetJoint)
        {
            targetJoint.target = worldPosition;
        }
    }
コード例 #18
0
 private void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         RaycastHit2D ray = Physics2D.GetRayIntersection(Camera.main.ScreenPointToRay(Input.mousePosition));
         if (ray)
         {
             TrySelectTile(ray.collider.gameObject.GetComponent <Tile>());
         }
     }
 }
コード例 #19
0
ファイル: GameMode.cs プロジェクト: ZXYmania/Chevauchee
    public GameObject FindObjectUnder()
    {
        Ray          mousePoint = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit2D foundItem  = Physics2D.GetRayIntersection(mousePoint, Mathf.Infinity, m_layerMask);

        if (foundItem.transform != null)
        {
            return(foundItem.transform.gameObject);
        }
        return(null);
    }
コード例 #20
0
    private GameObject checkTouch(Vector2 pos)
    {
        Ray          ray          = mainCamera.ScreenPointToRay(pos);
        RaycastHit2D raycastHit2D = Physics2D.GetRayIntersection(ray);

        if (raycastHit2D.collider)
        {
            return(raycastHit2D.collider.gameObject);
        }
        return(null);
    }
コード例 #21
0
 private void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         RaycastHit2D hit = Physics2D.GetRayIntersection(Camera.main.ScreenPointToRay(Input.mousePosition));
         if (hit.point != null)
         {
             CheckForSelectingTile(hit.collider.GetComponent <Tile>());
         }
     }
 }
コード例 #22
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("mousedown!");
            Ray          worldRay = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit2D hit      = Physics2D.GetRayIntersection(worldRay);
            if (hit)
            {
                GameObject clickedObject = hit.transform.gameObject;
                // Debug.Log("hit object with tag: "+clickedObject.tag);
                if (clickedObject == gameObject)
                {
                    Debug.Log("currentDraggingItem: " + gameObject.name);
                    currentDraggingItem = clickedObject;

                    // highlight associated person
                    // HACKY
                    foreach (GameObject person in GameObject.FindGameObjectsWithTag("Person"))
                    {
                        person.GetComponent <PersonController>().CheckItemHighlight(gameObject.name);
                    }
                }
            }
        }
        else if (Input.GetMouseButtonUp(0))
        {
            Debug.Log("mouseup!");
            if (currentDraggingItem)
            {
                currentDraggingItem = null;
                transform.position  = spawnPosition; // reset

                // HACKY
                foreach (GameObject person in GameObject.FindGameObjectsWithTag("Person"))
                {
                    person.GetComponent <PersonController>().CheckItemHighlight(gameObject.name, false);
                    person.GetComponent <PersonController>().CheckItem(gameObject.GetComponent <Collider2D>());
                }
            }
        }

        if (currentDraggingItem)
        {
            Vector3 mousePosition = Input.mousePosition;
            mousePosition.z = -Camera.main.transform.position.z; // otherwise we always get same point at camera (which is at -10)
            // Debug.Log("mousePosition on screen: "+Input.mousePosition);

            Vector3 mousePositionInWorld = Camera.main.ScreenToWorldPoint(mousePosition);
            // Debug.Log("mousePosition in world: "+mousePositionInWorld);

            currentDraggingItem.transform.position = mousePositionInWorld;
        }
    }
コード例 #23
0
    public static GameObject GetObjectUnderMouse2D(LayerMask mask)
    {
        Vector2 mouse = MouseWorldPos;
        var     hit   = Physics2D.GetRayIntersection(new Ray(new Vector3(mouse.x, mouse.y, -100), Vector3.forward), float.MaxValue, mask);

        if (hit.collider)
        {
            return(hit.collider.gameObject);
        }
        return(null);
    }
コード例 #24
0
    // Update is called once per frame
    private void Update()
    {
#if UNITY_EDITOR
        if (Input.GetMouseButton(0))
        {
            Ray          ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit2D hit = Physics2D.GetRayIntersection(ray, Mathf.Infinity);

            if (hit.collider != null && hit.collider.transform == transform)
            {
                if (isLeftTopButton == true)
                {
                    foreach (Platform p in platform)
                    {
                        p.transform.Rotate(0, 0, 3);
                    }
                }
                else
                {
                    foreach (Platform p in platform)
                    {
                        p.transform.Rotate(0, 0, -3);
                    }
                }
            }
        }
#endif

#if UNITY_ANDROID
        foreach (Touch toque in Input.touches)
        {
            if (GetComponent <BoxCollider2D>().OverlapPoint(Camera.main.ScreenToWorldPoint(toque.position)))
            {
                if (toque.phase == TouchPhase.Began || toque.phase == TouchPhase.Stationary)
                {
                    if (isLeftTopButton == true)
                    {
                        foreach (Platform p in platform)
                        {
                            p.transform.Rotate(0, 0, 3);
                        }
                    }
                    else
                    {
                        foreach (Platform p in platform)
                        {
                            p.transform.Rotate(0, 0, -3);
                        }
                    }
                }
            }
        }
#endif
    }
コード例 #25
0
ファイル: Stage.cs プロジェクト: hrSung/LineMatchGame
    // 퍼즐 동물들만 체크.
    private void PuzzleRayCastProcess()
    {
        Ray          ray     = UICamera.currentCamera.ScreenPointToRay(UICamera.lastEventPosition);
        RaycastHit2D raycast = Physics2D.GetRayIntersection(ray, Mathf.Infinity
                                                            , 1 << LayerMask.NameToLayer("Animal"));

        if (raycast)
        {
            SetAnimalCount(raycast.transform.GetComponent <Animal>());
        }
    }
コード例 #26
0
    private void Reset()
    {
        _ray = Physics2D.GetRayIntersection(_camera.ScreenPointToRay(Input.mousePosition), float.PositiveInfinity, _mainLayer);
        if (_ray)
        {
            var targetObject = _ray.collider.gameObject;
            switch (_membership)
            {
            case Membership.Undefined:
                break;

            case Membership.Player:
                _publisher.Publish(this, new CustomEventArgs(GameEventName.TargetPlayer, targetObject));
                break;

            case Membership.Enemy:
                _publisher.Publish(this, new CustomEventArgs(GameEventName.TargetEnemy, targetObject));
                break;

            case Membership.AlliesArea:
                _publisher.Publish(this, new CustomEventArgs(GameEventName.TargetArea, targetObject));
                break;
            }
            _publisher.Publish(this, new CustomEventArgs(GameEventName.TargetСapture, targetObject));
        }
        else
        {
            _publisher.Publish(this, new CustomEventArgs(GameEventName.NonTargetСapture));
        }

        _targeting = false;
        switch (_membership)
        {
        case Membership.Undefined:
            break;

        case Membership.Player:
            _publisher.Publish(this, new CustomEventArgs(GameEventName.NonTargetingPlayer));
            break;

        case Membership.Enemy:
            _publisher.Publish(this, new CustomEventArgs(GameEventName.NonTargetingСapture));
            break;

        case Membership.AlliesArea:
            _publisher.Publish(this, new CustomEventArgs(GameEventName.NonTargetingArea));
            break;
        }

        _startPosition     = transform.position;
        transform.position = new Vector3(0, 0, 0);
        _lineMaterial.mainTextureOffset = new Vector2(0, 0);
        gameObject.SetActive(false);
    }
コード例 #27
0
        public static T GetComponentOnGameObjectUnderMouse <T>() where T : MonoBehaviour
        {
            var hitInfo = Physics2D.GetRayIntersection(Camera.main.ScreenPointToRay(Input.mousePosition));

            if (hitInfo.collider != null)
            {
                return(hitInfo.collider.gameObject.GetComponent <T>());
            }

            return(null);
        }
コード例 #28
0
    private void DetectObject()
    {
        Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);

        RaycastHit2D hits2D = Physics2D.GetRayIntersection(ray);

        if (hits2D.collider != null)
        {
            Debug.Log("Hit 2D Collider" + hits2D.collider.tag);
        }
    }
コード例 #29
0
 private void Update()
 {
     if (Input.GetMouseButtonDown(0) && swapRunning == false)
     {
         RaycastHit2D ray = Physics2D.GetRayIntersection(Camera.main.ScreenPointToRay(Input.mousePosition));
         if (ray != false)
         {
             CheckSelectionTile(ray.collider.gameObject.GetComponent <Tile>());
         }
     }
 }
コード例 #30
0
    private bool IsHeadpatting()
    {
        Ray          ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit2D hit = Physics2D.GetRayIntersection(ray, Mathf.Infinity);

        if (hit.collider != null && hit.collider.transform == transform)
        {
            return(true);
        }
        return(false);
    }