- Home /
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