コード例 #1
0
    //Finds the closest controllable object to hit point and returns its controller script
    private IControllableMovement FindClosestControllableScript(Vector3 inputPosition, out GameObject closestObjectToInputPosition)
    {
        IControllableMovement controllableScript        = default(IControllableMovement);
        GameObject            closestControllableObject = default(GameObject);

        inputPosition.y = 0f;

        //Ignore objects that are not controllable
        //Used for generating OverlapSphere
        int layerMask = 1 << 8;

        Collider[] colliders = Physics.OverlapSphere(inputPosition, 5f, layerMask);

        //If there are multiple collactables finds the closest one
        if (colliders.Length > 0)
        {
            if (colliders.Length > 1)
            {
                GameObject[] gameObjects = ConvertCollidersToGameObjects(colliders);
                closestControllableObject = GetClosestObjectToAPoint(gameObjects, inputPosition);
            }
            else
            {
                closestControllableObject = colliders[0].gameObject;
            }

            controllableScript = closestControllableObject.GetComponent <IControllableMovement>();
        }
        closestObjectToInputPosition = closestControllableObject;
        return(controllableScript);
    }
コード例 #2
0
    void Update()
    {
        lineHitData = lineCreator.lastHitData;

        #region Controls
        #if UNITY_EDITOR
        Vector3 dragValueMouse = userInputController.GetMouseDragValue();

        //This is necessary to get first clicks
        //First clicks are used to find gameObjects that close to clicks world position
        if (Input.GetMouseButtonDown(0))
        {
            inputHit = GetHitData(Input.mousePosition);
        }
        //Finds closest object only while left mouse button is held down
        else if (Input.GetMouseButton(0))
        {
            controllableScript = FindClosestControllableScript(inputHit.point, out closestObjectToInputPosition);
        }
        else
        {
            closestObjectToInputPosition = null;    //Reset the closest object

            //If the aiming is succesfully done
            if (lineHitData.collider != null)
            {
                if (lineHitData.collider.CompareTag("GoalPost") && !isAimingDone)
                {
                    isAimingDone = true;
                    totalPoints.AddRange(lineCreator.currentPoints);
                    StartCoroutine(DelayBallScene());
                }
            }
        }

        if (controllableScript != null)
        {
            controllableScript.Move(dragValueMouse);
        }
        #elif UNITY_ANDROID
        Vector3 dragValueTouch = userInputController.GetTouchDragValue();

        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);
            if (touch.phase == TouchPhase.Began)
            {
                inputHit = GetHitData(touch.position);
            }
            //Finds closest object only while the screen is touched
            else if (touch.phase != TouchPhase.Ended)
            {
                controllableScript = FindClosestControllableScript(inputHit.point, out closestObjectToInputPosition);
            }
            else
            {
                closestObjectToInputPosition = null;    //Reset the closest object

                //If the aiming succesfully done
                if (lineHitData.collider != null)
                {
                    if (lineHitData.collider.CompareTag("GoalPost") && !isAimingDone)
                    {
                        isAimingDone = true;
                        totalPoints.AddRange(lineCreator.currentPoints);
                        StartCoroutine(DelayBallScene());
                    }
                }
            }

            if (controllableScript != null)
            {
                controllableScript.Move(dragValueTouch);
            }
        }
        #endif
        #endregion
    }