【发布时间】:2019-07-29 03:24:22
【问题描述】:
我正在尝试在 Unity 2D 上创建一个水果忍者风格的游戏,并且我想创建一条跟随玩家“切割”的路径。每次用户拖动时,我都尝试实例化一个包含线条渲染器的“剪切”对象。但是,线渲染器没有出现。任何人都可以更正任何错误或提出新方法吗?
public class CreateCuts : MonoBehaviour
{
public GameObject cut;
public float cutDestroyTime;
private bool dragging = false;
private Vector2 swipeStart;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
dragging = true;
swipeStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
else if (Input.GetMouseButtonUp(0) && dragging)
{
createCut();
}
}
private void createCut()
{
this.dragging = false;
Vector2 swipeEnd = Camera.main.ScreenToWorldPoint(Input.mousePosition);
GameObject cut = Instantiate(this.cut, this.swipeStart, Quaternion.identity) as GameObject;
cut.GetComponent<LineRenderer>().positionCount = 1 ;
cut.GetComponent<LineRenderer>().enabled = true;
cut.GetComponent<LineRenderer>().SetPosition(0, this.swipeStart);
cut.GetComponent<LineRenderer>().SetPosition(1, swipeEnd);
Vector2[] colliderPoints = new Vector2[2];
colliderPoints[0] = new Vector2(0.0f, 0.0f);
colliderPoints[1] = swipeEnd - swipeStart;
cut.GetComponent<EdgeCollider2D>().points = colliderPoints;
Destroy(cut.gameObject, this.cutDestroyTime);
}
}
我希望会有一条线,但什么也没有出现。还有一条警告指出 SetPosition(1, swipeEnd) 超出范围。
编辑:这是我剪切对象的设置
1st part of cut object settings
【问题讨论】:
-
positionCount = 1一行只有1分? -
要不要改成2?
-
2 代表 1 行,3 代表 2 行,依此类推。
-
我将位置计数更改为 2,但该行仍然没有显示。
-
另一种选择是创建一个粒子对象,所以当你的剪切对象移动时,它会随之移动,你会有一种轨迹的效果。