Set limit camera screen WITDH - c#

Look my game has a fixed camera, configured as a standard 1280x720.
I placed at each end of the cam width of an empty object with a collider to prevent the player pass this point.
Everything works perfectly until the default resolution decreases. When I run the test with 480x320 screen width decreases, but the collider, does not follow this decrease, staying out of the screen, which makes the player to be "cut off."
The following two images:
First: 1280x720
Second: 480x320
There is some way which I can only set the side margins left and right of the camera as a collider?
If it is not possible to configure only the edge of the camera as a delimiter, the player would have some other way to do something to solve my problem?
Some items fall from the sky, so I can not have a collider at the top of the camera.

There are many ways to do this. The easiest way is to create a 4 box collider 2D and position them to the edge of the screen.
I always use this complete script to do this. It's too long to post here. Just attach it to an empty GameObject in the scene. It will do the rest of the work for you. I will create the colliders and position them to the edge of the scrren automatically.

As what i have understand my be this can help you
Scale box collider to camera view

Related

Unity 2D drag and drop without using Canvas

I have a board that made with bunch of square tiles, i have player which is circle and i wanna drop over the tiles and set plater postion to the center of nearest tile while mouse button up ;
is there any way to handle this without Canvas and EventSystem ?
There are a few ways to do it,
You can store all the tiles in an array and when dragging the player check for the position of the closest one, this can work great if you have gaps between tiles and want the player to still snap on to the tile even if the player is not over it by having a threshold setup, it could get a bit costly though if you have thousands of tiles and you are iterating through them all.
Second and the easiest one is to just cast a Raycast from your player straight to the plane where your tiles are positioned, on hit just position the player in the middle of the tile. If you have more objects in the scene you can also tag the tiles with for example "Tile" and in the raycast check for the tag. The use of raycasts is documented here https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

Unity2D hide everything that is outside shape defined by edgecollider

I have a 2D polygon that is defined by a edgeCollider. Is there a way to hide everything that is outside of the shape and show only what is inside?
I tried using skyboxes and lights. I thought about creating a mask(but i dont know how to create such a mask).
Is there a way to only show what is inside the shape defined by edge collider?
What about using the colliders trigger events?
Set this polygon as a trigger in the edgeCollider2D component in the inspector. Then you can use the collider OnEnterTrigger2D.
Your gameobjects are all disabled until this edgeCollider collides with in. Then disable the gameobject OnExitTrigger2D.
If you wanted to limit it to a certain number of object only. You would set a layer to only hide/show these object.
void OnTriggerEnter2D(Collider2D other) {
if(other.gameObject.layer == "hiddenObject"){
other.gameObject.enable = true;
}
}
Then the reverse on the OnTriggerExit2D.
I'm not sure the effect that you are aiming for. So another solution could be a postprocessing shader.
I can only give a high level description on this however.
You would take the final screen image texture, and the current position of the polygon and then add the pixels from the screen texture to the polygon texture and output this texture. (this shader has to exists already).
But you want the inverse of these.
https://docs.unity3d.com/Manual/shader-TransparentCutoutFamily.html
You're wanting to keep what's in the hole and get rid of what's outside of it?
You can download the built in shaders to edit them here
https://unity3d.com/get-unity/download/archive
Just find your version of unity and select builtin shaders from the drop down.
Edit: "SPOTLIGHT!", try this but with your custom shape
http://www.shaderslab.com/demo-49---spotlight.html

Object-following Virtual Buttons

As shown in the picture, I want to be able to maintain this placement of virtual buttons wherever the ball moves.
This is what I've tried:
1.
The cue ball is a Rigidbody with Bounce Material, so it can bounce off the cushions. So in this case, I tried making the Virtual Buttons RigidBody Components and adding the same force to the buttons, but this didn't work because it wasn't able to detect the cushions and kept moving forward.
2.
I gave it a collider, it would bounce off the cushion before the ball and that would disrupt the formation around the ball.
3.
I tried to parent the Virtual Buttons under the Cue Ball and the Cue Ball under the Image Target, but this didn't help since it all centered within the ball and overlapped. I couldn't move it at all (I don't know why)
Willing to put a Bounty on the answer.
Virtual Butttons sometimes don't move above the Image Target, I really don't know why. But here's a work around.
Now consider your CueBall the parent. Throw in 8 Empty GameObjects as child Objects. Position these GameObjects in the position around the ball that you wish to maintain through the movement.
Now each virtual button is put as a child to these GameObjects respectively.
And now script movement of the ball. You should see it works perfectly. Make sure you have your X and Z rotation restriction. I guess since it's a Cue ball and its white, the rotation won't be much of a problem, unless you're scripting for Curve shots as well.
Hope this helps.
A solution might be to create a script to attach to your ImageTarget, that has 9 public transforms, (your 8 planes and the cue ball) that would then look at the cue ball's x and z coords, and then would change the planes' x and z coords accordingly.

How do I make so when user moves the camera it doesn't go beyond the scene borders?

How do I make so when I move the camera(with touch) it doesn't go beyond the scene borders?
How do I move the camera with touch so it moves strictly with scene parts, like slides(swipe-first slide, another swipe-another slide) with not going beyond the borders of the scene?
The game I'm making has a camera like in Disco Zoo game for android (I'm a newbie)
Technically, the scene doesn't really have a border. You'll need to define the border somehow within your game, then constrain the camera's position with something like Mathf.Clamp(value, min, max) in an Update() function on the camera.
How can you define the border? It's up to you. Some ideas:
Hard-code the values in the script that clamps the camera. Probably the quickest option, but not flexible
Make public parameters on the camera script that let you set min and max positions in the X and Y directions
If you have a background image: use the extents of that to define your camera's extents
Create empty objects in your scene that define the minimum and maximum extents of the scene. Put your "min" object at the top-left, and the "max" object at the top-right. Connect it to the camera script, then use those positions to see if you've gone too far in any given direction. The main reason to do this is that it's visual.
(Slower, but dynamic) If everything in your scene uses physics, you could search the entire scene for every Collider component, then find the furthest extents in each direction. However, this is probably going to be pretty slow (so you'll only want to do it once), it'll take a while to code, and you'll probably want to tweak the boundaries by hand anyway.

Collision detection 2D between rectangles

I am writting a collision detection engine for my game and I have some problems.
Indeed, since I have several fixed rectangle and one moving (the player), I need to know which side of the fixed one was collided at first by the player, to replace him correctly.
The fixed rectangle are NOT in a grid so they can be placed anywhere on the map and they can have diffents size. They are not rotated.
The player class stores it's direction vector.
Any idea?
KiTe
In a nutshell:
You'll compare the Y and X components of the bounding rectangles to eachother to check for a collision. If the top(Y) of the player is less than the bottom of an enemy then you don't need to check anymore because it's not possible that they're colliding. If the right(X) side of the player is less than the left side of the enemy then they can't be colliding. It would help to define top, right, bottom, left of each object you intend to check inside the class. This will allow you to know which side is hit also. This should be enough to get you thinking and experimenting.
Have fun!
The name is "Axis-Aligned Bounding Box" collision detection.
Now you know the name, you can Google for the rest.
thanks to both of you for your help.
I've heard about AABB, but at first sight it didnt seem to fit to my needs (since I didn't understand it well).
But after writing down everything on papper, the solution I found appeared to be exactly the same as AABB !

Categories