我想知道如何绘制包含对撞机的线。我正在尝试做类似的事情:
我遵循了此网站教程: https://unitycoder.com/blog/2017/09/23/drawing-2d-lines-and-adding-collider-to-it/
但是它没有按我预期的那样工作,正在绘制线条,但是应该与线条碰撞的对象却没有碰撞。
我需要更改我的代码吗?
代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DrawLine2D : MonoBehaviour
{
[SerializeField]
protected LineRenderer m_LineRenderer;
[SerializeField]
protected bool m_AddCollider = false;
[SerializeField]
protected EdgeCollider2D m_EdgeCollider2D;
[SerializeField]
protected Camera m_Camera;
protected List<Vector2> m_Points;
protected virtual void Awake ()
{
if ( m_Camera == null ) {
m_Camera = Camera.main;
}
m_Points = new List<Vector2> ();
}
protected virtual void Update ()
{
if ( Input.GetMouseButtonDown ( 0 ) )
{
Reset ();
}
if ( Input.GetMouseButton ( 0 ) )
{
Vector2 mousePosition = m_Camera.ScreenToWorldPoint ( Input.mousePosition );
if ( !m_Points.Contains ( mousePosition ) )
{
m_Points.Add ( mousePosition );
m_LineRenderer.positionCount = m_Points.Count;
m_LineRenderer.SetPosition ( m_LineRenderer.positionCount - 1, mousePosition );
if ( m_EdgeCollider2D != null && m_AddCollider && m_Points.Count > 1 )
{
m_EdgeCollider2D.points = m_Points.ToArray ();
}
}
}
}
protected virtual void Reset ()
{
if ( m_LineRenderer != null )
{
m_LineRenderer.positionCount = 0;
}
if ( m_Points != null )
{
m_Points.Clear ();
}
if ( m_EdgeCollider2D != null && m_AddCollider )
{
m_EdgeCollider2D.Reset ();
}
}
}