Unity3d: Object's pass through each other? - c#

Object's(collider) pass through each other? How to fix?
https://youtu.be/gJKCEMNI9ls

One of your colliders might have the IsTrigger-checkbox ticked, which is causing the ball to jump out. But I think it's more likely to be that the rigidbody isn't checking for collisions often enough so that when you rotate quickly it clips out.
Change
Collision Detection: Discrete
to
Collision Detection: Continous (or Dynamic)
In your Sphere's rigidbody.
Read more about collision detection here:
Discrete: Use Discreet collision detection against all other colliders in the scene. Other colliders will use Discreet collision detection when testing for collision against it. Used for normal collisions (This is the default value).
Continuous: Use Discrete collision detection against dynamic colliders (with a rigidbody) and continuous collision detection against static MeshColliders (without a rigidbody). Rigidbodies set to Continuous Dynamic will use continuous collision detection when testing for collision against this rigidbody. Other rigidbodies will use Discreet Collision detection. Used for objects which the Continuous Dynamic detection needs to collide with. (This has a big impact on physics performance, leave it set to Discrete, if you don’t have issues with collisions of fast objects)
Continuous Dynamic: Use continuous collision detection against objects set to Continuous and Continuous Dynamic Collision. It will also use continuous collision detection against static MeshColliders (without a rigidbody). For all other colliders it uses discreet collision detection. Used for fast moving objects.

Ok the colliders don't prevent you overlapping gameobjects in the editor. You just need to line up the walls of the maze so they don't cross over (visually) but you can overlap the actual colliders so there are no holes in the maze.
If you really don't want them to overlap then change the position or size of the colliders in the editor.
You can change the centre and size as shown in the image so they don't overlap.

Just set the transform's parent of everything that is on the maze to the maze's transform. Simple as that and it solved for me.

Related

Sibling game objects not colliding in Unity2D

I have a game in which waves of enemies are spawned by storing an arrangement of enemies as children of an empty game object, which I can then save as a prefab and instantiate with an enemy spawner script, but there is no collision between the enemies, and the only reason I can think of is because of some physics optimization due to them being siblings. These are my collider and rigidbody settings for the enemies: Any idea why this happens? Thanks!
This is likely because your Rigidbody constraints are set to freeze movement on both the X and Y axes:
This means that any movement you would normally expect from simulated physical interactions (for example, changing velocity from colliding with another enemy) will be ignored for this Rigidbody. See the Unity docs for more details on what they do.
The simple fix here would be to uncheck those checkboxes, so that you rely on the physics system to handle these interactions. If you want to fully control them through code though, you could technically write code in OnCollisionEnter() to handle the collision between two enemies - but that would be a much more involved solution.

How to make my button change on collision

Hey I am trying to make my sprite button go from green to red and backwards when my player collides with it. I have a boxcollider2d as is trigger, and a script
script : https://gyazo.com/ec64a2bca5b23526c6949bf18cb50a0d
I think this is some of it but I dont get anything at all when colliding with the button can anyone enlighten me on this problem would be very appreciated.
Thanks in advance
It is important to distinguish the difference between Colliders and Triggers.
Colliders are used by the Physics system in order to make it so object do not penetrate or overlap each other. Triggers on the other hand are made so that you can check for overlapping areas, an example is a Ring in Sonic. Sonic collects the rings but they do not stop him from passing, think of these as triggers. If they were colliders sonic would jump into them and bounce off.
In Unity the process for creating a trigger is the same for creating a collider, with 1 additional step, and that is marking a check box.
As such it is also important to know which functions you should use in each case.
OnCollision... For Colliders, (Or Colliders not marked as triggers)
OnTrigger... for Triggers, (Marking a collider in unity as a trigger makes it a trigger.)
To just add a bit more detail, It is also important to use the correct collider types for the correct dimensions of the game, 2d Colliders/Triggers do not interact with 3d Colliders/Triggers. More so physics components: 2d Rigidbodies do not works with 3d colliders, and 3d rigidbodies do not work with 2d colliders/triggers.
As such there are functions for each 2d, 3d, collision, and trigger.
Another important factor is to ensure if you want to use these functions atleast one of the objects have to have a rigidbody of the correct type(2d or 3d).
As such the problem you were running into was using OnCollisionEnter2D for a "collider" that was marked as a trigger, in this case OnCollisionEnter2D is not called, but OnTriggerEnter2D is called.

OnTriggerEnter Unity c# trigger not registering

I am trying to have a bullet item that is tagged with "Bullet" hit a target that this code is attached to. I have Is Trigger ticked on the bullet. Collision is working on the target as I can hit into it with the player. Any help would be greatly appreciated.
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Bullet"))
{
Debug.Log ("Hit!");
Destroy(this);
}
}
A bullet (collision detection type - discrete) travelling fast will go through a static wall (simply a box collider OR a mesh collider. Its not a dynamic object as there's no rigidbody) without registering OnTriggerEnter.
Continuous Collision Detection : To solve this issue we can change the collision detection type to continuous for the bullet. Bullet collision detection will become continuous and will not miss the wall even at fast speed.
Continuous Dynamic Collision Detection : Also if we want the bullet to hit a dynamic object(having rigidBody,eg. moving enemy character), then we need to set both of these object to Continuous collision detection and at least one of these objects to Continuous Dynamic collision detection.

optimizing a dynamic mesh for a collider

I have a dynamic mesh which is created and updated dynamically according to the game,and it has many vertices. However when I use it as a mesh for its collider, I get the warning that it has more then 255 polygons. I read it is what slows the game. However the mesh has to be very flexible and growble, and it has to detects collisions. How can I optimize it in runtime foe the collider which has only to have the current approximate shape of it?
I would recommend using capsule colliders. When you expand the width of the snake, scale the width of the colliders. When you add length to the snake, add another collider onto the end. This way different parts of your snake can have different sized colliders. Depending on how much the width varies you may need to add more or less colliders to approximate its shape.
But it's always better to have many primitive colliders than one very complex mesh collider.

Collision Detection for arbitrarily sized, positioned and rotated rectangles in XNA

I'm working with xna in C# and in my game I will have a variety of space ships flying all over the place. They will each have an arbitrary rotation, size and position in space and I need a method to determine when they collide. Ideally the method would take two Rectangles, two doubles and two Vector2s for size, rotation and position respectively and return a boolean that indicates whether they have intersected or not.
Have a look at these links:
Collision Detection Overview
Collision Detection Matrices
Putting Collision Detection Into Practice
They show you a way to do pixel-based collision detection, which is more accurate than rectangle-based for irregularly shaped objects.
Update 2021-01-17 (Martin Senne)
Links of XNA have moved to
XNA
XNA - Collision
You could also consider just using an out of the box solution for this and integrating something like the Farseer Physics Engine:
http://farseerphysics.codeplex.com/
These rectangles you describe are called OBB (Oriented Bounding Boxes)
The way to do collisions between them is using the 'Separating axis theorem'
A really nice page describing it in detail with lots of pictures can be found here

Categories

Resources