• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by OBess · Jun 19, 2018 at 06:53 PM · unity 2dmovelinerendererlinemove to

Move to line

I'm doing a LineRenderer, and I have a cube. How to make this cube go to the points of the line? The first of line - the second of cube. //first public class DrawLine : MonoBehaviour {

     public LineRenderer lineRenderer;
     public EdgeCollider2D edgeCol;
 
     List<Vector2> points;
     float k;
 
     public void UpdateLine (Vector2 mousePos) {
         if (points == null) {
             points = new List<Vector2> ();
             SetPoint (mousePos);
             return;
         }
         if (Vector2.Distance (points.Last (), mousePos) > .1f)
             SetPoint (mousePos);
     }
 
     void SetPoint(Vector2 point){
         points.Add (point);
 
         lineRenderer.numPositions = points.Count;
         lineRenderer.SetPosition (points.Count - 1, point);
 
         if (points.Count > 1)
             edgeCol.points = points.ToArray();
     }
 
     void OnCollisionEnter2D (Collision2D col) {
         if(col.gameObject.tag == "Player"){
             Debug.Log ("LOL");
         }
     }
 }
 //second
 public class MoveCube : MonoBehaviour {
 
     int s;
     float k;
 
     public GameObject gm;
 
     void Start(){
         PlayerPrefs.SetInt ("Move", 0);
     }
 
     void Update(){
         if (PlayerPrefs.GetInt ("Move") == 1) {
             transform.Translate (Time.deltaTime * GetComponent<Rigidbody2D> ().mass, 0f, 0f);
             k += 1 * Time.deltaTime;
             if (k > 1) {
                 s++;
                 k = 0;
             }
             GameObject.FindGameObjectWithTag ("Score").GetComponent<Text> ().text = s.ToString ();
         } else if (PlayerPrefs.GetInt ("Move") == 2) {
             transform.Translate (Time.deltaTime * GetComponent<Rigidbody2D> ().mass, 0f, 0f);
             k += 1 * Time.deltaTime;
             if (k > 1) {
                 s++;
                 k = 0;
             }
             GameObject.FindGameObjectWithTag ("Score").GetComponent<Text> ().text = s.ToString ();
         }
     }
 
     void OnCollisionEnter2D (Collision2D col) {
         if(col.gameObject.tag == "Platform"){
             PlayerPrefs.SetInt ("Move", 1);
             PlayerPrefs.SetInt ("Spawn", 1);
         }
         if(col.gameObject.tag == "Line"){
             PlayerPrefs.SetInt ("Move", 2);
             PlayerPrefs.SetInt ("Spawn", 1);
         }
     }
 }
 


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

86 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Draw over using Line Renderer 2D when hitting a collider 0 Answers

LineRenderer with nontrivial radius? 1 Answer

LineRenderer line disappearing (Alignment) 2 Answers

Smooth out a line renderer 1 Answer

How to move BoxCollider2D so it covers a line renderer? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges