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 FrosTech · Dec 20, 2013 at 02:08 PM · 2dgame

Stop movement at edge of screen! 2d shooter

Hi! I'm trying to learn to make a shooter!

I already made the player movement!

And, here's how it gets weird!!

I want the movement stopped at the edge of the screen!!

Here's what I did!!

  • box collider on player

  • I added a cube at the edge of the screen to restrict movement(box collider)!

  • Added rigidbody to player cube!

  • Disabled gravity

  • Freezed x,y,z position and rotation of both!

Now when I run the game and with the player if I hit the screen limiter...

It goes straight through it!

Probably A basic error on collision!!

But I wanted to know is there any btter way to restrict movement at edge of the screen for 2d shooter game!!

Comment
Add comment · Show 1
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 g0tNoodles · Dec 20, 2013 at 03:08 PM 0
Share

What are you using to detect the collision?

4 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by b0nk · Dec 20, 2013 at 04:40 PM

Hello,

You don't need the physics engine at all to accomplish that, just "limit" the player movements.

For example, let's say you want to limit movement on X axis from -4.3 to 4.3 and on Y axis from -2.7 to 2.7:

 // X axis
 if (transform.position.x <= -4.3f) {
     transform.position = new Vector2(-4.3f, transform.position.y);
 } else if (transform.position.x >= 4.3f) {
     transform.position = new Vector2(4.3f, transform.position.y);
 }
 
 // Y axis
 if (transform.position.y <= -2.7f) {
     transform.position = new Vector2(transform.position.x, -2.7f);
 } else if (transform.position.y >= 2.7f) {
     transform.position = new Vector2(transform.position.x, 2.7f);
 }

You just need to check when the player reaches the limits and put it back there.

Hope it helps!

Comment
Add comment · Show 6 · 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 FrosTech · Dec 21, 2013 at 05:48 AM 0
Share

Well, thanks on this!!

I found the similar post from 3d buzz earilier and used it!!

Yours and 3d buz reference the same stuff and did my job done!

avatar image underdog1515 · Jan 03, 2015 at 01:30 PM 0
Share

this is bad solution in my case because half of hero will cross edge of screen

avatar image merkaba48 · Jan 03, 2015 at 04:14 PM 0
Share

Can't you just modify the edge check values so they are +/- half the width of your player?

Sorry I tried formatting this but don't know how.

float playerHalfWidth = playerWidth/2f;

if (transform.position.x + playerHalfWidth > screenWidth) { transform.position = new Vector2(screenWidth - playerHalfWidth, transform.position.y); }

if (transform.position.x - playerHalfWidth < 0) { transform.position = new Vector2(playerHalfWidth, transform.position.y); }

avatar image underdog1515 · Jan 03, 2015 at 05:54 PM 0
Share

@merkaba48 thanks, but it's also not the best solution. Situation: I got a 640x640 sprite from my designer and the hero was actually full height by Y, but by X he was in the middle, like maybe 100-150px. And for your solution hero must be 640px :) Of course, you can edit sprite in some Paint, PS or etc, but i'm so lazy for doing such things:) Now what I did: i've added PolygonCollider2D on my hero. Then, i got Collider2d.bounds.extents.x property, which is actual half of hero in world coordinates and then i've use it similar to your script.

avatar image Psyrage · May 19, 2016 at 08:37 AM 0
Share

Thanks Bro.

Show more comments
avatar image
6

Answer by robertbu · Dec 20, 2013 at 04:39 PM

We need to see some of your code and perhaps have a better description to figure out why your collision detection is not working, but there are other ways to restrict movement. Viewport coordinates go from (0,0) in the lower left to (1,1) in the upper right. We can convert the world position to Viewport coordinates, clamp the result to some range, then convert back to world coordinates. After you done the character move, add these lines:

 var pos = Camera.main.WorldToViewportPoint(transform.position);
 pos.x = Mathf.Clamp(pos.x, 0.1, 0.9);
 pos.y = Mathf.Clamp(pos.y, 0.1, 0.9);
 transform.position = Camera.main.ViewportToWorldPoint(pos);

Note these lines clamp the pivot point, so the clamp is a bit inside the 0 to 1 range. You will need to adjust the '0.1' and '0.9' as appropriate to the size of your character.

Comment
Add comment · Show 3 · 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 FrosTech · Dec 21, 2013 at 05:52 AM 0
Share

There just not much code!

You put Translate code for player for both horizontal and vertical movement!!

The code only does that!!

Then, I added the screen border i.e. cube to restrict screen movement with rigidbody and box collider attached, removed gravity!

Note: Is trigeer is disabled to make collision occur!

Player has box collider too!

The problem is when the cube collides with the screen border, it gets that vibration effect!

The player start bouncing or vibrating all over the place after collision even if I don't use movement keys to move player!

After collision, player looses control upto 50% rate or you can say full control is lost!!

I FIGURED $$anonymous$$AYBE IT WAS THE transform.Translate() combined with Input.GetAxis() that made it happen through smoothing taking place!

I do found alternative to this!

BUT I DO WANT TO GET RID OF THE SHA$$anonymous$$ING/VIBRATING EFFECT!!

You can replicate it too as well!!

avatar image Papadog FrosTech · Mar 07, 2016 at 01:49 PM 0
Share

I know it's little late, but i found the solution to get rid of "shaking". You just have to put this code in FixedUpdate(), not in Update()

avatar image migmig765 · Jun 17, 2018 at 07:44 PM 0
Share

Thx, searching for like ever even if the post is old.

avatar image
1

Answer by merkaba48 · Dec 20, 2013 at 04:41 PM

There's (the start of a) a good tutorial on making a basic Pong game using the Unity2D components. Find it here: http://www.youtube.com/watch?v=yQXdREL4GGg

I don't know what's wrong with your scene, but if you follow that tutorial you might find something out you didn't know.

Comment
Add comment · 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
0

Answer by Oshigawa · May 20, 2016 at 05:07 AM

Always put your input-related code in Update().

@AndrewRyan

You're right, update() it is.

Comment
Add comment · Show 1 · 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 AndrewRyan · Nov 11, 2016 at 11:50 PM 1
Share

You probably mean Update().

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

27 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Making A Tileset 1 Answer

Unity iPhone Tutorial? 1 Answer

2D pong game. HELP 1 Answer

A node in a childnode? 1 Answer

2D Game Movement script(W,A,S,D input only)? 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