0

所以我想在统一 2D 中制作某种按钮(有或没有 UI),它会产生一个“塔”,然后我将它拖到所需的位置。

脚本拖动脚本如下:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(BoxCollider2D))]

public class DragEnemy : MonoBehaviour
{
    private Vector3 screenPoint;
    private Vector3 offset;

    void OnMouseDown()
    {

        offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
    }

    void OnMouseDrag()
    {
        Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
        Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
        transform.position = curPosition;
    }
}

通常我有预先确定的位置来放置我的塔,但我想把它们做成按钮来给玩家更多的控制权。我正在关注一个教程,所以如果它有助于找出代码。

http://www.raywenderlich.com/107525/create-tower-defense-game-unity-part-1

我要做的是制作一个 UI 图像,将图像设置为那些openspots(或我的图像),然后添加 Place Monster 脚本(参见教程中没有空格)和一个圆形对撞机,但它不起作用。我希望它在点击时产生一个怪物,然后我想将怪物拖到它应该射击敌人的地方。然后我将公共游戏对象设置为我想要生成的怪物(在这种情况下是带有拖动脚本的怪物),然后我希望有魔法。

4

0 回答 0