I've a simple project with a few objects.
I tried to move "player" object (Marked in the picture) to top of other objects but still it is in backward!
How to bring it to forward?
We need some more information to give an accurate answer for your exact situation, such as the properties of the Sprite Renderer (assuming Player is a sprite) and the properties of your Canvas and UI elements (especially if it is a world space canvas), and any code you might be using to instantiate, render, or move the player.
In general though, for sprites, you can bring a sprite in front of other sprites by either changing the Z value of the transform (negative Z is closer to the camera), or better by changing the Order in Layer of the player sprite renderer, and make sure it is in a layer that is in front of the other sprites.
As for Canvas UI elements, the element at the bottom of the hierarchy will be rendered last, meaning it will show in front of everything. Think of a UI hierarchy like stacking papers on top of each other. The first piece of paper placed down will be at the bottom of the pile.
Related
I am creating a 2D grid based game in Unity. I am using Tile maps for background (one grid and two tile maps).
On the first tile map I have the background (like grass) and on the second one I have things I add, like a hole in the ground. Now I add game objects to the scene (a sprite tree). I have two BoxCollider2D on the tree, first one is just a small one at the bottom of the tree and the other one covers the whole tree but is set as trigger (I have a script that automatically fades out the tree when user is behind it).
The problem is that the user keeps getting stuck at the collider (I think) at the bottom of the tree. It works from some angles. I tried creating a small 2D square too and sometimes I get stuck to that too. What am I missing?
I have added a custom material that has friction set to 0 on both the Player and the tree.
I don't know if it is relevant but the tree is quite big so I had to set its size to 0.5 instead of 1.
remove the interaction between the player and the tree, see in project setting, modify the script to work with a raycast
or just make the tree collider a Trigger..
I heard that game objects are drawn in the same order they appear in the Hierarchy. But in my case it doesn't work.
For example I wanted the wolf is placed in front of the rabbit, but it doesn't work.
Is there some way to make it with sorting objects according in hierarchy or I can make it only with layers?
The hierarchy sorting you speak of only works in the canvas - so for example with RectTransforms and Images. However I guess you want to use Sprites. SpriteRenderer component has a Order in layer property. Plus Sprites are more lightweight than Images with Transparency. Or you could just move Transforms closer and further away from the camera (even if your game is 2D/ using an orthogonal camera). If everything fails you could change the RenderQueue of the Materials.
Showing the general setup of the scene and where the sprite is
the sprite is "behind" the background but the Z-value should be correct
I have tried solutions such as changing the render mode of the canvas; the different options do not work and "World Space" turns the game view into the default background color. I could not figure this out after a day of reading up manuals/guides and looking at other questions. Thank you for helping me!
The issue is you are trying to use a SpriteRenderer on a Canvas object. Remove the SpriteRenderer and either use an Image or RawImage component instead.
Another note regarding UI that you will inevitably run into is unless specified otherwise, objects further down in a Canvas hierarchy will render on top of those above it.
I would also avoid changing the z-axis on all of your objects. Make Canvas groups that have different sorting layers to render groups on top of one another. Place objects in a specific order in the hierarchy to get the draw order you desire. Messing with the z-axis in a 2D scene can be a headache down the line and is not very modular. If you want to change sort order later, the specific z offset you set to each object will need to change.
I would like to move a tile with drag and drop in Unity 2D. The tile is a sprite. The scene is an 'Unblock me' or 'Blocked in' like gameplay.
Because the tiles in real life correspond to physical objects it seemed be to a good idea to model them with colliders and rigidbody. The border of the table surrunded with invisible colliders. I hoped these will constrain the moves of the tiles realistic, when the player moves them.
Then I implemented a simple (mouse based) drag and drop behavior which is worked perfectly except the moved tile penetrates to other tiles and the border, and sometimes jumps over them. Then I learned if I am overriding physics by explicitly setting transforms position (which I do exactly in my drag and drop implementation), this will happen. OK I accept, I should set only forces, ect. on rigidbody never directly the position.
Now the question:
I am stuck here. I still want to drag and drop like user experience, and some realistic visual result. When in the real life a player moves a tile, it seems it is "glued" to its finger. How can I achieve this (ot at least similar) with just applying forces? Any suggestions or point similar existing sample/blog code?
(I know as a backup plan I can omit all the physics and constrain the tile positions by code, and create some tweens to move the tiles. Is the real solution (what I am asking for) so complicated I should vote on this backup plan?)
Edit
According to comments I've added a video:
There is nothing wrong with the way you are manipulating your dragging. The beauty of developing is being able to do things in your own way. If it works for your game, then don't fret.
Now, i recommend:
Create a new physic material. Assets > Create > Physic Material
Set your new physic material inspector settings both to 0
Attach the physic material to all your wall object colliders. This should allow for your object being dragged to move smoothly against the walls without chopping.
Do a check to see if your mouse is over another collider. If so, then stop the movement in that direction.
Since your movements seem to always be on a single axis, on collision, tell your object to snap to the edge of the wall object. You know the Wall position and scales, also you have the position and scales of the object being dragged. with that you can write a function that will offset it to the correct position when the collision occurs.
Let me know if any of that works out :P
How does one create the game "area" for a scroller game?
How does one then put various obstacles with collision detection along this scrolled environment.
I want to try out a project which will allow the user to scroll to a certain direction in order to progress through the game.
How does one map the objects within the environment and then move what I guess is the "camera", the view of the environment.
Thanks
The trick is that there is no "area". The only bits that exist are what's under the camera (the view you currently see) and a small surrounding area giving you time to prepare more world in the direction you are moving..
Your world coordinates need to be defined as do the starting coordinates for the view. You use tiles to create the view - at its simplest that is 9 tiles, one you are currently "on" and one in each direction. If you look at the keyboard numberpad you are "on" the 5. If you move a little to the top right you are displaying parts of tiles 8, 9, 5 & 6. At that point you would create new tiles in case you move further. As you leave tile 5 you would probably release tiles 4, 1 & 2. Nine tiles may not be the optimal number of course.
If doing this with UIViews (probably not the high-performance choice) you are probably going to define one big view that can handle all the tiles and tile them onto the view (add and remove subviews), setting the large view's frame to define your camera position. As you move you change the frame to move your camera, when you need to shuffle tiles you move both the tiles and the frame to recenter giving room to move further within the coordinates of your view.
Collision detection is pretty simple since you define your own dimensions (the thing representing "you" in this world) and objects in your view have dimensions you can check against. CGRectIntersectsRect might be the simplest function to use but if you have irregularly-sized views it will get more complicated.
This answer about implementing a cyclic UIScrollView is a similar idea but it only handles scrolling on one direction.
This is a pretty common topic and if you google you will find a lot of sample code and tutorials around.
From the game logic side:
All your objects (lets call them gameobjects) should have a coordinate (x and y position) in your game world. You will keep all your gameobjects in a list. Your player object will be a gameobject too. Usually your "camera" position will be relative to your player objects position. I.e. the player will always be in the center of the screen. To determine the current "screen" position of your objects you will just subtract the camera position from your objects "world" position. Collision is usually made with simple rectangular overlap checks. You give all your objects a width and a height attribute and do your collision checks using x, y, width and height.
From the display side:
If you want to display many objects (i.e. Player, Enemies, Obstacles and so on) the best way to implement something like this is to use an OpenGL View. In this view you can display all Objects as Textures that are mapped to Polygons. You can use a library such as cocos2d which already has all of the code to achieve this easily.