- Home /
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!!
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!
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!
this is bad solution in my case because half of hero will cross edge of screen
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); }
@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.
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.
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!!
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()
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.
Your answer
Follow this Question
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