- Home /
How to use Trailrenderer.Getpositions?
Hello! I want to use Trailrenderer.Getpositions function. But I'm not good at Unity and C# so really confused about using that function.
How can I use Trailrenderer.Getpositions properly?
Or is there any function that can find trails vertex position?
thx!
Answer by Zarenityx · Sep 05, 2018 at 10:17 PM
TrailRenderer.GetPositions returns an int and has an out parameter of type Vector3[].
The int is just how many vertices you have.
The Vector3[] is the array of vertices themselves, and it what you want.
If you haven't used the out
keyword before, it's a write-only version of ref
, and is essentially used as a second return statement. Physics.Raycast uses out
as well to return data about the hit point. You may have also run across it's cousin ref
. Unlike ref
, however, out
is write-only and does not require that the object it references is initialized. It is guaranteed to be set after the function call if no exception occurs, just like a return statement. You use it like this:
TrailRenderer render = GetComponent<TrailRenderer>(); //Or however you want to do it.
Vector3[] positions; //No need to initialize the array. Out does this for us.
render.GetPositions(positions); //This function returns an int, but we don't really need it, since we have positions.Length.
//positions now contains the vertices, and you can do stuff like:
for(int i=0; i<positions.length; i++){
DoSomething(positions[i]);
}
Also,
Here is a good link about ref
and out
. The Ref Keyword is used to pass in an argument by reference, and can both read and write to it. The Out Keyword is like Ref, but write-only. The variable passed into it must be written to, and need not be initialized. A function that uses Out can be used to initialize a variable.
It's common to see people using Ref and Out to create functions that return many values at once, however for a number of reasons, including performance, this can be a bad idea. Returning more data can be achieved via Tuples or wrapping things in Structs. That's not to sat that Ref or Out are evil by any means, they have their uses, but they are also often misused.
Oh, thanks for your kindness answer!
I'll check (out) and (ref) keyword with trailrenderer.
Have a nice day :)
@Zarenityx This isn't working for me.
It says "use of unassigned local variable 'positions'" for render.GetPositions(positions);
Oops, sorry @Ne0mega , I wrote that code directly in and forgot that
You do need to use the out keyword on the argument when calling the function also
$$anonymous$$y mistake. The code should actually read
render.GetPositions( out positions);
Sorry for the confusion
Answer by Bunny83 · Mar 25, 2019 at 10:23 PM
Actually the documentation is misleading / wrong. The positions parameter is not an "out" parameter. It's actually a normal parameter and the method expects you to initialize the array yourself. The method will fill in the positions into the array and returns how many elements has been written. This pattern is used in order to reuse the array so you don't have to allocate a new array every time you call the method.
So it works like this:
Vector3[] positions = new Vector3[200]; // allocate large enough
// inside a method:
int count = trailRenderer.GetPositions(positions);
for(int i = 0; i < count; i++)
{
Vector3 p = positions[i];
}
Note the use of "count". You can not simply iterate through the whole array since only count elements has been written.
Oddly the documentation has positions as an out parameter. (https://docs.unity3d.com/ScriptReference/TrailRenderer.GetPositions.html)
$$anonymous$$ake sense to use trailRenderer.positionCount if you don't want to reuse the array! (https://docs.unity3d.com/ScriptReference/TrailRenderer-positionCount.html)
Answer by Ne0mega · Dec 16, 2018 at 06:44 PM
@Zarenityx Thanks for replying! But, I ended up using a particle system instead for the "trail points" I wanted.
Your answer
Follow this Question
Related Questions
Custom trail fixed length and custom form 1 Answer
[Solved] How to correctly use TrailRenderer.GetPositions 1 Answer
Vector3 movement problems 5 Answers
How do i instantiate a Vector3[] ?? 2 Answers
Incorrect velocity after collision 1 Answer