Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 santyarellano039 · Jan 27, 2017 at 12:53 AM · c#2d gamescreen resolutionscreen sizeborders

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

alt text

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);
     }

captura-de-pantalla-44.png (16.0 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 2 · Share
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
avatar image santyarellano039 · Jan 27, 2017 at 03:59 PM 0
Share

Sorry... is that suposed to be in a UI? if it is, I'm not making the game in the UI.

avatar image EDevJogos santyarellano039 · Jan 27, 2017 at 04:06 PM 0
Share

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

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.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges