Version: 2023.1
LanguageEnglish
  • C#

LineRenderer.SetPosition

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

Declaration

public void SetPosition(int index, Vector3 position);

Parameters

index Which position to set.
position The new position.

Description

Set the position of a vertex in the line.

Consider using SetPositions instead, if setting multiple positions, as it is much faster than making individual function calls for each position.

See Also: positionCount property.

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("Sprites/Default")); 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 t = Time.time; for (int i = 0; i < lengthOfLineRenderer; i++) { lineRenderer.SetPosition(i, new Vector3(i * 0.5f, Mathf.Sin(i + t), 0.0f)); } } }