简体   繁体   English

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

[英]Unity C# TouchScript - Moving an object to direction of Flick

I'm trying to get an object to read the flick (using the Touchscript package on Unity) - then flick the object which the script is attached to in the direction. 我正在尝试获取一个对象来读取轻弹(使用Unity上的Touchscript包) - 然后在方向上轻弹脚本附加的对象。

It definitely reads the flick, but I have no idea how to read/obtain the direction to add the force onto the rigidbody/translate it in the direction. 它肯定会读取轻弹,但我不知道如何读取/获取方向以将力添加到刚体上/将其平移到方向上。 I looked at the references such as ScreenFlickVector & GestureDirection with no luck. 我查看了ScreenFlickVector和GestureDirection这样的引用,没有运气。 I would love your help! 我很乐意帮助你!

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

    }
}

Accessing the FlickGesture info can be done like this: 访问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);
}

By taking the start and end positions of the gesture and converting those positions into world space, then applying a scaling with the elapsed gesture time results in the world space velocity of the object being flicked. 通过获取手势的开始和结束位置并将这些位置转换为世界空间,然后应用具有经过的手势时间的缩放导致被轻弹的对象的世界空间速度。

I hope this gets you on your way. 我希望这能让你顺利上路。

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

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