如何在Unity中使用触摸输入旋转对象并使其缓慢停止?

时间:2018-10-27 20:07:03

标签: c# unity3d

我从这里得到一些代码: https://answers.unity.com/questions/34317/rotate-object-with-mouse-cursor-that-slows-down-on.html

 private float rotationSpeed = 10.0F;
 private float lerpSpeed = 1.0F;

 private Vector3 theSpeed;
 private Vector3 avgSpeed;
 private bool isDragging = false;
 private Vector3 targetSpeedX;

 void OnMouseDown() {

     isDragging = true;
 }

 void Update() {

     if (Input.GetMouseButton(0) && isDragging) {
         theSpeed = new Vector3(-Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"), 0.0F);
         avgSpeed = Vector3.Lerp(avgSpeed, theSpeed, Time.deltaTime * 5);
     } else {
         if (isDragging) {
             theSpeed = avgSpeed;
             isDragging = false;
         }
         float i = Time.deltaTime * lerpSpeed;
         theSpeed = Vector3.Lerp(theSpeed, Vector3.zero, i);
     }

     transform.Rotate(Camera.main.transform.up * theSpeed.x * rotationSpeed, Space.World);
     transform.Rotate(Camera.main.transform.right * theSpeed.y * rotationSpeed, Space.World);
 }

此代码通过单击鼠标并拖动来旋转对象,单击此对象后,该对象将缓慢停止。我现在想在手机上使用此功能。这是我的移动版本代码:

// Update is called once per frame
public void Update() {

    // Track a single touch as a direction control.
    if (Input.touchCount > 0) {
        Touch touch = Input.GetTouch(0);

        // Handle finger movements based on touch phase.
        switch (touch.phase) {
            // Record initial touch position.
            case TouchPhase.Began:
                startPos = touch.position;
                directionChosen = false;
                break;

            // Determine direction by comparing the current touch position with the initial one.
            case TouchPhase.Moved:
                direction = touch.position - startPos;
                break;

            // Report that a direction has been chosen when the finger is lifted.
            case TouchPhase.Ended:
                directionChosen = true;
                stopSlowly = true;
                Debug.Log("end");
                break;
        }
    }
    if (directionChosen) {
        // Something that uses the chosen direction...
        theSpeed = new Vector3(-direction.x, direction.y, 0.0F);
        avgSpeed = Vector3.Lerp(avgSpeed, theSpeed, Time.deltaTime * 5);
    } else {
        if (stopSlowly) {
            Debug.Log("TESTOUTPUT");
            theSpeed = avgSpeed;
            isDragging = false;
        }
        float i = Time.deltaTime * lerpSpeed;
        theSpeed = Vector3.Lerp(theSpeed, Vector3.zero, i);
    }

    transform.Rotate(camera.transform.up * theSpeed.x * rotationSpeed, Space.World);
    transform.Rotate(camera.transform.right * theSpeed.y * rotationSpeed, Space.World);

这是一些变量,我将Vector2变量用于startPos和direction:

private float rotationSpeed = 1f;
private float lerpSpeed = 1.0F;

private Vector3 theSpeed;
private Vector3 avgSpeed;
private bool isDragging = false;
private Vector3 targetSpeedX;


public Vector2 startPos;
public Vector2 direction;
public bool directionChosen;
public bool stopSlowly;

现在,如果我按播放并旋转手机上的对象,它会旋转,但它不会自行结束。它还旋转得非常快。一次触摸对象时,它立即停止。 请有人可以告诉我我的代码到底有什么问题。我的目标只是旋转,从触摸输入开始初始化,直到它静止不动为止。

谢谢

2 个答案:

答案 0 :(得分:0)

您的情况非常混乱。 尝试这样的事情

    // Handle finger movements based on touch phase.
    switch (touch.phase) {
        // Record initial touch position.
        case TouchPhase.Began:
            startPos = touch.position;
            break;

        // Determine direction by comparing the current touch position with the initial one.
        case TouchPhase.Moved:
            direction = touch.position - startPos;
            isDragging = true;
            break;

        // Report that a direction has been chosen when the finger is lifted.
        case TouchPhase.Ended:
            isDragging = false;
            stopSlowly = true;
            Debug.Log("end");
            break;
    }

if (isDragging) {
    // Something that uses the chosen direction...
    theSpeed = new Vector3(-direction.x, direction.y, 0.0F);
    avgSpeed = Vector3.Lerp(avgSpeed, theSpeed, Time.deltaTime * 5);
} else {
    if(stopSlowly) {
        Debug.Log("TESTOUTPUT");
        theSpeed = avgSpeed;
        stopSlowly = false;
    }
    float i = Time.deltaTime * lerpSpeed;
    theSpeed = Vector3.Lerp(theSpeed, Vector3.zero, i);
}

答案 1 :(得分:0)

我现在使用一些物理学来解决这个问题:

// React on User Touch Input -> Rotate gameObject
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
            // Get movement of the finger since last frame
            Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;

            // Add Torque to gameObject
            rb.AddTorque(camera.transform.up * -touchDeltaPosition.x/* * optionalForce*/);
            rb.AddTorque(camera.transform.right * touchDeltaPosition.y/* * optionalForce*/);
        } else if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended) {
            // throw anker, stop rotating slowly
            rb.angularDrag = 0.7f;
        }

此代码用于触摸输入并旋转对象+缓慢将其停止。您需要在物体上旋转一个刚体。这里叫rb。相机是我的主要相机。