• 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 lemiwinks · Feb 05, 2014 at 10:57 PM · limitboundaryside scroller

making boundry for sidescroller

hey all, im trying to make a simple script so if my player object $$anonymous$$ts the top of ther screen he stops moving up and just stays on screen at the top until he is moved down. and visa versa for the bottom. ive tried using collision on a boundry box with no luck so trying out a script, however im not sure how to go about it thanks

btw my movement script so far is t$$anonymous$$s

 public class Runner : MonoBehaviour {
     
     void Update () {
         transform.Translate(5f * Time.deltaTime, 0f, 0f);
 
         if ( Input.GetKey(KeyCode.UpArrow) )
             transform.Translate(5f * Time.deltaTime, 20f * Time.deltaTime, 0f);
 
         if ( Input.GetKey(KeyCode.DownArrow) )
             transform.Translate(5f * Time.deltaTime, -20f * Time.deltaTime, 0f);
 
     }
 
 
 
 }
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
Best Answer

Answer by Maui-M · Feb 05, 2014 at 11:35 PM

A simple way to do it would be to clamp the transform.Y value to a min/max height;

 int maxHeight = 200f;
 itn minHeight = 0f;
 
 public class Runner : MonoBehaviour {
     void Update () {
        transform.Translate(5f * Time.deltaTime, 0f, 0f);
        if ( Input.GetKey(KeyCode.UpArrow) ) {
          transform.Translate(5f * Time.deltaTime, 20f * Time.deltaTime, 0f);
          transform.y = transform.y > maxHeight ? maxHeight : transform.y; // max height
       }
        if ( Input.GetKey(KeyCode.DownArrow) ){
          transform.Translate(5f * Time.deltaTime, -20f * Time.deltaTime, 0f);
          transform.y = transform.y < minHeight ? minHeight : transform.y; //min height
        }
     }
 }

Comment
Add comment · Show 5 · 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 lemiwinks · Feb 06, 2014 at 08:02 PM 0
Share

i have sort of got it working, using the script below. however when i hold down the mouse key and the player hits the top limit, it flickers up and down, in a frantic way. im not sure how to make it smoothly stay at the top of the screen when held up.

     void Update () {
         //time = Time.time;
         Debug.Log (Time.time);
         transform.Translate(5f * Time.deltaTime, -20f * Time.deltaTime, 0f);
 
         if ( Input.GetMouseButton(0) )
             transform.Translate(0f, 60f * Time.deltaTime, 0f);
 
         if ( transform.position.y <= -4)
             transform.Translate(0f,20f * Time.deltaTime , 0f);
 
         if ( transform.position.y >= 4)
             transform.Translate(0f, -60f * Time.deltaTime , 0f);
 
 
     }
avatar image Maui-M · Feb 07, 2014 at 04:05 PM 1
Share
 float maxHeight = 4.0f;
 float minHeight = -4.0f;
 
 void Update () {
     Debug.Log (Time.time);
     Vector3 offset = new Vector3(5f * Time.deltaTime, -20f * Time.deltaTime, 0f);
 
     if(Input.GetMouseButton(0))
         offset.y = 60f * Time.deltaTime;
     else
         offset.y = -20f * Time.deltaTime;
 
     float newY = transform.position.y + offset.y;
     if(newY >= maxHeight)
         offset.y = maxHeight - transform.position.y;
     if(newY <= minHeight)
         offset.y = minHeight - transform.position.y;
 
     transform.Translate(offset.x, offset.y, offset.z);
 }
avatar image lemiwinks · Feb 07, 2014 at 04:41 PM 0
Share

thankyou! that works great on the top of the screen however now the player is bouncing up and down if i dont hold a key and let him fall to the bottom :/

avatar image Maui-M · Feb 07, 2014 at 06:37 PM 1
Share

My line 17 was off, use:

 offset.y = minHeight - transform.position.y;
avatar image lemiwinks · Feb 07, 2014 at 07:42 PM 0
Share

perfect! thankyou again for your help :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 People are following this question.

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

Related Questions

elastic scrolling scripting 0 Answers

Setting Click and Drag constraints 2 Answers

How to keep an object within a circle/sphere (radius) - NO RECTANGLE 3 Answers

How to make a limit so that my character stays on the screen? 0 Answers

limiting rotation to solid angle 5 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