Combining touch control camera panning in Unity

I was asked to study creating a simple iterative application with Unity. This application has two main functions regarding the camera.

  • LERP - turn on the camera to focus on the target.
  • After you deselected user control and allowed the user to rotate and zoom in on the object.

I am new to this, but I managed to create two scenarios that achieve these goals in isolation. Now I try my best to match them.

I will start with the appropriate code for user interaction.

First, I use TouchKit to set the delta values ​​for each frame that is in 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);

And on 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;
}

, . , script. , 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 script currentTarget script.

, .

+4
1

, . , , script, , .

, , .

using UnityEngine;
using UnityEngine.UI;

public class NewCamera : MonoBehaviour {

    // targets
    public GameObject target;
    public GameObject target2;

    // settings
    public float RotateSpeed = 50.0f;
    public float RotateDistance = 3;
    public float CameraMoveSpeed = 20f;

    public float VerticalPivotMin = 5;
    public float VerticalPivotMax = 65;

    public float MinZoomIn = 1.7f;
    public float MaxZoomIn = 4f;

    // private
    private GameObject lookTarget;

    private bool currentlyMoving = false;
    private Vector3 targetVector;
    private float timeStartedLerping;

    private float HorizontalDelta;
    private float VerticalDelta;

    void Start ()
    {
        lookTarget = target;

        var oneTouch = new TKPanRecognizer();
        oneTouch.gestureRecognizedEvent += (r) =>
        {
            if (currentlyMoving == false)
            {
                HorizontalDelta += r.deltaTranslation.x * RotateSpeed * Time.deltaTime;
                VerticalDelta -= r.deltaTranslation.y * RotateSpeed * Time.deltaTime;

                VerticalDelta = Mathf.Clamp(VerticalDelta, VerticalPivotMin, VerticalPivotMax);
            }
        };

        var pinch = new TKPinchRecognizer();
        pinch.gestureRecognizedEvent += (r) =>
        {
            if (currentlyMoving == false)
            {
                RotateDistance -= r.deltaScale * 200.0f * Time.deltaTime;
                RotateDistance = Mathf.Clamp(RotateDistance, MinZoomIn, MaxZoomIn);
            }
        };

        TouchKit.addGestureRecognizer(oneTouch);
        TouchKit.addGestureRecognizer(pinch);
    }

    void Update ()
    {
        if (currentlyMoving)
        {
            FocusTarget();
        }
        else
        {
            var direction = GetDirection(HorizontalDelta, VerticalDelta);

            transform.position = lookTarget.transform.position - direction * RotateDistance;
            transform.LookAt(lookTarget.transform.position);
        }
    }

    public void OnButtonClick()
    {
        var currentTarget = target.GetInstanceID() == lookTarget.GetInstanceID() ? target : target2;
        var nextTarget = currentTarget.GetInstanceID() == target.GetInstanceID() ? target2 : target;

        var cameraPosition = transform.position;
        var targetPosition = currentTarget.transform.position;

        SetMoveToTarget(nextTarget, cameraPosition - targetPosition);
    }

    void SetMoveToTarget(GameObject moveTo, Vector3 offset)
    {
        currentlyMoving = true;
        targetVector = moveTo.transform.position + offset;
        lookTarget = moveTo;
    }

    void FocusTarget()
    {
        transform.position = Vector3.Lerp(transform.position, targetVector, CameraMoveSpeed * Time.deltaTime);

        if (Vector3.Distance(transform.position, targetVector) < 0.01)
        {
            transform.position = targetVector;
            currentlyMoving = false;
        }
    }

    private Vector3 GetDirection(float x, float y)
    {
        Quaternion q = Quaternion.Euler(y, x, 0);
        return q * Vector3.forward;
    }
}

- , script . , , .

!

+2

All Articles