Unity 2D line collider - c#

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.

Related

Avoid the character jumping because of capsule collider

My unity game have a character with a capsule collider. When the character hit another collider on the spherical part of the capsule, the character is send in the air. How to avoid that?
Here is a video to explain the problem
I already have some solutions but it doesn't really work:
Use a cylinder collider instead of capsule, but it doesn't exist. I tried to make one with meshcollider and the result is not as good as a caspule collider since the meshcollider is not perfectly rounded. I also tried to make one with many rect collider but it produce same problems as meshcollider.
Set a really big gravity force when the character is on the ground, but the problem is that I have inclined planes in the game, so it doesn't work neither.
Thank you for your help
I might not be right but I think you should delete the capsule collider and add a Mesh Collider or you could also just add colliders to each part of the player which would take longer but I think that's what you're needing. So add sphere colliders to the head, box colliders to the arms and etc.
You can propably fix the unwanted behavior:
with a different physics material for your colliders. Removing bounciness may help.
by adjusting scales of your objects.
by changing the collider of the ground from a box collider with sharp edges to a mesh collider with slopes on the edges.
by adding a code that will snap your character to the ground.
There is a great tutorial on character movement from Catlike Coding I can recommend: A series about controlling the movement of a character.

Walking up and down 2d slopes (pixel art)

I'm having an issue where I can't walk up slopes in unity. My art is pixel so I understand the slope is made with a gap of 1 pixel to create the actual slope, but I have no idea how to fix it so I can just walk up and down them normally. Currently, going down them makes it a little bouncy and going up them is impossible unless you jump. Any help would be appreciated! Here is what the slope looks like:
Edit: Collider looks like this but I don't know how to fix it:
Sprites automatically have a polygon collider created for them when imported into the project. This polygon collider drives the tilemap polygon collider shapes.
You can modify the physics shape for a sprite to smooth it out and remove this unwanted when going up a ramp. Custom Physics Shape documentation
Another important thing to note in your specific problem: Often when a character has a "box-like" shape, they will get snagged on edges and small collider deviations. Unless your game's playstyle is based around a box-shaped entity and interactions, it's usually recommended to use a rounded collider for the moving characters (like a 2d or 3d capsule collider).

Colliders not working in Unity3D 2D Project

I have 2 colliders, its a game where I have a ball, and a Labyritnh. The ball should pass through the labyrinth and collides with the labyrinth's walls.
But I have a problem, the physic aren't working that well, the collider don't work.
I have a rigidbody and a collider in both of these objects.
Also, I have a problem with choosing box or edge colliders.
Because it is a labyrinth, I need to design the colliders with some complex shapes.
I have used edges colliders and box colliders but the ball is passing through all these colliders like nothing, It seems like its not working.
Can someone help me?
you need a rigid body object attached to at least one of them for the colloders to work.
make each wall individually and use rectangel colliders. add a rigidbody component, and set that rigid body to kinematic. should work then.

Character gets stranded with polygon collider in sloped platform

I have used polygon collider for the hills and terrain like sprite in my 2d game.
I used the same polygon collider for my hero character. Here is my problem the movement is smooth when i used box collider in hills and all that but it makes my character look like floating in air. so i opt to use polygon collider for hills.
However polygon collider produces some small pit or irregular shape to fix the best possible shape of collider for hill. Here my player or hero get stuck in those pits or irregular shape. Even the slightest pit cause my player to strand. I tried various way to mitigate all those errors but no any result.
Please suggest to me some way to make my character movement smooth in uphill.
Instead of using one collider, try 2 different ones for your player. Put a box collider around his mid section and head, and a circle/capsule collider that goes around his feet. That way you get the circle/capsule collider will reduce the chance of your player getting stuck on awkward locations :-)
You can always try to edit the collider yourself, or even start with an EdgeCollider and make the shape yourself. Anyway, I don't know how irregular those "pits" are, but you can always edit in a very easy way the collider in 2D.
Oh, and you can edit the collider in the component itself, and then in the editor window.

Unity C# raycasting from center of an object to find mesh intersection

I'm trying to make a respawn system for a game in unity that starts the character back on the last platform they were on.
As it is currently, it keeps track of which platform they were last grounded on with onCollisionEnter and detects if onCollisionExit touches an out of bounds area.
I need to find the position of a face on the mesh with the y axis (assuming the best way is to do a raycast on the global y axis from the center of the platform) and add the height of the character/2 to determine where to respawn the character.
I'm very new to unity and c#, so I've never done a raycast before and I'm not sure if it's possible to raycast from inside an object to find it's mesh in a given direction, or if there is a better/more efficient way.
Thanks in advance :)
"if it's possible to raycast from inside an object to find it's mesh in a given direction"
You can place a empty gameobject at the center of your mesh (make it child of your mesh ) then pass the position of this empty gameobject to raycast origin.
I usually make re-spawn system with triggers. If you explain little bit more what actually you want to do. I'll try to guide you in that particular direction.

Categories

Resources