Unity 2D - How to make Resolution-Independent colliders on my sprite - unity3d

I'm currently working on a school project where we have to merge a 2010 game with an arcade-style game. I chose to merge COD:Blackops Zombie mode with Pacman.
Right off the bat I'm having issues. I'm trying to figure out how I can make the colliders for my level scale with the resolution.
I have an orthographic camera that has a size of 5.
This is what my level currently looks like.
As you can see above, the level itself is a single sprite, placed in a canvas that has Scale with Screen Size enabled.
I've placed a couple of 2D colliders on empty game objects on it for testing purposes.
The current resolution is 1920x1080, which is a 16:9 aspect ratio.
However, if I change the resolution to something like 800x600, which is a 4:3 aspect ratio... then the colliders are completely wrong now.
I thought that the colliders would scale with screen size just like the level sprite does, but I was wrong.
I am now trying to find an alternative approach to making the level have colliders that also change with the size of the sprite...
If you need more information please let me know and I will gladly update my question with the requested information.

To keep things simple, first start with a square aspect canvas.
Make an Empty GameObject, and place it at the bottom left corner of your canvas.
Put all your colliders inside this empty GameObject, and set them up so they match your play-field.
Now all you'll need to do is have a script on this empty gameobject that sets in Update():
Vector2 screenScale = new Vector2(Screen.width, Screen.height).normalized;
transform.localScale = new Vector3(screenScale.x, screenScale.y, 1);
This will make it scale everything inside it to the same proportions as the screen. Alternatively, you could use the Canvas's size if you want to make it based on that instead of the screen itself.

Related

How to align sprites of smaller sizes to a moving gameobject sprite?

