Problems with the BoxCollider2D and Pivot - c#

I make a 2D game in Unity. I have a player with BoxCollider2D and Objects with BoxCollider2D and the player can't go through these Objects like it should but when the player walks from down to the top (topdown PixelRPG) Than he got blocked very early. I will try to show it in the pictures. I tried to change the size of the colliders the layers and more but can't find a solution. ProblemCollision2DChest
Collision2DNPC
Collision2DWindow
NoProblem
NoProblemswithObjects
ProblemAgainfromdowntoTop

From my experience, it looks like you simply need to resize your BoxCollider2D to fit the player.
You can either use the Edit Collider button, and in the Unity viewport you can click and drag to change the size of the collider, or change the Size value in the inspector. Since your collider seems to be too large vertically, change the Y value to be smaller.

Related

Making an object move through another and keep going?

I am currently making a sidescrolling game in Unity 2D where an enemy rises up, and then dives down to the player, but then keeps going until it is off screen, like how this image shows. 1 I can get the enemy to collide with the player but I can't make it keeping in that direction until off screen. How would I be able to do this?
You have to make your collider to trigger (it's a checkbox on your collider)
That means your object will not be affected by physics when it touch another collider, but it can calls functions.
You have to replace OnCollisionEnter/OnCollisionEnter2D, etc by OnTriggerEnter/OnTriggerEnter2D, etc.
Be careful, the parameter of the function may change too (Collision/Collider).

Unity 2D C# Instantiate sprite on canvas. Can't find what's wrong

I read many questions about this, but I still can't find what my problem is...
I'm trying to instantiate a prefab at the canvas. It is compose of a button and a sprite. The button looks ok, but the sprite is not visible at the Game (but is visible at the Scene).
I'm doing something wrong, but I can't see what...
[SerializeField] GameObject finishedLevel;
private void Start()
{
finishedLevel = Instantiate(finishedLevel, transform.position, transform.rotation);
finishedLevel.transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform, false);
}
SpriteRenderer is not made to be used with the Canvas. You are confusing and misusing the two.
SpriteRenderer is used for displaying 2D Objects like a 2D animated character or a 2D environment. You can attach Rigidbody2D and Colliders to SpriteRenderer.
Canvas is used for UI display only. It is used for displaying things such as UI texts, buttons, sliders, scrollbars and images. You shouldn't attach Rigidbody2D and Colliders to it or its child objects.
With the explanation above, you should be able to determine which one to use. If you just need to display image under a Canvas, use the Image, or RawImage component since they are part of the UI system. This should work but do not make SpriteRenderer a child of a Canvas. If you have to use SpriteRenderer, make it its own object or under another object but it should not be under a Canvas. You may find Unity's UI tutorial useful.

Moving 2D collider with animation

I was trying to create a basic cricket game. I created a batting animation, and added polygon collider. But I'm confused about adding collider to the moving bat in the animation.
Or are there any other ways to do it?
you can move and resize the bat's polygon collider (make sure it's a 2d collider)
in the animation itself on every single frame of the animation.
simply click "add property" and look for your collider, once you added the collider to the animation you can start resizing him (size) and also moving it around (offset)

Dragger with collider on Unity 3D

I´m making a simple CAD application on Unity 3D using C# to configure closets. I'm trying to limit the movement of the shelves so you can move them only on the hole, so I have a dragger attached to every piece of the closet I want to move, but obviously they can cross with each other.
I thought I could use the collider system that Unity has to limit this movement but since I never worked with Unity before I´m kinda lost. This is my dragger so far:
mousePosition = new Vector3(Input.mousePosition.x, cubo_tmp.transform.position.y, distance);
objPosition = Camera.main.ScreenToWorldPoint(mousePosition);
objPosition.Set(objPosition.x, cubo_tmp.transform.position.y, cubo_tmp.transform.position.z);
I keep y and z components so it can move only in one direction.
Box Colliders are best for cuboid shaped objects.
In the editor, if you click GameObject > 3d Object > Cube, Unity will add a cube with a Box Collider to your scene:
If you want to add a Box Collider to a GameObject that doesn't have one, click Add Component in the Inspector panel and type in "Box Collider" to find it. It looks like this:
Note: If the IsTrigger box is checked, the Collider acts as a Trigger rather than a Collider.
A GameObject with a Collider will "collide" with any other GameObject that also has a Collider, with a small exception involving 2 Mesh Colliders that are both set to Convex.
EDIT: I think you are able to intersect the objects because you are modifying Transform.position directly in your code. This might override the collision behaviour.
Also Go to Edit > Project Settings > Physics and make sure your layer collision matrix allows collisions for the layers on which your objects are placed.

Unity 2D line collider

I try to do the following: I have a point A at -4x-4y and a point B at 4x 0y. I want to make a colliding line from position A to position B.
I tried to do it with a linerenderer but I can't get the line to collide with my other 2d objects.
My other tought was to calculate the center of the points and the rotation and do it with a box collider but that seems to be really complicated and hacky.
Is there a simple way to achieve this?
Thanks in advance
You can use PolygonCollider2D, it's automatically create collider for sprites, and if you are not satisfied results, you can edit it by clicking Edit Collider in inspector, or trought Unity's API.
I think you must have a Rigidbody2D attached with your other 2D object. Then this will work 100%. You can use any collider it doesn't matter.

Categories

Resources