【发布时间】:2020-09-24 00:48:07
【问题描述】:
所以我一次只想在屏幕上显示一行。当球碰撞或绘制另一条线时,我想破坏当前线。到目前为止,这是我的代码。它创建了一个带有两个点的线渲染器,添加了一个 Box 碰撞器。我试过做一个循环并将向量点设置回零。
每次我添加 OnCollisonEnter2D 时,我都无法让 Line 记录它击中了一个球。在 Ball 脚本中,当 Ball 撞到对撞机时,我可以让 Log 打印 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!");
}
}
}
【问题讨论】:
-
嗨,Lunchbox,欢迎来到 Stack Overflow。 Stack Overflow 是针对有关技术和应用的基于知识的问题。您的问题是实施性的,并没有为社区提供任何具体的见解和知识。读起来好像这是一个家庭作业问题。我建议您专注于基本的编码技术,并使用调试器逐步检查您的代码,以了解它为什么不能像您一样工作。另外,请不要用大字体格式化您的问题。使用标准字体。吉姆博
标签: c# unity3d collision-detection