在Unity中通過動態(tài)畫線來實現(xiàn)碰撞(2D)

之前在很多2D游戲中,發(fā)現(xiàn)有些小游戲可以通過手指動態(tài)在屏幕中繪制線來阻擋小球碰撞。好奇實現(xiàn)方法,所以自己學(xué)習(xí)了一下,效果還行。

參考鏈接:https://www.bilibili.com/video/BV1Et41177to?from=search&seid=6793552372166214965

主要用到的組件是Line Render、dege ColliderBox2D


這是實現(xiàn)的效果

1、具體實現(xiàn)邏輯是,通過獲取手指在屏幕上的坐標并轉(zhuǎn)換成世界坐標,賦值給LineRender組件來生成mesh線

2、將賦值給LineRender的點數(shù)據(jù)記錄到List中,在繪制mesh線完成后。將數(shù)據(jù)全部賦值給degeColliderBox2D碰撞體

3、關(guān)于設(shè)置一些其他的參數(shù)startWidth 、endWidth 設(shè)置線寬

????????????lr = newLine.GetComponent<LineRenderer>();

? ? ? ? ? ? lr.startWidth = LineWidth;

? ? ? ? ? ? lr.endWidth = LineWidth;


4、設(shè)置碰撞體大小的參數(shù)edgeRadius?

edgeCollier.edgeRadius = LineWidth / 2;


下面是組件配置截圖和代碼,有興趣的可以研究下。因為很簡單,所以我說的比較籠統(tǒng),如果你看不懂,可以看看我發(fā)的參考鏈接,那個是視頻比較詳細,一步步教你怎么做


組件配置

源碼:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class DrawColliderLine : MonoBehaviour

{

? ? public GameObject line;

? ? public GameObject Knob;

? ? // 線寬

? ? private const float LineWidth = 0.1f;

? ? private LineRenderer lr;

? ? private EdgeCollider2D edgeCollier;

? ? private int index;

? ? private Vector3 upPoint = Vector3.zero;

? ? private List<Vector2> colliderPos = new List<Vector2>();

? ? void Start()

? ? {


? ? }


? ? void Update()

? ? {

? ? ? ? // 鼠標按下創(chuàng)建一條線的obj

? ? ? ? if(Input.GetMouseButtonDown(0))

? ? ? ? {

? ? ? ? ? ? // 清理上一條線的是碰撞盒數(shù)據(jù)

? ? ? ? ? ? colliderPos.Clear();

? ? ? ? ? ? GameObject newLine = Instantiate(line, line.transform.position, Quaternion.identity).gameObject;

? ? ? ? ? ? newLine.transform.SetParent(this.transform);

? ? ? ? ? ? edgeCollier = newLine.GetComponent<EdgeCollider2D>();

? ? ? ? ? ? lr = newLine.GetComponent<LineRenderer>();

? ? ? ? ? ? lr.startWidth = LineWidth;

? ? ? ? ? ? lr.endWidth = LineWidth;

? ? ? ? ? ? index = 0;

? ? ? ? }

? ? ? ? // 鼠標按住不放,根據(jù)鼠標位置添加點位置

? ? ? ? if (Input.GetMouseButton(0))

? ? ? ? {

? ? ? ? ? ? Vector3 point = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1));

? ? ? ? ? ? if (lr != null)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? if(upPoint != point)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? // 設(shè)置LineRender點數(shù)量和位置

? ? ? ? ? ? ? ? ? ? index++;

? ? ? ? ? ? ? ? ? ? lr.positionCount = index;

? ? ? ? ? ? ? ? ? ? lr.SetPosition(index - 1, point);

? ? ? ? ? ? ? ? ? ? // 記錄需要碰撞的位置

? ? ? ? ? ? ? ? ? ? colliderPos.Add(new Vector2(point.x, point.y));

? ? ? ? ? ? ? ? ? ? upPoint = point;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? // 鼠標抬起,結(jié)束畫線,并根據(jù)數(shù)據(jù)生成碰撞盒

? ? ? ? if (Input.GetMouseButtonUp(0))

? ? ? ? {

? ? ? ? ? ? // 設(shè)置LineRender點碰撞和位置

? ? ? ? ? ? edgeCollier.points = colliderPos.ToArray();

? ? ? ? ? ? edgeCollier.edgeRadius = LineWidth / 2;

? ? ? ? ? ? Knob.GetComponent<Rigidbody2D>().gravityScale = 1f;

? ? ? ? }

? ? }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容