Beispiel #1
0
    private void FixedUpdate()
    {
        if (visitedCoinCount == MazeDescription.Coins)
        {
            if (!finished)
            {
                successTime = Time.unscaledTime;
            }
            Debug.Log("Success. Time:  " + successTime);
            success = true;
            Finish();
            return;
        }

        if (iterations == MazeDescription.BallEnergy)
        {
            Debug.Log("Ball has no energy!");
            success = false;
            Finish();
            return;
        }

        if (true || !MazeDescription.IsConsoleRun())
        {
            var stepCount = 0;
            while (Time.time * 1000 + IntervalMilliseconds >= lastCallTime)
            {
                ++stepCount;
                lastCallTime += IntervalMilliseconds;
            }
            if (stepCount == 0)
            {
                return;
            }
        }

        SaveBallPosition();
        if (mRigidBody != null)
        {
            int move = 0;
            try {
                move = ballController.GetMove(transform.position.x, transform.position.z);
            }
            catch (Exception) {
                Application.Quit(1);
            }
            var velocity = Vector3.zero;
            if ((move & BallControl.MoveTypeRight) != 0)
            {
                velocity += Vector3.right;
            }
            if ((move & BallControl.MoveTypeBottom) != 0)
            {
                velocity += Vector3.back;
            }
            if ((move & BallControl.MoveTypeLeft) != 0)
            {
                velocity -= Vector3.right;
            }
            if ((move & BallControl.MoveTypeTop) != 0)
            {
                velocity -= Vector3.back;
            }

            mRigidBody.velocity = velocity;
            if (velocity != Vector3.zero)
            {
                mRigidBody.velocity = velocity.normalized * Speed;
            }
            Debug.Log(iterations);
            ++iterations;
        }

        if (ViewCamera != null)
        {
            var        direction = (Vector3.up * 5 + Vector3.back) * 4;
            RaycastHit hit;
            Debug.DrawLine(transform.position, transform.position + direction, Color.red);
            ViewCamera.transform.position =
                Physics.Linecast(transform.position, transform.position + direction, out hit)
               ? hit.point
               : transform.position + direction;
            ViewCamera.transform.LookAt(transform.position);
        }
    }
    private void FixedUpdate()
    {
        if (visitedCoinCount == MazeDescription.Coins)
        {
            if (!finished)
            {
                successTime = Time.unscaledTime;
            }
            Debug.Log("Success. Time:  " + successTime);
            success = true;
            Finish();
            return;
        }

        SaveBallPosition();
        var move = 0;

        try { move = ballController.GetMove(transform.position.x, transform.position.z); }
        catch (Exception exception) { if (Debug.isDebugBuild)
                                      {
                                          throw exception;
                                      }
                                      Application.Quit(1); }
        var torque = Vector3.zero;

        if ((move & BallControl.MoveTypeRight) != 0)
        {
            torque += Vector3.right;
        }
        if ((move & BallControl.MoveTypeBottom) != 0)
        {
            torque += Vector3.back;
        }
        if ((move & BallControl.MoveTypeLeft) != 0)
        {
            torque -= Vector3.right;
        }
        if ((move & BallControl.MoveTypeTop) != 0)
        {
            torque -= Vector3.back;
        }

        if (torque != Vector3.zero)
        {
            var newTorque = torque.normalized * Speed;
            rigidBody.AddTorque(new Vector3(newTorque.z, 0, -newTorque.x));
        }
        ++iterations;
        Debug.Log(iterations + " " + visitedCoinCount);

        if (ViewCamera != null)
        {
            var        direction = (Vector3.up * 5 + Vector3.back) * 4;
            RaycastHit hit;
            ViewCamera.transform.position =
                Physics.Linecast(transform.position, transform.position + direction, out hit)
                    ? hit.point
                    : transform.position + direction;
            ViewCamera.transform.LookAt(transform.position);
        }
    }