C# 在Unity中使用线条渲染器时,如何销毁线条?
所以我一次只想在屏幕上显示一行。我想在球碰撞或绘制另一条线时破坏当前线。这是到目前为止我的代码。它创建一个具有两个点的线渲染器,并添加一个长方体碰撞器。我试着做一个循环,把向量点设置回零 每次我添加一个C# 在Unity中使用线条渲染器时,如何销毁线条?,c#,unity3d,collision-detection,C#,Unity3d,Collision Detection,所以我一次只想在屏幕上显示一行。我想在球碰撞或绘制另一条线时破坏当前线。这是到目前为止我的代码。它创建一个具有两个点的线渲染器,并添加一个长方体碰撞器。我试着做一个循环,把向量点设置回零 每次我添加一个OnCollisionInter2D时,我都无法让线记录它击中了一个球。在Ball脚本中,我可以获得日志,以便在球撞击碰撞器时打印Hit using System.Runtime.CompilerServices; using UnityEditor.SceneManagement; using
OnCollisionInter2D
时,我都无法让线记录它击中了一个球。在Ball脚本中,我可以获得日志,以便在球撞击碰撞器时打印Hit
using System.Runtime.CompilerServices;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.UI;
public class Player1 : MonoBehaviour
{
private LineRenderer line; // Reference to LineRenderer
private Vector3 mousePos;
public Vector3 startPos; // Start position of line
public Vector3 endPos; // End position of line
void Update()
{
GetLine(); // On mouse down new line will be created
}
private void GetLine()
{
if (Input.GetMouseButtonDown(0))
{
if (line == null)
CreateLine();
SetFirstPos();
}
else if (Input.GetMouseButtonUp(0))
{
if (line)
{
SetSecondPos();
AddColliderToLine();
line = null;
}
}
else if (Input.GetMouseButton(0))
{
if (line)
{
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos.z = 0;
line.SetPosition(1, mousePos);
}
}
}
// Following method creates line runtime using Line Renderer component
private void CreateLine()
{
line = new GameObject("Line").AddComponent<LineRenderer>();
line.positionCount = 2;
line.numCapVertices = 2;
line.startWidth = .25f;
line.endWidth = .25f;
line.startColor = Color.black;
line.endColor = Color.black;
line.useWorldSpace = true;
}
// Following method adds collider to created line
private void AddColliderToLine()
{
BoxCollider2D col = new GameObject("Collider").AddComponent<BoxCollider2D>();
col.transform.parent = line.transform; // Collider is added as child object of line
float lineLength = Vector3.Distance(startPos, endPos);// length of line
lineLength.ToString();
col.size = new Vector3(lineLength, 0.25f, 1f); // size of collider is set where X is length of
line, Y is width of line, Z will be set as per requirement
Vector3 midPoint = (startPos + endPos) / 2;
col.transform.position = midPoint; // setting position of collider object
// Following lines calculate the angle between startPos and endPos
float angle = (Mathf.Abs(startPos.y - endPos.y) / Mathf.Abs(startPos.x - endPos.x));
if ((startPos.y < endPos.y && startPos.x > endPos.x) || (endPos.y < startPos.y && endPos.x >
startPos.x))
{
angle *= -1;
}
angle = Mathf.Rad2Deg * Mathf.Atan(angle);
col.transform.Rotate(0, 0, angle);
}
//Sets the first position
private void SetFirstPos()
{
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos.z = 0;
line.SetPosition(0, mousePos);
startPos = mousePos;
Debug.Log("Line");
}
//Sets the second position
private void SetSecondPos()
{
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos.z = 0;
line.SetPosition(1, mousePos);
endPos = mousePos;
}
//Cannot get it to Log Hit. Line show up in the Hierarchy.
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameobject.name =="Line")
{
Debug.Log("Hit!");
}
}
}
使用System.Runtime.CompilerServices;
使用UnityEditor.SceneManagement;
使用UnityEngine;
使用UnityEngine.UI;
公共类玩家1:单一行为
{
私有LineRenderer line;//对LineRenderer的引用
私人矢量3鼠标;
public Vector3 startPos;//行的起始位置
公共向量3 endPos;//行的结束位置
无效更新()
{
GetLine();//鼠标按下时将创建新行
}
私有void GetLine()
{
if(Input.GetMouseButtonDown(0))
{
如果(行==null)
CreateLine();
SetFirstPos();
}
else if(Input.GetMouseButtonUp(0))
{
如果(行)
{
SetSecondPos();
addColliderOnline();
行=空;
}
}
else if(Input.GetMouseButton(0))
{
如果(行)
{
mousePos=Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos.z=0;
行。设置位置(1,鼠标点);
}
}
}
//下面的方法使用线条渲染器组件创建线条运行时
私有void CreateLine()
{
line=新游戏对象(“line”).AddComponent();
line.positionCount=2;
line.numcapverties=2;
line.startWidth=.25f;
line.endWidth=.25f;
line.startColor=Color.black;
line.endColor=Color.black;
line.useWorldSpace=true;
}
//下面的方法将碰撞器添加到创建的线
私有void addColliderOnline()
{
BoxCollider2D col=新游戏对象(“碰撞器”).AddComponent();
col.transform.parent=line.transform;//将碰撞器添加为line的子对象
float lineLength=Vector3.距离(startPos,endPos);//线的长度
lineLength.ToString();
col.size=new Vector3(线宽,0.25f,1f);//设置碰撞器的大小,其中X是
线条,Y为线条宽度,Z将根据要求设置
向量3中点=(起始点+结束点)/2;
col.transform.position=中点;//设置碰撞器对象的位置
//以下几行计算startPos和endPos之间的角度
浮动角度=(Mathf.Abs(startPos.y-endPos.y)/Mathf.Abs(startPos.x-endPos.x));
如果((startPos.yendPos.x)| |(endPos.y
startPos.x)
{
角度*=-1;
}
角度=Mathf.Rad2Deg*Mathf.Atan(角度);
坐标变换旋转(0,0,角度);
}
//设置第一个位置
私有void SetFirstPos()
{
mousePos=Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos.z=0;
line.SetPosition(0,鼠标点);
startPos=mousePos;
Debug.Log(“行”);
}
//设置第二个位置
私有void SetSecondPos()
{
mousePos=Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos.z=0;
行。设置位置(1,鼠标点);
endPos=鼠标点;
}
//无法使其记录命中率。行显示在层次结构中。
专用空心OnCollisionInter2D(碰撞2D碰撞)
{
如果(collision.gameobject.name==“Line”)
{
Log(“Hit!”);
}
}
}
如果您的OnCollisionInter2D
被调用,为什么不直接使用它呢
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.name =="Line")
{
Debug.Log("Hit!");
// destroy the hit line
GameObject.Destroy(collision.gameObject);
}
}
如果要在创建新线路后销毁该线路,则必须存储该线路并手动销毁:
public class Player1 : MonoBehaviour
{
private GameObject lastLine;
[...]
private void GetLine()
{
if (Input.GetMouseButtonDown(0))
{
if(lastLine != null)
{
GameObject.Destroy(lastLine);
}
if (line == null)
CreateLine();
SetFirstPos();
}
else if (Input.GetMouseButtonUp(0))
{
if (line)
{
SetSecondPos();
AddColliderToLine();
lastLine = line.gameObject;
line = null;
}
}
[...]
}
嗨,午餐盒,欢迎来到Stack Overflow。堆栈溢出用于解决有关技术和应用程序的基于知识的问题。您的问题是实施性的,没有向社区提供任何具体的见解和知识。这看起来像是一个家庭作业问题。我建议你专注于基本的编码技术,并使用调试器一步一步地完成你的代码,理解为什么它不能像你一样工作。此外,请不要用大字体格式化您的问题,请使用标准字体。吉姆博