0

I'm using this code here (very simple). It draws a line:

Create a new scene, Create a cylinder, Scale the cylinder to (0.005, 1.0, 0.005), Put the following script on the cylinder

#pragma strict
var pos1 : Vector3;
var pos2 : Vector3;
var objectHeight = 2.0; // 2.0 for a cylinder, 1.0 for a cube
function Update () {
if (Input.GetMouseButtonDown(0)) {
pos1 = Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5);
pos1 = Camera.main.ScreenToWorldPoint(pos1);
pos2 = pos1;
}
if (Input.GetMouseButton(0)) {
pos2 = Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5);
pos2 = Camera.main.ScreenToWorldPoint(pos2);
}
if (pos2 != pos1) {
var v3 = pos2 - pos1;
transform.position = pos1 + (v3) / 2.0;
transform.localScale.y = v3.magnitude/objectHeight;
transform.rotation = Quaternion.FromToRotation(Vector3.up, v3);
}
}

When I move the mouse fast, the line lags. It also lags on the smartphone. I put that code in OnGUI, because I read somewhere, that it is faster than Update. It's still lagging.

I write a game. Is there a way to speed up the Line drawing. I mean like hardware accelerated = true? Or I'm on the wrong way?

I know, that there are some 2d linedrawing codes and linerenderer. But I want to make it like above. I want to stretch an object.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Johnny
  • 612
  • 3
  • 13
  • 32
  • Where's the line drawn? What do you mean by "lag"? Is the framerate actually dropping, or do you simply refer to the fact that touch/mouse may have moved a large distance between two calls to Update and you simply end up with an "edgy" line? In the latter case you need to implement line smoothing, there's nothing that fixes this issue automagically. – CodeSmile Oct 23 '14 at 14:22
  • Yes, I meant "edgy" line. Sorry. Hmm.. is there something hardwareAccelerated = true -> like in android? – Johnny Oct 23 '14 at 15:05
  • Nope, this has nothing to do with accelerated drawing. The problem is in points generated. One touch may be at 10x10 and the next at 300x300, so a great distance away. You can only fix this adding in-between points at certain distances and apply a line-smoothing algorithm. – CodeSmile Oct 23 '14 at 18:03

0 Answers0