将相机平移与Unity中的触控控制相结合

时间:2016-04-08 13:06:57

标签: c# unity3d

我被要求研究用Unity创建一个简单的迭代应用程序。此应用程序有两个关于相机的主要功能。

  1. 将相机对准目标物体。
  2. 一旦将放弃控制移动到用户并允许用户围绕对象旋转和缩放。
  3. 我是新手,但我已经成功创建了两个单独实现这些目标的脚本。现在我正努力让他们在一起。

    我将从用户交互的相关代码开始。

    首先,我使用TouchKit在Start的每一帧上设置增量值。

    // set the delta on each frame for horizontal and vertical rotation
    var oneTouch = new TKPanRecognizer();
    oneTouch.gestureRecognizedEvent += (r) =>
    {
        HorizontalDelta += r.deltaTranslation.x * rotateSpeed * Time.deltaTime;
        VerticalDelta -= r.deltaTranslation.y * rotateSpeed * Time.deltaTime;
    };
    
    // do the same for pinch
    var pinch = new TKPinchRecognizer();
    pinch.gestureRecognizedEvent += (r) =>
    {
        rotateDistance -= r.deltaScale * 200.0f * Time.deltaTime;
    };
    
    TouchKit.addGestureRecognizer(oneTouch);
    TouchKit.addGestureRecognizer(pinch);
    

    Update

    VerticalDelta = Mathf.Clamp(VerticalDelta, verticalPivotMin, verticalPivotMax);
    
    var direction = GetDirection(HorizontalDelta, VerticalDelta);
    
    var currentTarget = targetsSwitched ? target2 : target;
    
    transform.position = currentTarget.position - direction * rotateDistance;
    transform.LookAt(currentTarget.position);
    
    // ...
    
    private Vector3 GetDirection(float x, float y)
    {
        Quaternion q = Quaternion.Euler(y, x, 0);
        return q * Vector3.forward;
    }
    

    这很好用,完全符合我的要求。当我尝试将此代码与我的相机移动脚本集成时,问题就来了。这显示了我想要添加Update代码

    的位置
    void Update ()
    {
        if (currentlyMoving)
        {
            FocusTarget(currentTarget);
        }
        else
        {
            // accept user input if not moving
            if (Input.GetKeyDown(KeyCode.Space))
            {
                SetMoveToTarget(mainTargetObject);
            }
    
            if (Input.GetKeyDown(KeyCode.Q))
            {
                SetMoveToTarget(subTargetObject1);
            }
    
            if (Input.GetKeyDown(KeyCode.E))
            {
                SetMoveToTarget(subTargetObject2);
            }
        }
    }
    

    这些是实际移动相机的功能:

    void SetMoveToTarget(GameObject target)
    {
        if (currentlyMoving == false)
        {
            currentlyMoving = true;
            fromRotation = currentTarget.transform.rotation;
            currentTarget = target;
            toRotation = currentTarget.transform.rotation;
    
            timeStartedLerping = Time.time;
        }
    }
    
    void FocusTarget(GameObject target)
    {
    
        float timeSinceStarted = Time.time - timeStartedLerping;
        float percentageComplete = timeSinceStarted / (lerpSpeed);
    
        transform.position = Vector3.MoveTowards(transform.position, target.transform.position, moveSpeed * Time.deltaTime);
        transform.rotation = Quaternion.Lerp(fromRotation, toRotation, Mathf.Pow(percentageComplete, (float)1.2));
    
        if (Vector3.Distance(transform.position, target.transform.position) < 0.1 && percentageComplete > 0.99)
        {
            transform.position = target.transform.position;
            transform.rotation = target.transform.rotation;
            currentlyMoving = false;
        }
    }
    

    我认为我需要做的事情(我可能错了)将rotateDistance设置为第二个脚本中的currentTarget和第一个脚本中的currentTarget之间的差异脚本。

    提前谢谢你,这对我来说非常棘手。

0 个答案:

没有答案