Unity2D:拖动预制件时播放实例化预制件的动画
Unity2D: play instantiated prefab's animation when prefab is being dragged
我有一个预制件,当用户从我的游戏内商店购买商品时实例化,实例化了多少,所有预制件都有一个特定位置的起始位置。可以使用我在网上找到的 this TouchScript 包在场景中拖动预制件!我的问题:每次用户在屏幕上拖动预制件时,我都想播放预制件的动画,我通过创建一个 RaycastHit2D 函数来尝试实现这一点,该函数允许我检测用户是否单击了预制件的碰撞器,脚本如下:
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);
}
}
但是,如果我要购买多个预制件,当我开始仅拖动一个实例化预制件时,所有实例化预制件都会播放它的动画,希望我是有道理的。有人能帮我吗?谢谢!
我在 2D 游戏中遇到了类似的平台问题。我建议的解决方案是创建一个 GameObject
作为您希望设置动画的当前项目,以及一个 LayerMask
作为光线投射可以命中的对象的过滤器。您可以将此 LayerMask
与 Physics2D.Raycast
API 结合使用,它具有一个将 LayerMask
作为参数的重载方法。
首先创建一个新层,这可以通过转到场景中对象的右上角并访问 "Layer" 框来完成。一旦你创建了一个新层(我叫我的"item"),确保你的预制层被正确分配。
然后,在您的场景中创建一个空对象,并将此脚本附加到它上面。在该对象上,您将看到一个下拉菜单,询问您的光线投射应该命中哪些层。分配给它 "item" 层;这确保您的光线投射只能击中该层中的对象,因此单击游戏中的任何其他对象都不会产生任何效果。
using UnityEngine;
public class ItemAnimation : MonoBehaviour
{
private GameObject itemToAnimate;
private Animator itemAnim;
[SerializeField]
private LayerMask itemMask;
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
CheckItemAnimations();
}
else if (Input.GetMouseButtonUp(0) && itemToAnimate != null) //reset the GameObject once the user is no longer holding down the mouse
{
itemAnim.SetBool("draging", false);
itemToAnimate = null;
}
}
private void CheckItemAnimations()
{
Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero, 1, itemMask);
if (hit) //if the raycast hit an object in the "item" layer
{
itemToAnimate = hit.collider.gameObject;
itemAnim = itemToAnimate.GetComponent<Animator>();
itemAnim.SetBool("draging", true);
Debug.Log(itemToAnimate.name);
}
else //the raycast didn't make contact with an item
{
return;
}
}
}
我有一个预制件,当用户从我的游戏内商店购买商品时实例化,实例化了多少,所有预制件都有一个特定位置的起始位置。可以使用我在网上找到的 this TouchScript 包在场景中拖动预制件!我的问题:每次用户在屏幕上拖动预制件时,我都想播放预制件的动画,我通过创建一个 RaycastHit2D 函数来尝试实现这一点,该函数允许我检测用户是否单击了预制件的碰撞器,脚本如下:
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);
}
}
但是,如果我要购买多个预制件,当我开始仅拖动一个实例化预制件时,所有实例化预制件都会播放它的动画,希望我是有道理的。有人能帮我吗?谢谢!
我在 2D 游戏中遇到了类似的平台问题。我建议的解决方案是创建一个 GameObject
作为您希望设置动画的当前项目,以及一个 LayerMask
作为光线投射可以命中的对象的过滤器。您可以将此 LayerMask
与 Physics2D.Raycast
API 结合使用,它具有一个将 LayerMask
作为参数的重载方法。
首先创建一个新层,这可以通过转到场景中对象的右上角并访问 "Layer" 框来完成。一旦你创建了一个新层(我叫我的"item"),确保你的预制层被正确分配。
然后,在您的场景中创建一个空对象,并将此脚本附加到它上面。在该对象上,您将看到一个下拉菜单,询问您的光线投射应该命中哪些层。分配给它 "item" 层;这确保您的光线投射只能击中该层中的对象,因此单击游戏中的任何其他对象都不会产生任何效果。
using UnityEngine;
public class ItemAnimation : MonoBehaviour
{
private GameObject itemToAnimate;
private Animator itemAnim;
[SerializeField]
private LayerMask itemMask;
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
CheckItemAnimations();
}
else if (Input.GetMouseButtonUp(0) && itemToAnimate != null) //reset the GameObject once the user is no longer holding down the mouse
{
itemAnim.SetBool("draging", false);
itemToAnimate = null;
}
}
private void CheckItemAnimations()
{
Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero, 1, itemMask);
if (hit) //if the raycast hit an object in the "item" layer
{
itemToAnimate = hit.collider.gameObject;
itemAnim = itemToAnimate.GetComponent<Animator>();
itemAnim.SetBool("draging", true);
Debug.Log(itemToAnimate.name);
}
else //the raycast didn't make contact with an item
{
return;
}
}
}