我试图让对象读取轻弹(使用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
}
}
答案 0 :(得分:2)
访问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);
}
通过获取手势的开始和结束位置并将这些位置转换为世界空间,然后应用经过手势时间的缩放,可以使对象的世界空间速度被轻弹。
我希望这能让你顺利上路。