我有一个预制件,当用户从我的游戏内商店购买物品时实例化,有多少实例化,所有预制件都有一个特定位置的起始位置 . 使用我在网上找到的this TouchScript包可以在场景中拖动预制件!我的问题:我想播放预制's animation every time the user is dragging the prefab around the screen, I attempted this by creating a RaycastHit2D function that would allow me to detect if the user has clicked on the prefab' s对撞机,脚本如下:
if (Input.GetMouseButtonDown (0)) {
Vector2 worldPoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast (worldPoint, Vector2.zero);
if (hit.collider != null) {
if (this.gameObject.name == "Item5 (1)(Clone)" +item5Increase.i) {
monkeyAnim.SetBool ("draging", true);
Debug.Log (hit.collider.name);
}
} else {
monkeyAnim.SetBool ("draging", false);
}
}
然而,如果我要购买多个预制件,当我开始拖动一个实例化的预制件时,所有实例化的预制件都会播放它的动画,希望我有意义 . 有人能帮我吗?谢谢!
1 回答
我在2D游戏中遇到了与平台类似的问题 . 我建议的解决方案是创建一个
GameObject
,它充当你想要动画的当前项目,以及一个LayerMask
,它充当你的光线投射可以击中的对象的过滤器 . 您可以将此LayerMask
与Physics2D.Raycast API结合使用,它具有以LayerMask
作为参数的重载方法 .首先创建一个新图层,可以通过转到场景中对象的右上角并访问“图层”框来完成 . 一旦你创建了一个新图层(我称之为“item”),请确保正确分配了预制图层 .
然后,在场景中创建一个空对象,并将此脚本附加到该对象 . 在该对象上,您将看到一个下拉菜单,询问您的光线投射应该击中哪些图层 . 将它分配给“item”图层;这可以确保您的光线投射只能击中该层中的对象,因此点击游戏中的任何其他内容都不会产生任何影响 .