Hello,
I’m trying to shorten a line renderer, and I can’t for the life of me figure it out! I’m sure it’s an easy solution.
Basically, I have two line renderers, one coming from the player (which is a ball), if the player is facing another ball, the first line renderer is drawn between the player, and the other ball. A second line renderer is then draw from the other ball to wherever the balls force will go if I were to hit it… Pretty much like an aiming system you see in pool/billiard games.
All I want to do is shorten the second line renderer. Below is the code I’m using to calculate the second line direction:
var ballRadius = 0.49;
var contactPointSize = 0.1;
var rayLength : float = 30;
var direction = player.transform.TransformDirection(-Vector3.forward);
var hit : RaycastHit;
Physics.Raycast(transform.position, direction, hit, rayLength);
var newDirection = new Vector3();
newDirection = Vector3(hit.normal.x, hit.normal.y, hit.normal.z);
newDirection *= -1;
var hitPos = hit.collider.transform.position;
lineDirection.SetPosition(0, hit.collider.transform.position);
var deflection = Vector3((newDirection.x * rayLength), hitPos.y, (newDirection.z * rayLength));
var newDeflec = (hitPos + deflection) / 2;
lineDirection.SetPosition(1, deflection);
So lineDirection is the second line renderer, I want to always have its length at 5. I tried playing around with the maths, by adding hitPos and deflection, and then dividing by 2. That works fine - I get the middle of the raycast, but then when I try dividing it further, the coordinates go all messed up.
Also, I cant decrease the size the in ‘rayLength’ as then my Physics.Raycast wont shoot very far.
Any advice? Thanks.
PS, this is pretty much a repeat of a question I asked months ago, but never solved it:
http://answers.unity3d.com/questions/485577/linerenderer-setting-max-distance.html

Isn't it the example in the [docs][1]? I've only had a glance but it seems like you need to lay down multiple sections of unit length line to the size you want. [1]: http://docs.unity3d.com/Documentation/ScriptReference/LineRenderer.SetPosition.html
– meat5000Unfortunately the info within the docs isnt what I am looking for, it doesn't show me how to shorten the line - my problem is more of a mathematical issue I guess, rather than the functionality behind the line renderer itself. Thanks anyway
– oliver-jonesI'm not so sure. The docs set line length by simply adding in more vertex points, I believe. Otherwise the 'sine wave curve' would be some spirograph mess :) I'm assuming the LineRenderer simply draw from a point to the next assigned vertex. If you want to make it shorter, assign a new vertex. Of course, I could be wrong... I've never used the LineRenderer, I'm just looking at the example.
– meat5000Will it only have two points? If that is the case, isn´t it enough to normalize like this deflection = 5*deflection.normalized; Or did I misunderstood your question?
– MrVerdouxYes, you are right, I missed that!
– MrVerdoux