简体   繁体   English

Unity2D:拖动预制件时播放实例化的预制件动画

[英]Unity2D: play instantiated prefab's animation when prefab is being dragged

I have a prefab that is instantiated when the user buys the item from my In-game store, how ever many is instantiated, the all of prefab has a start position of a certain position. 我有一个预制件,当用户从我的游戏内商店购买物品时被实例化,实例化了多少,所有预制件都具有某个特定位置的开始位置。 The prefab can be dragged around the scene using this TouchScript package I found online! 预制可以在场景中使用被拖进这个 TouchScript包我在网上找到! My issue: I want to play the prefab'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 collider, script below: 我的问题:我想在用户每次在屏幕上拖动预制件时播放预制件的动画,我通过创建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);
        }
    }

However if I were to buy more than one prefab, all instantiated prefabs will play it's animation when I start to drag only one of the instantiated prefabs, hope I'm making sense. 但是,如果我要购买多个预制件,那么当我开始只拖动其中一个实例化预制件时,所有实例化预制件都将播放其动画,希望我能理解。 Can some one help me? 有人能帮我吗? Thank you! 谢谢!

I faced a similar issue with platforms in my 2D game. 我的2D游戏平台也遇到了类似的问题。 The solution I would suggest is to create a GameObject that acts as the current item you wish to animate, and a LayerMask that acts as a filter for which objects your raycast can hit. 我建议的解决方案是创建一个GameObject充当你希望动画当前项目,以及LayerMask充当哪些对象的光线投射可以打一个过滤器。 You can use this LayerMask in conjunction with the Physics2D.Raycast API , which has an overload method that takes a LayerMask as a parameter. 您可以将此LayerMaskPhysics2D.Raycast API结合使用,该API具有使用LayerMask作为参数的重载方法。

Start by creating a new layer, which can be done by going to the top right of an object in your scene and accessing the "Layer" box. 首先创建一个新图层,方法是转到场景中对象的右上角,然后访问“图层”框。 Once you've created a new layer (I called mine "item"), make sure your prefab's layer is assigned correctly. 一旦创建了一个新层(我称为我的“ item”),请确保正确分配了预制层。

Then, create an empty object in your scene, and attach this script to it. 然后,在场景中创建一个空对象,并将此脚本附加到该对象。 On that object you will see a dropdown menu that asks which layers your raycast should hit. 在该对象上,您将看到一个下拉菜单,询问您的光线投射应击中哪一层。 Assign it the "item" layer; 给它分配“ item”层; this ensures that your raycast can only hit objects in that layer, so clicking on anything else in your game will produce no effect. 这样可以确保您的光线投射只能命中该层中的对象,因此单击游戏中的其他任何内容都不会产生效果。

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;
        }
    }
}

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

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