如何创建一个以端点为鼠标位置但仅面向一个方向的线条渲染器

时间:2016-11-02 10:24:47

标签: c# unity3d line

我有两个对象,一行从一个到另一个,如下所示:

enter image description here

我想创建另一条线,其中一个对象的起始点和鼠标位置的终点,但是仍然面向第二个对象,看起来第一行填充到鼠标所在的位置,就像这样:

enter image description here

在Unity中没有办法(据我所知)设置线条渲染器的方向,那么我该如何实现呢?

连接形状的代码:

 public void AddShape(GameObject shape) {
    selectedShape = shape;
    selectedShapeComp = shape.GetComponent<Shape>();

    if (!selectedShapes.Contains(selectedShape) && (selectedShapes.Count == 0 || selectedShape.CompareTag(selectedShapes[0].tag))) {
        currentAdjacentShapes = selectedShapeComp.GetAdjacentShapes();

        if (selectedShapes.Count == 0 && !selectedShapeComp.key) return; //If it's first and not a key, don't add it
        if (selectedShapes.Count == 0 && selectedShapeComp.key) activeKeys++; //If it's first and is a key, continue

        FindCurrentlyAdjacentShapes();

        if (currentAdjacentShapes != null && !currentAdjacentShapes.Contains(selectedShape)) return; //If it's not adjacent to previous shape, don't add it

        selectedShapes.Add(selectedShape);
        MarkShape();

        if (selectedShapes.Count > 1 && selectedShapeComp.key) MarkLeftOutShapes();

        SelectionLineSpawner.instance.SpawnLine();
    }

    ClearShapesAhead();
    CalculateSelectedShapesAmount();
}

private void RemoveShape(int index) {
    UnmarkShape(selectedShapes[index]);
    selectedShapes.RemoveAt(index);
}

private void ClearShapesAhead() {
    for (int i = selectedShapes.Count; i > selectedShapeComp.selectedPos; i--) {
        RemoveShape(i - 1);
    }
}

private void FindCurrentlyAdjacentShapes() {
    if (selectedShapes.Count > 1) {
        currentAdjacentShapes = selectedShapes[selectedShapes.Count - 1].GetComponent<Shape>().GetAdjacentShapes();
    }
}

private void MarkLeftOutShapes() {
    var shapes = new List<GameObject>();

    switch (selectedShape.tag) {
        case "Diamond":
            shapes = LevelLoader.instance.currentLevel.GetAllDiamonds();
            break;
        case "Triangle":
            shapes = LevelLoader.instance.currentLevel.GetAllTriangles();
            break;
    }

    foreach (var shape in shapes) {
        if (!selectedShapes.Contains(shape)) {
            Debug.Log("Not selected: " + shape.name);
        }
    }
}

private void MarkShape() {
    selectedShape.gameObject.GetComponent<SpriteRenderer>().color = Color.black;
    selectedShape.gameObject.GetComponent<Animator>().SetTrigger("select");
    selectedShapeComp.selected = true;
    selectedShapeComp.selectedPos = selectedShapes.Count;
}

private void UnmarkShape(GameObject shape) {
    var shapeComp = shape.GetComponent<Shape>();
    shapeComp.selected = true;
    shapeComp.selectedPos = 0;
    shape.gameObject.GetComponent<SpriteRenderer>().color = Color.white;
}

private void CalculateSelectedShapesAmount() {
    switch (selectedShape.tag) {
        case "Diamond":
            LevelLoader.instance.currentLevel.selectedDiamonds = selectedShapes.Count;
            break;
        case "Triangle":
            LevelLoader.instance.currentLevel.selectedTriangles = selectedShapes.Count;
            break;
    }
}

private void DrawLines() {
    foreach (var shape in LevelLoader.instance.currentLevel.GetAllShapes()) {
        var adjacentShapes = shape.GetComponent<Shape>().GetAdjacentShapes();

        foreach (var adjacentShape in adjacentShapes) {
            var lineClone = Instantiate(line);
            lineClone.transform.parent = shape.transform;
            var lineComp = lineClone.GetComponent<Line>();

            foreach (Transform child in adjacentShape.transform) {
                if (child.CompareTag("Line"))
                    if (child.GetComponent<Line>().origin != adjacentShape)
                        lineComp.DrawLine(shape, adjacentShape);
            }

            if (!lineComp.IsDrawn())
                Destroy(lineClone);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我的猜测是你必须通过使用数学而更精确地vector projection来实现这一点。关注this picture您可以轻松地将 b 向量移动,因为您的行从一个对象移动到另一个对象,您可以使用某些对象获取 a 向量比如new Vector3(Input.mouse.position - basePositionOfYourLine),然后计算 a1 向量。