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 roncel106 · Jun 25, 2012 at 01:01 PM · androidcollider

How do I prevent objects from coming out the screen in Android?

Imagine this. I'm creating a simple android game where the player dodges some objects coming towards it. The camera is placed on top (i think it's called birds eye) and it doesn't move. I want to play on the field of view of the camera, but the problem is if I move the player on the side, it goes beyond the screen infinitely. How do I prevent this from happening? Please help :D

If you're having trouble thinking about the game, it's like that old game "asteroids".

Comment
Add comment · Show 2
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 Chris12345 · Jun 25, 2012 at 02:05 PM 0
Share

You mean a wrap screen or a collision wall?

avatar image Chris12345 · Jun 25, 2012 at 02:07 PM 0
Share

For collision wall:If you use in your code transform.translate that adds a constant force if you add a collision wall and the player hits it the player will shake but if you have Add velocity your player can have a collision wall.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Tim-Michels · Jun 25, 2012 at 01:42 PM

Depending on how you actually move the character, you should just restrict the movement by code.

When you use physics for your character, you could just place two (invisible) colliders on the sides, preventing the character from passing those "walls".

If you move the character manually (setting its transform.position), you should have 2 variables (for example m_MinX, m_MaxX). The code where you move the character should then just clamp its position between the min and max value.

Example

void MoveCharacterToRight()

 {
     Vector3 newPos = transform.position;  //store position
     newPos.x += Time.deltaTime * moveSpeed;  //add movement in x-direction

     if (newPos.x > m_MaxX)
     {
         newPos.x = m_MaxX;  //if x is out of range, set x to maximum x
     }

     transform.position = newPos;  //set its position to clamped position
 }


You can do the opposite for the movement to the left, but the idea remains the same.

Cheers

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 AlucardJay · Jun 25, 2012 at 02:20 PM 0
Share

or $$anonymous$$athf.Clamp(newPos.x, m_$$anonymous$$inX, m_$$anonymous$$axX)

you could also work out m_$$anonymous$$inX and m_$$anonymous$$axX with :

 var m_$$anonymous$$inX : float = Camera.main.ScreenToWorldPoint(Vector3(Screen.width*0.05,0.0,0.0)).x;
 var m_$$anonymous$$axX : float = Camera.main.ScreenToWorldPoint(Vector3(Screen.width*0.95,0.0,0.0)).x;

same for other axis =]

avatar image Berenger · Jun 25, 2012 at 05:20 PM 0
Share

roncel106 : Yay!! Thanks a lot, about the colliders, how do I position them in the very edge of the screen? :D

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

6 People are following this question.

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

Related Questions

A trigger to happen only once. 0 Answers

How to add Circle Collider 2D in script 1 Answer

3D text with box collider Android 1 Answer

Rigidbody not working properly on Android 0 Answers

Tilemap for mobile 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