简体   繁体   English

通过在Unity中滑动来翻译对象

[英]Translate object by swiping in Unity

I am trying to translate my sphere while swiping my finger on the screen. 我正在尝试在屏幕上滑动手指的同时平移球体。
With the code below I need to swipe again and again to move it forward. 使用下面的代码,我需要一次又一次地滑动以将其向前移动。 How can I translate an object forward by swiping only once? 如何只滑动一次就可以向前平移对象?

public float speed;

void FixedUpdate ()
{  
    if (Input.touchCount > 0 && 
        Input.GetTouch(0).phase == TouchPhase.Ended || (Input.GetMouseButtonDown(0)))
    {
        transform.Translate(Vector3.forward * Time.deltaTime*speed);
    }
}

Your code makes the object move for the exact moment when the TouchPhase has Ended. 您的代码使对象在TouchPhase结束时的确切时刻移动。 In order to push the object, try applying a force. 为了推动物体,请尝试施加力。

Modified C# example from Unity Tutorials : 来自Unity教程的修改后的C#示例:

public float speed;
public Rigidbody rb;

void Start() 
{
    rb = GetComponent<Rigidbody>();
}

void FixedUpdate() 
{
    if (Input.touchCount > 0 &&
        Input.GetTouch(0).phase == TouchPhase.Ended || (Input.GetMouseButtonDown(0)))
    {
        rb.AddForce(Vector3.forward * Time.deltaTime * speed);
    }
}

You need to add a Rigidbody3D Component to the object, if you use this technique. 如果使用此技术,则需要向对象添加Rigidbody3D组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM