找回密码
 立即注册
查看: 332|回复: 7

[笔记] 20行代码,Unity中的“神笔马良”

[复制链接]
发表于 2022-2-22 08:52 | 显示全部楼层 |阅读模式
0.效果



1.代码

using UnityEngine;

public class GameObjectBrush : MonoBehaviour
{
    [SerializeField] private float width = 0.1f;
    [SerializeField] private Color color = Color.grey;

    private LineRenderer currentLR;
    private Vector2 previousPoint;

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            //线条渲染
            currentLR = new GameObject("LineRenderer").AddComponent<LineRenderer>();
            currentLR.material = new Material(Shader.Find("Sprites/Default")) { color = color };
            currentLR.widthMultiplier = width;
            currentLR.useWorldSpace = false;
            currentLR.positionCount = 1;
            currentLR.SetPosition(0, (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition));

            //更新数据
            previousPoint = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);
        }

        else if (Input.GetMouseButton(0))
        {
            if (previousPoint != (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition))
            {
                //线条渲染
                currentLR.positionCount++;
                currentLR.SetPosition(currentLR.positionCount - 1, (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition));

                //碰撞器
                BoxCollider2D collider = new GameObject("BoxCollider2D").AddComponent<BoxCollider2D>();
                collider.transform.parent = currentLR.transform;
                Vector2 latestPoint = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);
                collider.transform.position = (previousPoint + latestPoint) * 0.5f;
                float angle = Mathf.Atan2((latestPoint - previousPoint).y, (latestPoint - previousPoint).x) * Mathf.Rad2Deg;
                collider.transform.eulerAngles = new Vector3(0, 0, angle);
                collider.size = new Vector2(Vector2.Distance(latestPoint, previousPoint), width);

                //更新数据
                previousPoint = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);
            }
        }

        else if (Input.GetMouseButtonUp(0))
        {
            if (currentLR.transform.childCount > 0)
            {
                currentLR.gameObject.AddComponent<Rigidbody2D>().useAutoMass = true;
            }
        }
    }
}
2.一些要点

1.为什么不选择EdgeCollider2D,它似乎更加方便?

对于LineRenderer添加碰撞来说,EdgeCollider2D确实比BoxCollider2D看起来更加方便,因为只要同步两个组件的Points数组,再加厚EdgeCollider2D的边缘半径,就立刻可以实现线条的碰撞效果,而不用再自己去用Atan和开平方根计算每个线段碰撞盒的角度与尺寸。EdgeCollider2D完全可以实现碰撞的需求,但是它却不能实现刚体的重心(Center Of Mass)需求。EdgeCollider2D本质上还是一个线条碰撞器,没有形状,所以不参与刚体的重心计算,当EdgeCollider2D附加到刚体时,刚体的重心永远是[0,0,0],这样的结果就造成了物体错误的重心位置,物理模拟失真。而附加了BoxCollider2D的刚体,刚体会依据BoxCollider2D的布局计算一个最符合的重心位置,实现一个较为拟真的物理物体。
2.为什么刚体要使用自动质量?

首先密度的计算公式为: ,所以刚体的质量就是 ,也就是说如果BoxCollider2D的面积尺寸为2并且密度为2,那么这个附加了BoxCollider2D的刚体质量就是4,依此类推。基于此,我们就可以使用自动质量,实现一个较为拟真的物理结构,也就是说我们用画笔着墨越多的地方,BoxCollider2D的面积就越大,它的质量也越大,着墨越少的地方,BoxCollider2D的面积越小,它的质量也越小。这样就让下方这种结构的物体的物理模拟成为可能:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
发表于 2022-2-22 08:53 | 显示全部楼层
麦克斯与魔法画笔!
发表于 2022-2-22 08:57 | 显示全部楼层
效果同小时候玩的单机游戏《蜡笔物理学》,至今印象深刻
发表于 2022-2-22 09:05 | 显示全部楼层
还要设置什么吗,脚本直接用,虽然生成了物体,但是没有显示出来
发表于 2022-2-22 09:12 | 显示全部楼层
找到了一个小问题,不能直接用Input.mousePosition,要不然第二个人循环进不去,所以画不出来。
发表于 2022-2-22 09:21 | 显示全部楼层
经典收藏比赞多
发表于 2022-2-22 09:22 | 显示全部楼层
好文章,提供了思路[赞同]
发表于 2022-2-22 09:27 | 显示全部楼层
没问题,是因为他用的正交摄像机
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-4-28 16:36 , Processed in 0.095465 second(s), 27 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表