コード例 #1
0
    void Update()
    {
        if (TouchPadInput.GetTouchesCount() > 0)
        {
            GameObject go = TouchPadInput.TouchedObject();
            if (go == handler.gameObject)
            {
                touchingHandler = true;
            }
        }
        else
        {
            touchingHandler = false;
        }

        if (touchingHandler)
        {
            Vector3 touchPos    = Camera.main.ScreenToViewportPoint(TouchPadInput.CurrentTouchPosition());
            Vector2 handlerPos1 = Camera.main.WorldToViewportPoint(handler.position);

            float t = Time.deltaTime;

            float difference = touchPos.x - handlerPos1.x;

            if (difference > 0)
            {
                Vector3 rightLimit = new Vector3(bar.position.x + boundsBar.extents.x, bar.position.y, bar.position.z);


                float rightX = (rightLimit.x - handler.position.x);


                if (rightX > 0)
                {
                    handler.transform.Translate(-t * handlerSpeed, 0f, 0f);
                }
            }
            else if (difference < 0)
            {
                Vector3 leftLimit = new Vector3(bar.position.x - boundsBar.extents.x, bar.position.y, bar.position.z);
                float   leftX     = (handler.position.x - leftLimit.x);
                if (leftX > 0)
                {
                    handler.transform.Translate(t * handlerSpeed, 0f, 0f);
                }
            }
        }

        //Keep handler z axis the same as the bar's;
        Vector3 handlerPos = handler.position;
        Vector3 barPos     = bar.position;

        handlerPos.y     = barPos.y;
        handlerPos.z     = barPos.z;
        handler.position = handlerPos;

        CheckDistance();
    }
コード例 #2
0
    void SetBallRolling()
    {
        if (ballState == BallState.Idle)
        {
            GameObject b = TouchPadInput.TouchedObject();

            if (b)
            {
                if (b.name == "Ball")
                {
                    timeSinceTouchingBall += Time.deltaTime;
                }
                else
                {
                    timeSinceTouchingBall = 0.0f;
                }

                if (timeSinceTouchingBall > MinTimeNeededToTouchBallAndSetRolling)
                {
                    ballState             = BallState.Rolling;
                    timeSinceTouchingBall = 0.0f;
                }
            }
            else
            {
                timeSinceTouchingBall = 0.0f;
            }
        }
        else if (ballState == BallState.Rolling)
        {
            if (!currentWaypoint)
            {
                currentWaypoint = wpc.FirstWaypoint();
            }

            else
            {
                currentWaypoint = wpc.NextWaypoint(currentWaypoint, transform.position);
            }

            Vector3 adjustedDirection;
            if (!currentWaypoint.IsLast)
            {
                adjustedDirection = new Vector3(currentWaypoint.transform.position.x - xPitch,
                                                currentWaypoint.transform.position.y,
                                                currentWaypoint.transform.position.z);
            }
            else
            {
                adjustedDirection = new Vector3(pinSet.transform.position.x - xPitch,
                                                pinSet.transform.position.y,
                                                pinSet.transform.position.z);
            }

            Vector3 direction = adjustedDirection - transform.position;

            rb.AddForce(direction * ballSettings.speed);

            ballState = BallState.Rolling;


            if (debugBall.ShowNextWaypointLine)
            {
                Debug.DrawRay(transform.position, direction, Color.green);
            }
        }
    }