- Home /
Screen Swap
Hi guys i am currently working on a 2d grid based movement game where the player moves with the mouse swipe and if it goes off the screen then it will come on another side everything works fine when i go off screen the player comes from another end of the screen but the controllers are not working the player goes in the same direction over and over again please help me thanks in advance!!
here's my script: Vector3 up = Vector3.zero, right = new Vector3(0, 0, 270), left = new Vector3(0, 0, 90), down = new Vector3(0, 0, 180), currentdirection = Vector3.zero; Vector3 destination, nextPos, direction; float speed = 3f; Vector2 currentpos; Vector2 lastpos; Vector2 distance; float swipeangle = 0;
public GameObject playerprefab,spawn;
public GameObject player;
public void Start() { player = GameObject.Find("player"); currentdirection = up; nextPos = Vector2.up; destination = player.transform.position;
}
public void move() { pos();
if (Vector2.Distance(destination, transform.position) <= .001f)
{
player.transform.localEulerAngles = currentdirection;
destination = player.transform.position + nextPos;
}
}
public void pos()
{
player.transform.position = Vector2.MoveTowards(transform.position, destination, speed * Time.deltaTime);
}
public void moveright() {
nextPos = Vector2.right;
currentdirection = right;
}
private void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.tag == "left") { player.transform.position = new Vector3(2.7f, player.gameObject.transform.position.y, player.gameObject.transform.position.z); Invoke("move", .5f); } if (collision.gameObject.tag == "right") { player.transform.position = new Vector3(-2.7f, player.gameObject.transform.position.y, player.gameObject.transform.position.z); Invoke("move", .5f); } if (collision.gameObject.tag == "up") { player.transform.position = new Vector3(player.gameObject.transform.position.x, -4.9f, player.gameObject.transform.position.z); Invoke("move", .5f); } if (collision.gameObject.tag == "down") { player.transform.position = new Vector3(player.gameObject.transform.position.x, 4.9f, player.gameObject.transform.position.z); Invoke("move", .5f); } }
Your answer
Follow this Question
Related Questions
Grid Based Movement System AI freezing? 1 Answer
Grid Layout Group not working for 3d cube 0 Answers
How do you find adjacent tiles of the same type in grid array [x,y].type == [x+1,y].type 0 Answers
2D Isometric grid movement direction 0 Answers
How do I swap gameObjects on a list by clicking in the UI? 1 Answer