unity5, box collider 2d size doesn't match platform - unity3d

I created a 1x7 platform. then added a box collider 2D and set the size to 7. you can see in the picture. the platform and box collider are not matching. how can i get it fixed?

X Offset is the easy answer and definitely not a bad one, but maybe not the best one since it could make this problem show up in other areas.
BoxCollider2D by default has its origin at the center of the object ( where the translate tools are showing).
Since the translate tool is at the middle of the platform, the collider should be centered on it as well.
There are several possibilities that I can think of:
The sprites do not have their pivot at the center (If you want it
that way, use Offset to center the box)
The children of the selected object are offset (They should be
shifted over to line up with everything else, unless you need them
this way for some reason)

Create an empty Gameobject - add any sprite objects that you need in there - press the "GameObject" button in the menu and select "Centered on Children" in order for the empty object to be centered properly. This will have to be done each time you add a new object into the bulk. Hope this helps.

Related

Set a custom sorting point (without changing the pivot)?

(Unity2D) Is there a way to set a custom sorting point for a spriterenderer without changing the pivot? I only see default and custom based on pivot as options, but I don't know if you can change them via script, has anyone done something like this?
This is a workaround that works for me, albeit a bit odd:
Make your game object child of an empty GameObject, and add a Sorting Group component to the empty GameObject. Move the empty GameObject to where you want your "pivot" to be.
This way, the empty parent with only the Sorting Group component will be used to sort. You'll have to add any movement code and colliders to the new parent, instead, so effectively you'll be moving the empty game object, and the child is just responsible for rendering the sprite with respect to the "pivot."
SpriteRenderer sort point
By default, a Sprite’s Sort Point is set to its Center, and Unity measures the distance between the camera’s Transform position and the Center of the Sprite to determine their render order.
To set to a different Sort Point from the Center, select the Pivot option. Edit the Sprite’s Pivot position in the Sprite Editor.
No, there is not a way to change the sorting point without changing the pivot. The sort point is the same as the sort point. What is your use case that you need to change the sorting point and cannot use sorting layers?

Unity all objects have same position

I have a 3d building model in my Unity project. It has many children like doors, walls etc. The problem is, all of the children points to same position in the Unity world (24.97, -2.08, 19.35). Their transforms show this position. And this position is far away from their actual one. How can i fix this?
I tried freeing all children from parent but this didn't change anything.
I want them to show their real position, which appears with move tool when we click upon them.
Here is the image link
It seems that this is simply their pivot point exported "wrongly" from the 3D editor your model was made with.
This won't change until you export it correctly from a 3D editor (Blender, Maya, etc).
Unity is not made for 3D mesh modeling and therefore in Unity itself you can't change the pivot points.
There is a very simple fix
Add a new empty GameObject
In the Inspector go to the Transform component, click on the context menu and hit Reset (you also simply set it to position 0,0,0 rotation 0,0,0 and scale 1,1,1) assuming the pivot should be at 0,0,0
Now drag and drop all objects into the empty GameObject
=> You have now one parent object with correct pivot.
By wrapping it in a parent object the childrens pivots don't matter anymore. You can simply do all translation, rotation and scaling on the parent object with the correct pivot and don't have to care about that "wrong" position at all.

finding submesh in Unity 2017

How can I find submesh under my mouse NOT using Raycasting? Is any way to do it? I know how to do it clicking on object and using raycast but completely haven't idea how to id it without it. I need it because of bug in Unity - I can't update version of Unity, so I need to find any solution.
You can see if your mesh contains the mouse.
You get the position and the size of the mesh which allows you to create a square or a cube (whichever you need) and then you just see if your mouse fits within that square or a cube. If so, then you want to select that mesh.
https://tutorialedge.net/gamedev/aabb-collision-detection-tutorial/
or
If your object was a circle/sphere then you can perform a simple distance check between mesh's origin and mouse, if the distance is <= than the radius of the object, then you want to select that mesh.
http://cgp.wikidot.com/circle-to-circle-collision-detection
It is similar, just instead of using 2nd object, you are using a mouse.

Preventing an object from landing on its side; ensuring it always lands face-up or face-down

Currently I am doing a project of shell toss in Unity. The shell is made in Blender and imported in Unity. I attached a single box collider to the shell.
Now when I apply the random force to the shell to flip it, it sometimes rests in the vertical up position when either a result of heads (face-up) or tails (face-down) should have occurred.
I have tried changing the axis, increasing the gravity. But none of these solutions worked.
What I have done for now is when the shell is in a resting position after it lands, I rotate the shell 90 degrees - but this is not a great solution, since it is slow and takes time to check whether the shell has come to rest or not.
I am looking for the better idea so that the shell should only rests in a heads or tails state.
If you use a box collider for your shells, you are going to end up in this problematic situation where the flipped shell sometimes ends up sitting sideways, instead of face-up or face-down.
In this situation, I would recommend making use of Capsule Colliders, which are cylindrical colliders with a rounded top and bottom. If a capsule collider lands on either end, it'll fall over sideways:
However, one capsule collider isn't enough - otherwise, the shell will start rolling around after it falls over. I suggest 2 or 3, oriented in a cross such that their tips correspond to the sides of the shell:
Now, if the shell lands on any of its sides, it will topple over onto one of the faces of the cross - either face-up, or face-down. To add capsule colliders to your GameObjects, just click on Add Component, and select Physics > Capsule Collider. Then modify the properties to get them into the desired positions:
The Direction you choose should probably be X-Axis and Z-Axis. Once you have the correct orientation, change the Center value to move the colliders to the right positions to make a cross through the shell. Then, alter the Radius of the colliders to affect the thickness of the cross, and the Height so their tips line up with the edges of the shell.
Hope this helps! Let me know if you have any questions.

Unity 2D game Shooting Target Problems

so basically i have a target like the type for archery the 3 rings (bullseye, inner circle and outer circle)
now i basically used a cylinder to create these and then added to them rigidbody2D and a circleCollider 2D , now my problem is because the rings are essentially on top of each other i have them layered out on the z axis a little to make them all visible but when it comes to doing a raycast2d on the target it isnt picking correct ones up for example it goes from outer circle straight to bullseye and skips out inner circle yet all have colliders set up the same way
i cant figure out a way to overcome this and if not ill have to change to a different target where nothing overlaps in order to get it to work but i would really like the archery type targets
Thanks
You could just vary the distance from the camera for each ring so that the ones over the top are hit first.
Alternatively you could add tags to the three rings, use raycast all, and check the tags of all hit colliders to decide which one was hit first. For example, if all 3 register a hit, then you know the center was hit, and if the outer 2 register then you know it's the inner ring, and so on.
http://docs.unity3d.com/ScriptReference/Physics.RaycastAll.html