我试图获取一个对象来读取动画(使用Unity上的Touchscript包)-然后将脚本附加到该对象的对象朝该方向滑动。
它肯定读过轻弹,但是我不知道如何读取/获得方向以将力添加到刚体上/沿该方向平移。我没有运气地看着诸如ScreenFlickVector和GestureDirection之类的引用。希望您的帮助!
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);
}
通过获取手势的开始和结束位置并将这些位置转换为世界空间,然后对经过的手势时间应用缩放,将导致对象的世界空间速度被轻拂。
希望这能帮助您。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句