Search Unity

Change start position of line renderer

Discussion in '2D' started by ZeKassaK, Dec 26, 2016.

  1. ZeKassaK

    ZeKassaK

    Joined:
    Sep 29, 2014
    Posts:
    17
    Hello !

    I have a script for draw a circle like :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DrawCircle : MonoBehaviour
    5. {
    6.     public int segments;
    7.     public float xradius;
    8.     public float yradius;
    9.     LineRenderer line;
    10.  
    11.     void Start ()
    12.     {
    13.         line = gameObject.GetComponent<LineRenderer>();
    14.  
    15.         line.numPositions = segments + 1;
    16.         line.useWorldSpace = false;
    17.  
    18.         CreatePoints ();
    19.     }
    20.        
    21.     void CreatePoints ()
    22.     {
    23.         float x;
    24.         float y;
    25.         float z = 0f;
    26.  
    27.         float angle = 20f;
    28.  
    29.         for (int i = 0; i < (segments + 1); i++)
    30.         {
    31.             x = Mathf.Sin (Mathf.Deg2Rad * angle) * xradius;
    32.             y = Mathf.Cos (Mathf.Deg2Rad * angle) * yradius;
    33.  
    34.             line.SetPosition (i,new Vector3(x,y,z) );
    35.  
    36.             angle += (360f / segments);
    37.         }
    38.     }
    39. }

    This code is working, If I attach my script on a GameObject, I have a circle drawn around my GameObject like in pink :

    orbit.png

    My question is, how can I change the center of mycircle ? It's always the center of the GameObject, Is there a way to change that ?

    Thanks you :)
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    This should work (where middle will be an offset from the center of your GameObject):

    Code (CSharp):
    1.     void CreatePoints (Vector2 middle)
    2.     {
    3.         float x;
    4.         float y;
    5.         float z = 0f;
    6.         float angle = 20f;
    7.         for (int i = 0; i < (segments + 1); i++)
    8.         {
    9.             x = middle.x + Mathf.Sin (Mathf.Deg2Rad * angle) * xradius;
    10.             y = middle.y + Mathf.Cos (Mathf.Deg2Rad * angle) * yradius;
    11.             line.SetPosition (i,new Vector3(x,y,z) );
    12.             angle += (360f / segments);
    13.         }
    14.     }