I have two objects with a line going from one to the other, like this:
I want to create another line with a starting point one of the objects and an end point the mouse position, however still facing the second object, which will look like the first line is filling up to where the mouse is, like this:
There is no way (as far as I know) to set a direction of a line renderer in Unity, so how can I achieve this?
Code for connecting the shapes:
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);
}
}
}