首页 > 其他分享 >Unity游戏开发——画出有物理效果的线

Unity游戏开发——画出有物理效果的线

时间:2022-12-28 14:25:51浏览次数:35  
标签:游戏 currentLine 画出 transform private Unity currentCollierObject currentLineRender

using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Starry
{
    public class DrawLine : MonoBehaviour
    {
        public Material line;
        public bool gravity;
        public Color lineColor = Color.white;
        
        private List<GameObject> lineList = new List<GameObject>();
        private List<Vector2> pointList = new List<Vector2>();
        private GameObject currentLine;
        private LineRenderer currentLineRenderer;

        private bool stopHolding;
        private static readonly int EmissionColor = Shader.PropertyToID("_EmissionColor");

        private void Update()
        {
            if (Input.GetMouseButtonDown(0))
            {
                pointList.Clear();
                CreateLine();
            }

            if (Input.GetMouseButton(0))
            {
                Vector2 item = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                if (!pointList.Contains(item))
                {
                    pointList.Add(item);
                    currentLineRenderer.positionCount = pointList.Count;
                    currentLineRenderer.SetPosition(pointList.Count - 1, pointList.Last());
                    if (pointList.Count >= 2)
                    {
                        Vector2 vector1 = pointList[pointList.Count - 2];
                        Vector2 vector2 = pointList[pointList.Count - 1];

                        GameObject currentCollierObject = new GameObject("Collider");
                        currentCollierObject.transform.position = (vector1 + vector2) / 2f;
                        currentCollierObject.transform.right = (vector2 - vector1).normalized;
                        currentCollierObject.transform.parent = currentLine.transform;
                        BoxCollider2D currentBoxCollider2D = currentCollierObject.AddComponent<BoxCollider2D>();
                        currentBoxCollider2D.size = new Vector3((vector2 - vector1).magnitude, 0.1f, 0.1f);
                        currentBoxCollider2D.enabled = false;
                    }
                }
            }
            
            if (Input.GetMouseButtonUp(0))
            {
                if (currentLine.transform.childCount > 0)
                {
                    for (int i = 0; i < currentLine.transform.childCount; i++)
                    {
                        currentLine.transform.GetChild(i).GetComponent<BoxCollider2D>().enabled = true;
                    }

                    lineList.Add(currentLine);

                    if (gravity)
                    {
                        currentLine.AddComponent<Rigidbody2D>().useAutoMass = true;
                    }
                }
                else
                {
                    Destroy(currentLine);
                }
            }
        }

        private void CreateLine()
        {
            currentLine = new GameObject("Line");
            currentLineRenderer = currentLine.AddComponent<LineRenderer>();
            currentLineRenderer.material = line;
            currentLineRenderer.material.EnableKeyword("_EMISSION");
            currentLineRenderer.material.SetColor(EmissionColor, this.lineColor);
            currentLineRenderer.positionCount = 0;
            currentLineRenderer.startWidth = 0.1f;
            currentLineRenderer.endWidth = 0.1f;
            currentLineRenderer.startColor = lineColor;
            currentLineRenderer.endColor = lineColor;
            currentLineRenderer.useWorldSpace = false;
        }

        public void ClearAll()
        {
            foreach (var obj in lineList)
            {
                Destroy(obj);
            }
            lineList.Clear();
        }
    }
}

 

标签:游戏,currentLine,画出,transform,private,Unity,currentCollierObject,currentLineRender
From: https://www.cnblogs.com/ld839216579/p/17010024.html

相关文章

  • 云游戏的2022:破局、新生、元宇宙
    文|智能相对论作者|青月如果说2021年是「元宇宙元年」,那么2022年更像是元宇宙的「祛魅之年」,在这一年里,原本处在狂奔状态下的元宇宙正在褪去虚火。在这样的大环境下,由于在实......
  • 不局限大平台,如何拓宽小游戏分发渠道?
    9月份《羊了个羊》的突然爆火,让已经四岁的微信小游戏再次引起了大众的广泛关注。相比手游与端游,小游戏以超级应用(微信、抖音、百度等)作为载体,具备无需下载、点开即玩等优点,......
  • Android系统,怎么在自有App中引入小游戏?
    之前有跟大家分享过ios系统上引入FinClipSDK,并将小程序游戏运行到自有App中,这周就继续分享如何在Android系统中引入FinClipSDK。​​​​实现效果:在自有App中实现小程......
  • 干货|小游戏赛道如何做到成功变现?
    伴随着2022年微信小游戏的不断发展,据统计,目前阶段微信小游戏的开发者数量已经高达了十万多人。尤其是在小游戏爆火社交平台的不断出现的背景下,小游戏的发展劲头更是强盛。从......
  • 【木棉花】基于JAVA UI开发的小游戏——推箱子(上)
    前言在上期文章中,分享了关于项目的效果预览图,从这一期开始,将逐步分享这个项目的构建流程。实际上,笔者在进行开发的过程中,并不是写完一个界面的内部逻辑,就开始对界面进行美......
  • 本溪丹东游戏网站高防物理机租用
    下面说的香港服务器,主要是指web服务器,也可以叫做专用的服务器,主要是网站业务应用提供的服务器服务。这类服务器是可以向发出的浏览器提供文档的程序。这类服务器算起来算是......
  • 本溪丹东游戏网站高防BGP物理机
    些用户来说不愿租用,更愿意购买,然后进行托管,或是自建机房进行放置,那有什么好处呢?1.只有自己使用,而不会像租用那样,是常见的资源共享。2.可以见到,然后享有产权。用户可以真......
  • Unity 检测FPS工具
    检测FPS工具publicclassFPS:MonoBehaviour{publicfloatf_UpdateInterval=0.5F;privatefloatf_LastInterval;privatein......
  • 「反Nim」数列游戏
    本题为12月27日22寒假集训每日一题题解题目来源:(未知)题面题目描述cad和jyx最近迷上了一款名为插入数列的游戏,有一个n行m列的网格,你每次可以按下1个或多个格子,但必......
  • 参加MVP OpenDay 和2015 MVP Community Camp社区大课堂
    微软MVPOpenday1月30日在北京召开,到时全国上百位MVP专家将齐聚北京。当然还有亚太的其他国家地区的MVP也会来北京,1月31日微软MVP项目组主办的年度微软技术社区分享大......