I used to have a game object used as a sword with sprites with a specific size, despite including quite a bit of transparent space, to ensure these are aligned properly with the player game object:
This ensures the sword follows the player as he jumps by making use of the hero transition like this transform.position = hero.transform.position; , and though there may be issues with sprite changes I would address these later.
However, since I want to have several different equipment, and other sprites of this same sword might need a bigger dimension to look good (such as a sword attack while standing on the ground), I could either make even bigger sprites which would eventually affect performance due to transparent pixels loading, or I thought of making sprites with specific sizes:
(if this works I'd make sure to draw and put them close together instead of being separate)
And although when I prepare the animation I make sure to shift the position of the new sword to where it would be based on the player sprite on its own air attack animation (thus I had to modify this frame by frame,
The sword doesn't seem to follow the player, even when its game object still uses the script that makes use of the player's transform position:
I'm assuming something else has to be changed frame by frame, but what could it be? Is there a way to align or anchor a smaller sprite to follow the pivot of a bigger sprite?
All rotation or changing of sprites is done relative to the sprite's Pivot Point.
When you currently swap your sprites, your sword looks like it is rotating on it's blade rather than the handle.
Change the Pivot point to the handle, and it will do most of the work.
The rest is just making sure the handle of the sword follows the character's hands.

Recommendations for clipping an entire scene in Unity

I'm looking for ways to clip an entire unity scene to a set of 4 planes. This is for an AR game, where I want to be able to zoom into a terrain, yet still have it only take up a given amount of space on a table (i.e: not extend over the edges of the table).
Thus far I've got clipping working as I want for the terrain and a water effect:
The above shows a much larger terrain being clipped to the size of the table. The other scene objects aren't clipped, since they use unmodifed standard shaders.
Here's a pic showing the terrain clipping in the editor.
You can see the clipping planes around the visible part of the terrain, and that other objects (trees etc) are not clipped and appear off the edge of the table.
The way I've done it involves adding parameters to each shader to define the clipping planes. This means customizing every shader I want to clip, which was fine when I was considering just terrain.
While this works, I'm not sure it's a great approach for hundreds of scene objects. I would need to modify whatever shaders I'm using, and then I'd have to be setting additional shader parameters every update for every object.
Not being an expert in Unity, I'm wondering if there are other approaches that are not "per shader" based that I might investigate?
The end goal is to render a scene within the bounds of some plane.
One easy way would be to use Box Colliders as triggers on each side of your plane. You could then turn off Renderers on objects staying in the trigger with OnTriggerEnter/OnTriggerStay and turn them on with OnTriggerExit.
You can also use Bounds.Contains.

Offsetting the rendered result of a camera in Unity

I am trying to make everything that is rendered by my perspective "Camera A" appear 100 points higher. This is due to the fact that my App has an interface with an open space on the upper part.
My app uses face detection to simulate the face movement into an in game avatar. To do this I compute the "Model-View-Matrix" to set it into the camera's "worldToCameraMatrix".
So far this works well, but everything is rendered with the center as the origin, now i want to move this center origin a certain distance "up" so that it matches my interface.
Is there a way to tell Unity to offset the rendered camera result?
An alternative I thought about is to render into a texture, then I can just move the texture itself, but I thought there must be an easier way.
By the way, my main camera is orthographic, and i use this one to render the camera texture. In this case simply moving the rendering game object quad up does the trick.
I found a property called "pixelRect", the description says:
Where on the screen is the camera rendered in pixel coordinates.
However moving the center up seems to scale down my objects.
You can set the viewport rect/orthosize so that its offset or you can render to a render texture and render that as a overlay with a offset or diffirence in scale.
Cheers

Purpose of mipmaps for 2D sprites?

In current Unity,
For use in Unity.UI as conventional UI ..
for any "Sprite (2D and UI)", in fact it always defaults to having "Generate Mip Maps" turned ON. Every time you drop an image in, you have to turn that "off" and apply.
As noted in the comments, these days you can actually use world space UI canvasses, and indeed advanced users may indeed have (say) "buttons that float over the head of Zelda and they are in the far distance". However if you're a everyday Unity user adding a button, just turn it off :)
In Unity, "sprites" can still be positioned in 3D space. For example, on a world space canvas. Furthermore, mipmaps are used when the sprite is scaled. This is because the mipmap sampling is determined by the texel size rather than the distance.
If a sprite is flat and perfectly scaled then there is no reason to use mipmaps. This would likely apply to your icon example.
I suspect that it is enabled by default for 2D games where sprites will often not be perfectly scaled. To clarify, a sprite does not need to be on a canvas. Sprites can exist as their own GameObject with a Sprite Renderer (not on a canvas.) When this is the case, scaling the camera view will change the sprite's size on the screen resulting in mipmapping due to the texel size changing. This results in making the sprite always perfectly scaled challenging without a canvas.

Unexpected sprite tearing?

I making a 2d game and I am having some very random problems... My sprites are not displaying as they should be. Below is a screen shot of what I see when I run my game. This bug is present in the game window and the scene window, both when the game is running and not running. And the glitch is not because of the tiles being offset (The background is made up of tiles), if you look closely the player sprite is also glitched up. I have tried restarting unity and my computer, one of which has worked. I have been having this problem ever since i started using tilesets (using one PNG image and cropping out smaller sprites by setting the sprite mode to multiple, instad of using just one PNG image where there is only one sprite and the sprite mode is singular). How do I fix this? (BTW I am using Oryx's lo-fi fantasy and sci-fi sprites at www.oryxdesignlab.com)
This happend when your atlas is not sliced correctly. When you making 2D game there are several important things about sprites.
Set Filter Mode to Point
Set Fromat to 16 bit or TrueColor (in special cases)
Make sure that your atlas is sliced properly.
Edit:
Adjust "Max Size" - make sure is value represents number equal or bigger than spritesheet size
The best way in sliceing atlas in Sprite Editor is NOT to use automatic. I got glithes like that in my project too and those steps can eliminate them.
Have a look at your Sprite in the Sprite Editor. I think you can get to it by double clicking.
Your sprites will have sort of boxes around them to identify each individual sprite (if you set your texture to multiple). One of these boxes will not have sliced correctly, and will have overlapped into another sprite, hence why you're seeing what looks like the arm of another sprite in your picture :)