Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 noobunity · Aug 15, 2014 at 08:36 AM · screenbordersscreen.height

How to restrict the player to not go below or above the screen

Hi

I am new to Unity and programming. I am making a game(in 3D) where there is a player and a camera both moves at a same speed in x axis. and user has only control to move the player up and down to avoid the obstacles which are coming towards him (So player is moving from left to right and obstacles from right to left).

But my problem is I am not able to restrict the player going out of screen. i.e if I keep pressing up arrow player goes out of screen same for down arrow. There are some answers for this in this forum but I am not able to make it work. Here is my code for camera and player

For Camera Controll

function Update () {

transform.position.x += ManControll.moveSpeed * Time.deltaTime;

}


For player control

pragma strict

static var moveSpeed:float = 5; var updownSpeed:float = 100;

function Update () {

transform.position.x += moveSpeed Time.deltaTime; transform.position.y += updownSpeed Input.GetAxis("Vertical") * Time.deltaTime;

}

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
0
Best Answer

Answer by brycem24 · Aug 16, 2014 at 08:55 PM

You could always use empty game objects with colliders attached as an invisible barrier.

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 smoggach · Aug 16, 2014 at 09:25 PM 1
Share

You don't even need the colliders if you change your update loop with some logic. Here's some pseudocode: 1. Calculate the new y position. 2. Is this y value higher than the y value of our top gameobject? 3. Is this y value lower than the y value of our bottom gameobject? 4. If no to both: change the transform.position.y value to the new one.

avatar image noobunity · Aug 22, 2014 at 10:35 PM 0
Share

Thanks for the answers, It is working now. I have marked answer as accepted

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Screen.width and Screen.height sometimes flipped for Android 1 Answer

Ligning up a health bar. 1 Answer

When are Screen.width and Screen.height updated after changing the Screen.orientation value? 0 Answers

Unity 3.5.7 Android Screen returns the wrong value? 0 Answers

Make area adapt to screen size 1 Answer


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