Legacy Documentation: Version 2017.2 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

LineRenderer.SetPositions

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual
public method SetPositions(positions: Vector3[]): void;
public void SetPositions(Vector3[] positions);

Parameters

positions The array of positions to set.

Description

Set the positions of all vertices in the line.

This method is preferred to SetPosition when setting all positions, as it is more efficient to set all positions using a single command than to set each position individually.

See Also: numPositions property.

See Also: SetPosition function.

#pragma strict
public var c1: Color = Color.yellow;
public var c2: Color = Color.red;
public var lengthOfLineRenderer: int = 20;
function Start() {
	var lineRenderer: LineRenderer = gameObject.AddComponent.<LineRenderer>();
	lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
	lineRenderer.widthMultiplier = 0.2f;
	lineRenderer.positionCount = lengthOfLineRenderer;
	// A simple 2 color gradient with a fixed alpha of 1.0f.
	var alpha: float = 1.0f;
	var gradient: Gradient = new Gradient();
	gradient.SetKeys([new GradientColorKey(c1, 0.0f), new GradientColorKey(c2, 1.0f)], [new GradientAlphaKey(alpha, 0.0f), new GradientAlphaKey(alpha, 1.0f)]);
	lineRenderer.colorGradient = gradient;
}
function Update() {
	var lineRenderer: LineRenderer = GetComponent.<LineRenderer>();
	var points: var = new Vector3[lengthOfLineRenderer];
	var t: var = Time.time;
	for (var i: int = 0; i < lengthOfLineRenderer; i++) {
		points[i] = new Vector3(i * 0.5f, Mathf.Sin(i + t), 0.0f);
	}
	lineRenderer.SetPositions(points);
}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { // Creates a line renderer that follows a Sin() function // and animates it.

public Color c1 = Color.yellow; public Color c2 = Color.red; public int lengthOfLineRenderer = 20;

void Start() { LineRenderer lineRenderer = gameObject.AddComponent<LineRenderer>(); lineRenderer.material = new Material(Shader.Find("Particles/Additive")); lineRenderer.widthMultiplier = 0.2f; lineRenderer.positionCount = lengthOfLineRenderer;

// A simple 2 color gradient with a fixed alpha of 1.0f. float alpha = 1.0f; Gradient gradient = new Gradient(); gradient.SetKeys( new GradientColorKey[] { new GradientColorKey(c1, 0.0f), new GradientColorKey(c2, 1.0f) }, new GradientAlphaKey[] { new GradientAlphaKey(alpha, 0.0f), new GradientAlphaKey(alpha, 1.0f) } ); lineRenderer.colorGradient = gradient; }

void Update() { LineRenderer lineRenderer = GetComponent<LineRenderer>(); var points = new Vector3[lengthOfLineRenderer]; var t = Time.time; for (int i = 0; i < lengthOfLineRenderer; i++) { points[i] = new Vector3(i * 0.5f, Mathf.Sin(i + t), 0.0f); } lineRenderer.SetPositions(points); } }

Did you find this page useful? Please give it a rating: