コード例 #1
0
 void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         Vector3      screenToWorldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         RaycastHit2D hit2d = Physics2D.Raycast(screenToWorldPoint, Vector2.zero);
         deltaRotation = 0f;
         if (hit2d)
         {
             if (hit2d.collider.CompareTag("Rotatable"))
             {
                 gameObjectToRotate = hit2d.collider.gameObject.transform.parent;
                 rotatingMode       = true;
                 savedRotation      = gameObjectToRotate.rotation;
                 previousRotation   = angleBetweenPoints(gameObjectToRotate.position, screenToWorldPoint);
             }
         }
     }
     if (Input.GetMouseButton(0))
     {
         if (rotatingMode)
         {
             currentRotation = angleBetweenPoints(gameObjectToRotate.position, Camera.main.ScreenToWorldPoint(Input.mousePosition));
             deltaRotation   = Mathf.DeltaAngle(currentRotation, previousRotation);
             if (Mathf.Abs(deltaRotation) > deltaLimit)
             {
                 deltaRotation = deltaLimit * Mathf.Sign(deltaRotation);
             }
             previousRotation = currentRotation;
             gameObjectToRotate.Rotate(Vector3.back * Time.deltaTime, deltaRotation);
         }
     }
     if (Input.GetMouseButtonUp(0))
     {
         if (gameObjectToRotate && rotatingMode)
         {
             ManualBelt belt = gameObjectToRotate.gameObject.GetComponent <ManualBelt> ();
             Wall       wall = gameObjectToRotate.gameObject.GetComponent <Wall> ();
             if (belt && !belt.getPlaceable() && gameObjectToRotate.transform.position.x < 7)
             {
                 gameObjectToRotate.rotation = savedRotation;
                 errorSound.Play();
             }
             else if (wall && !wall.getPlaceable() && gameObjectToRotate.transform.position.x < 7)
             {
                 gameObjectToRotate.rotation = savedRotation;
                 errorSound.Play();
             }
             else
             {
                 deltaRotation = Mathf.Lerp(deltaRotation, 0, deltaReduce * Time.deltaTime);
                 placeSound.Play();
             }
             rotatingMode = false;
         }
     }
 }
コード例 #2
0
ファイル: NonPlaceableArea.cs プロジェクト: zane-c/flow
 void OnTriggerStay2D(Collider2D collider)
 {
     if (collider.gameObject.layer == 10)
     {
         ManualBelt belt = collider.gameObject.GetComponent <ManualBelt> ();
         if (belt)
         {
             belt.setPlaceable(false);
         }
         Wall wall = collider.gameObject.GetComponent <Wall> ();
         if (wall)
         {
             wall.setPlaceable(false);
         }
     }
 }
コード例 #3
0
ファイル: DraggingScript.cs プロジェクト: zane-c/flow
    void Update()
    {
        if (Input.GetMouseButtonDown(0) && keeperScript.getActiveBalls() == 0)
        {
            RaycastHit2D hit2d = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);

            if (hit2d)
            {
                if (hit2d.collider.CompareTag("Draggable"))
                {
                    gameObjectToDrag = hit2d.collider.gameObject;
                    GOCenter         = gameObjectToDrag.transform.position;
                    clickPosition    = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                    offset           = clickPosition - GOCenter;
                    draggingMode     = true;
                }
            }
        }

        if (Input.GetMouseButton(0) && keeperScript.getActiveBalls() == 0 && draggingMode)
        {
            clickPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            newGOCenter   = clickPosition - offset;
            gameObjectToDrag.transform.position = new Vector3(newGOCenter.x, newGOCenter.y, GOCenter.z);
        }

        if (Input.GetMouseButtonUp(0))
        {
            if (draggingMode)
            {
                ManualBelt belt = gameObjectToDrag.GetComponent <ManualBelt> ();
                Wall       wall = gameObjectToDrag.GetComponent <Wall> ();
                if (belt && !belt.getPlaceable() && gameObjectToDrag.transform.position.x < 7)
                {
                    gameObjectToDrag.transform.position = GOCenter;
                    errorSound.Play();
                }
                else if (wall && !wall.getPlaceable() && gameObjectToDrag.transform.position.x < 7)
                {
                    gameObjectToDrag.transform.position = GOCenter;
                    errorSound.Play();
                }
                else
                {
                    placeSound.Play();
                }
                if (gameObjectToDrag.transform.position.x > 7)
                {
                    if (gameObjectToDrag.transform.GetChild(0).gameObject.CompareTag("ConveyorBelt"))
                    {
                        creator = GameObject.Find("ConveyorBeltCreator");
                        creator.GetComponent <Create> ().Increment();
                    }
                    if (gameObjectToDrag.transform.GetChild(0).gameObject.CompareTag("Wall"))
                    {
                        creator = GameObject.Find("WallCreator");
                        creator.GetComponent <Create> ().Increment();
                    }
                    Destroy(gameObjectToDrag);
                    errorSound.Play();
                }
                draggingMode = false;
            }
        }
    }