Привет! От поверхности одного объекта к другому рисуется линия, но когда объекты двигаю линия остаёться неподвижной, а мне нужно чтобы она тянулась вслед за объектом. Подскажите как реализовать? Вот код, рисуется линия и сохраняется в коллекции. Вот как рисуется линия http://prntscr.com/aafxxs
Код:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class UsingGL9 : MonoBehaviour {
private RaycastHit _raycastHit1;
private RaycastHit _raycastHit2;
private Ray _ray1;
private Ray _ray2;
private Vector3 _start;
private Vector3 _end;
private Vector3 _startLocal;
private Vector3 _endLocal;
private Camera _camera;
private bool _drawLine = false;
private List<Line> _line = new List<Line>();
public Material LineMaterial;
// Use this for initialization
void Start()
{
_camera = Camera.main;
}
// Update is called once per frame
void Update()
{
//returns true only once per left mouse button click
//will not be called multiple times while button is pressed
if (Input.GetMouseButtonDown(0))
{
//cast ray
_ray1 = Camera.main.ScreenPointToRay(Input.mousePosition);
var rayHitTheObject1 = Physics.Raycast(_ray1, out _raycastHit1, 100);
//was ray hit the object?
if (rayHitTheObject1)
{
_drawLine = true;
//if it did, remember coordinates of a hit point. Do this by saving coordinate to "start" variable.
_start = _raycastHit1.point;
}
Debug.Log(string.Format("Left button down,Hit!{0},{1},{2}", _start.x, _start.y, _start.z));
}
//returns true while left mouse button is pressed
//code inside a body of this "if" statement will be called multiple times
//once per each frame while player holds the mouse button down
//if (Input.GetButton("Fire1"))
if (Input.GetButton("Fire1"))
{
//cast ray
_ray2 = Camera.main.ScreenPointToRay(Input.mousePosition);
var rayHitTheObject2 = Physics.Raycast(_ray2, out _raycastHit2, 100);
if (rayHitTheObject2)
{
//reassign second variable "End" with the value of ray hit coordinate.
_end = _raycastHit2.point;
Debug.Log(string.Format("Left button hold, Fire1Hit!{0},{1},{2}", _end.x, _end.y, _end.z));
}
}
if (Input.GetMouseButtonUp(0))
{
//todo: transform start and end points to local coordinates of a body
_line.Add(new Line { Start = _start, End = _end });
Debug.Log("Left button up");
}
}
void OnPostRender()
{
if (_drawLine)
{
//draw line from point "start" to point "end"
LineDraw(_start, _end, LineMaterial, _camera);
foreach (var line in _line)
{
LineDraw(line.Start, line.End, LineMaterial, _camera);
}
}
}
static void LineDraw(Vector3 start, Vector3 end, Material material, Camera camera)
{
GL.PushMatrix();
GL.LoadOrtho();
GL.Begin(GL.LINES);
material.SetPass(0);
GL.Color(Color.red);
GL.LoadProjectionMatrix(camera.projectionMatrix);
GL.modelview = camera.worldToCameraMatrix;
GL.Vertex(start);
GL.Vertex(end);
GL.End();
GL.PopMatrix();
}
}
class Line
{
public Vector3 Start;
public Vector3 End;
}