Unity C#TouchScript – 将对象移动到Flick的方向

我正在尝试获取一个对象来读取轻弹(使用Unity上的Touchscript包) – 然后在方向上轻弹脚本附加的对象.它肯定会读取轻弹,但我不知道如何读取/获取方向以将力添加到刚体上/将其平移到方向上.我看了一下像ScreenFlickVector& amp;没有运气的手势方向.我很乐意帮助你!

private Rigidbody rb;
    // Use this for initialization
    void Start () {
        rb= GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        transform.Translate(Vector3.down * Time.deltaTime);

    }

    private void OnEnable()
    {
        // subscribe to gesture's Tapped event
        GetComponent<FlickGesture>().Flicked += OnFlick;

    }
    private void OnDisable()
    {
        GetComponent<FlickGesture>().Flicked -= OnFlick;
    }

    private void OnFlick(object sender, EventArgs e)
    {
        // Implement the Flick

    }
}

最佳答案 访问FlickGesture信息可以这样做:

void OnFlick(object sender, System.EventArgs e)
{
    var gesture = sender as FlickGesture;

    float distanceFromCamera = Vector3.Distance(transform.position, Camera.main.transform.position);

    Vector3 wp1 = new Vector3(gesture.PreviousScreenPosition.x, gesture.PreviousScreenPosition.y, distanceFromCamera);
    wp1 = Camera.main.ScreenToWorldPoint(wp1);

    Vector3 wp2 = new Vector3(gesture.ScreenPosition.x, gesture.ScreenPosition.y, distanceFromCamera);
    wp2 = Camera.main.ScreenToWorldPoint(wp2);

    Vector3 velocity = (wp2 - wp1) / gesture.FlickTime;

    rigidbody.AddForce(velocity, ForceMode.VelocityChange);
}

通过获取手势的开始和结束位置并将这些位置转换为世界空间,然后应用具有经过的手势时间的缩放导致被轻弹的对象的世界空间速度.

我希望这能让你顺利上路.

点赞