- Home /
2D snake game stop sprite from leaving screen
Hi,
I've been stuck for weeks. I'm making a 2D classic snake mobile game in C#... but I can't stop the snake from leaving the screen! I've tried to create empty objects and use them as borders, but if the screen resolution changes, the borders will either end outside the screen or inside the screen. I don't want to limit the screen resolution, I want to stop the player from leaving the screen on ANY DEVICE RESOLUTION... how can I do it?
Here's a screenshot of my game.
Please... I am very very VERY noob at this... I'd never imagine that it would be so hard to make just a simple thing
Here is my actual Snake Code:
using UnityEngine;
using System.Collections;
public class HeadController : MonoBehaviour {
public bool isPlaying = true;
public float SnakeSpeed = 10.0f;
public bool frontBlocked = false;
public bool rightBlocked = false;
public bool leftBlocked = false;
public bool SnakeAlive = true;
void Awake() {
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void DetectBait() {
}
/*void MoverArriba () {
this.transform.localScale = new Vector3 (1, 1, 1);
this.transform.rotation = Quaternion.Euler (0, 0, 0);
this.transform.position = this.transform.position + new Vector3 (0 ,SnakeSpeed, 0);
this.transform.rotation = Quaternion.Euler (0, 0, 90);
}*/
void CheckTriggerColliders () {
//---------------------checar el frontCollider-------------------
if (GameObject.Find ("frontCollider").GetComponent<FrontCollider> ().EnTrigger== true) {
frontBlocked = true;
}
if (GameObject.Find ("frontCollider").GetComponent<FrontCollider> ().EnTrigger== false) {
frontBlocked = false;
}
//---------------------checar el rightCollider---------------------
if (GameObject.Find ("rightCollider").GetComponent<RightCollider> ().EnTrigger == true) {
rightBlocked = true;
}
if (GameObject.Find ("rightCollider").GetComponent<RightCollider> ().EnTrigger == false) {
rightBlocked = false;
}
//---------------------checar el leftCollider--------------------
if (GameObject.Find ("leftCollider").GetComponent<LeftCollider> ().EnTrigger == true) {
leftBlocked = true;
}
if (GameObject.Find ("leftCollider").GetComponent<LeftCollider> ().EnTrigger == false) {
leftBlocked = false;
}
}
void Advance () {
this.transform.position = this.transform.position + new Vector3 (SnakeSpeed, 0, 0);
}
Answer by EDevJogos · Jan 27, 2017 at 01:15 AM
If you're doing your game in screen space, as it seems from your question, just set the anchors on the RectTransfoms to their respective sides, like LeftCollider set the Anchor to middle Left, RightCollider midle right and so on, this will make them to be positioned relative to the borders of the screen.
Sorry... is that suposed to be in a UI? if it is, I'm not making the game in the UI.
Oh, check out this answer then, this should do the trick:
http://answers.unity3d.com/questions/623959/how-to-keep-object-from-going-off-screen.html
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity 2D How to Scale or Re-position sprite relative to different Screen sizes 1 Answer
2d game for different screen resolutions 1 Answer
How can I flip only my 'Player' gameobject, and l leave it's child object alone? 2 Answers