# How Can Create Line Renderer Runtime When Player in Gravity

I want to Create functionality Like Rock Runner Game.when player into the gravity and hold the button then generate line and player hold the line and move forward. After So many Research i couldn't find anything in google and also in YouTube then i post the question. I put some snippets to very well understand what i want actually. if any one have idea how to do this then tell me. thanks in advance

Watching a video of the game this looks like a simple straight line between the player and the object of the ceiling.

For that you can use a Line Renderer component

When holding the finger pressed and the player is within the range of the "ceiling object" you just

• create an empty game object
• add a line renderer component
• and add some script like

[RequireComponent(typeof(LineRenderer))]
public class LineController : MonoBehaviour {

public GameObject startObject;  //e.g. the player
public GameObject endObject;    //e.g. the object on the ceiling

private LineRenderer lineRenderer;

void Start () {
lineRenderer = GetComponent(); //stackoverflow cut out the generic part ;-)
}

void Update () {
lineRenderer.SetPosition(0, startObject.transform.position);
lineRenderer.SetPosition(1, endObject.transform.position);
}
}


The rest should be configuration of the line renderer component to match the look of the line as you want it. And if the player releases his finger delete the line game object.

• first thanks for very useful ans..i do this.. and successfully create line between player to "ceiling object" using Line Renderer component find the distance between player to "ceiling object" using Vector3.distance. – Darshan Soni Sep 20 '16 at 10:11
• but i want when the player holding the finger Press then player hold the Edge of Line Renderer can't fall into the ground .. how can i do any idea.?? – Darshan Soni Sep 20 '16 at 10:12
• Oh. In the game showen above the player can tap/press anywhere on the screen. Do I understand it right you want to limit the "touchable screen space" to the end of the line? Since the player character is at the end of the line it would mean you could make the character clickable/pressable and check if the player clicks/presses the character. – monty Sep 20 '16 at 11:01
• If you mean the flying behaviour (for not falling into the ground) you would add some force while the player presses (if you use physics). Like in this question (stackoverflow.com/questions/24266065/…) – monty Sep 20 '16 at 11:04

I found the solution to Create Line when player in Gravity.

When holding the finger pressed and the player is within the range of the "ceiling object" i Create a LineRenderer , create One Empty Object Child of "celling object" and attached linerenderer to Empty Object.& For Calculate the Distance using Vector3.distance

public GameObject StarObj,EndObj;
private LineRenderer lineRenderer;
private HingeJoint2D hingJoint;

// Use this for initialization
void Start () {

lineRenderer = GetComponent<LineRenderer> ();
hingJoint = GetComponent<HingeJoint2D> ();
}

// Update is called once per frame
void Update () {

if (Vector3.Distance (transform.position, StarObj.transform.position) <= 8f) {
lineRenderer.enabled = true;
lineRenderer.SetPosition (0, StarObj.transform.position);
lineRenderer.SetPosition (1, EndObj.transform.position);

if (DemoLongPress.instance.jointTrue) {
hingJoint = EndObj.GetComponent<HingeJoint2D> ();
hingJoint.connectedBody = StarObj.GetComponent<Rigidbody2D> ();
hingJoint.connectedAnchor = new Vector2 (0, 1f);
hingJoint.anchor.Set (0, 1);
}

} else {
lineRenderer.enabled = false;
}
}


When Player Hold the finger Player Can Stick to the LineRenderer using HingJoint2D When player enter to the distance to create line between player to "ceiling object" and when player hold the finger then joint is connected between player to "ceiling object" and player stick to the line. And if the player releases his finger delete the line game object. if any correction or mistake then tell me i correct it...The rest should be configuration of the line renderer component to match the look of the line as you want it. Thanks