unity3d 2D版本见缝插针demo

作者: 好怕怕 | 来源:发表于2017-02-13 15:32 被阅读321次

这不是一个完整的,但是实现了难点部分,剩下的就是一些UI表现了,关卡设计也很简单,里面都提供了接口


图片.png
using UnityEngine;
using System.Collections;

public class HeroColtrol : MonoBehaviour
{
    // 圆
    private float TotalAngle = 360f;
    // 圆生产预制件
    public GameObject Bullet;
    // 需要创建的数量,根据数目可控制关卡难度(自己看一下就知道什么意思了)
    public float CreateNumber = 5;
    // 旋转速度
    public float RotateSpeed = 5;

    void Update()
    {
        // 生产圆
        if (Input.GetKeyDown(KeyCode.Space))
        {
            float angle = TotalAngle / CreateNumber;
            for (int i = 1; i <= CreateNumber; i++)
            {
                float curAngle = angle * i;
                GameObject item = CreateItem();
                item.transform.RotateAround(transform.position, Vector3.forward, curAngle);
            }

        }
        // 删除所以对象
        if (Input.GetKeyDown(KeyCode.D))
        {

            for (int i = 0; i < transform.childCount; i++)
            {
                GameObject item = transform.GetChild(i).gameObject;
                Destroy(item);
            }

        }
        // 旋转
        if (transform.childCount > 0)
        {
            transform.Rotate(-Vector3.forward * Time.deltaTime * RotateSpeed);
        }

        // 按下鼠标左键创建对象
        if (Input.GetMouseButtonDown(0))
        {

            // Invoke("CreateItem", 1);
            CreateItem();
        }
    }

    /// <summary>
    /// 创建一个圆
    /// </summary>
    /// <returns></returns>
    private GameObject CreateItem()
    {
        GameObject item = Instantiate(Bullet);
        item.transform.localPosition = new Vector3(0f, -2, 0f);
        item.transform.SetParent(transform);
        item.AddComponent<RotateItem>();
        return item;
    }




}

using UnityEngine;
using System.Collections;


public class RotateItem : MonoBehaviour
{
    private LineRenderer Line;
    void Awake()
    {
        Line = transform.GetComponent<LineRenderer>();
    }

    void FixedUpdate()
    {
        // 设置线的1index对应的坐标
        Line.SetPosition(1, transform.position);
    }
}

图片.png

相关文章

网友评论

    本文标题:unity3d 2D版本见缝插针demo

    本文链接:https://www.haomeiwen.com/subject/ngftwttx.html