i’ve come up with a way to make the track endless,but i’m not sure why it’s not working.the logic might not be right,if so please tell me how to correct it.if it is right,then help me find why it is not working?from the image,the colliders can be seen. script attached to the player.Pim,Pjm and Pkm are colliders in the middle of i,j and k blocks respectively(not in image)
i can’t seem to upload the image.here’s the link http://s26.postimg.org/sweoprmo9/Untitled.png
here’s the code
using UnityEngine;
using System.Collections;
public class loco : MonoBehaviour {
// Use this for initialization
public int movementSpeed=2;
public int ctr=0;
public Vector2 StartPos;
public int SwipeID = -1;
public float minMovement = 20.0f;
public int lane=1;
public int popo=1;
public GameObject block1;
public GameObject block2;
public GameObject block3;
public Vector2 Pii;
public Vector2 Pif;
public Vector2 Pji;
public Vector2 Pjf;
public Vector2 Pki;
public Vector2 Pkf;
public Vector2 Pim;
public Vector2 Pjm;
public Vector2 Pkm;
public float half;
public bool isAlive=true;
void Start () {
block1 = GameObject.FindGameObjectWithTag ("Block_i");
block2 = GameObject.FindGameObjectWithTag ("Block_j");
block3 = GameObject.FindGameObjectWithTag ("Block_k");
Pii=GameObject.FindGameObjectWithTag("Pii").transform.position;
Pif=GameObject.FindGameObjectWithTag("Pif").transform.position;
Pji=GameObject.FindGameObjectWithTag("Pji").transform.position;
Pjf=GameObject.FindGameObjectWithTag("Pjf").transform.position;
Pki=GameObject.FindGameObjectWithTag("Pki").transform.position;
Pkf=GameObject.FindGameObjectWithTag("Pkf").transform.position;
half = (block1.transform.localScale.y) / 2;
Pim=GameObject.FindGameObjectWithTag("Pim").transform.position;
Pjm = GameObject.FindGameObjectWithTag ("Pjm").transform.position;
Pkm=GameObject.FindGameObjectWithTag("Pkm").transform.position;
}
// Update is called once per frame
void Update () {
transform.Translate (Vector2.up * Time.deltaTime * movementSpeed);
foreach (var T in Input.touches)
{
var P = T.position;
if (T.phase == TouchPhase.Began && SwipeID == -1)
{
SwipeID = T.fingerId;
StartPos = P;
}
else if (T.fingerId == SwipeID)
{
var delta = P - StartPos;
if(lane==1)
{
if (T.phase == TouchPhase.Moved && delta.magnitude > minMovement)
{
SwipeID = -1;
if (Mathf.Abs(delta.x) > Mathf.Abs(delta.y))
{
if (delta.x > 0)
{
transform.Translate (new Vector3(1.5f,0,0));
lane++;
}
else if (delta.x < 0)
{
transform.Translate (new Vector3(-1.5f,0,0));
lane--;
}
}
}
else if (T.phase == TouchPhase.Canceled || T.phase == TouchPhase.Ended)
SwipeID = -1;
}
else if(lane==0)
{
if (T.phase == TouchPhase.Moved && delta.magnitude > minMovement)
{
SwipeID = -1;
if (Mathf.Abs(delta.x) > Mathf.Abs(delta.y))
{
if (delta.x > 0)
{
transform.Translate (new Vector3(1.5f,0,0));
lane++;
}
}
}
else if (T.phase == TouchPhase.Canceled || T.phase == TouchPhase.Ended)
SwipeID = -1;
}
else if(lane==2)
{
if (T.phase == TouchPhase.Moved && delta.magnitude > minMovement)
{
SwipeID = -1;
if (Mathf.Abs(delta.x) > Mathf.Abs(delta.y))
{
if (delta.x < 0)
{
transform.Translate (new Vector3(-1.5f,0,0));
lane--;
}
}
}
else if (T.phase == TouchPhase.Canceled || T.phase == TouchPhase.Ended)
SwipeID = -1;
}
}
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "fish") {
ctr++;
Destroy (collision.gameObject);
} else if (collision.gameObject.tag == "obstacle") {
isAlive=false;
}
}
void OnTriggerEnter2D(Collider2D collider)
{
label1:if (collider.gameObject.tag == "Pjm") {
if (Pif.y < Pji.y && Pjf.y < Pki.y) {
Debug.Log ("replace i block at Pkf");
block1.transform.position = (new Vector2 (block1.transform.position.x, Pkf.y+half));
}
} else if (collider.gameObject.tag == "Pkm") {
if (Pjf.y < Pki.y && Pkf.y < Pii.y) {
Debug.Log ("replace j block at Pif");
block2.transform.position = (new Vector2 (block1.transform.position.x, Pif.y+half));
}
} else if (collider.gameObject.tag == "Pim") {
if (Pkf.y < Pii.y && Pif.y < Pji.y) {
Debug.Log ("replace k block at Pjf");
block3.transform.position = (new Vector2 (block1.transform.position.x, Pjf.y+half));
}
}
goto label1;
}
void OnGUI()
{
GUI.color = Color.black;
GUI.Label (new Rect (50, 25, 100, 30), "Fishes :"+ctr.ToString());
}
}
it has cut off the code but here is what it is
block1.transform.position = (new Vector2 (block1.transform.position.x, Pkf.y+half));
block2.transform.position = (new Vector2 (block1.transform.position.x, Pif.y+half));
block3.transform.position = (new Vector2 (block1.transform.position.x, Pjf.y+half));