How to detect mesh collision in Unity3d? - c#

Can we detect mesh collision in unity3d? I want to show some texture on collision, currently I am using box colliders, that's why its surface/edge do not match with the object body mesh, Also even if I get the hit point on the surface of mesh, I don't know how to put a texture on a mesh at specific position over mesh, is there any in-built component specific for the same kind of requirement or a workaround of this in unity3d?
As we can see in picture the collision has been detected inside the game object because the box collider is inside the target object mesh
Note: here the actual mesh I have replaced with a dummy cube mesh
But is there any way to detect when bullet collider/mesh was actually going through the mesh of the target object as in below image I have shown where exactly I want to detect the hit point between target object mesh and bullet mesh/collider also how to draw a texture over here (the hit point on the mesh).

Short answer: No
Because that's not how that works.
Long answer:
If you want the mesh to be used as the collider, you should set the mesh as the target for a MeshCollider component. However, mesh colliders are very expensive: Unity needs to recalculate them every time they move, scale, rotate, or otherwise change their boundaries.
There is, however this asset which will perform raycasts against the renderer mesh, without the need for a collider, but I have no idea what kind of performance hit that will have.

Related

2D unity platform game, conflict with Tilemap colliders

I'm trying to create a platform game, watching videos and tutorials I see that they use the Tile Palette option very frequently.
When creating the 2D Tilemap Collider, each square has its collider, and I guess that makes it consume a lot of resources, so to solve that I create a 2D Composite collider.
The problem is when the player collides with the collider of a vertical wall, causing the player's rigidBody.velocity.y to add force and that doesn't allow me to execute the jump.
https://clipchamp.com/watch/nFKCJOv7DEX
Here you can see under the scene of the game the debug.log that ends up showing me a speed to the Y axis
This project would be for android, and I would like to optimize it as much as possible. Do you have any idea so that in that collision I do not add force Y?
I have tried to change the speed in an onColliderStay2d and put the vertical tilemaps with another tag to differentiate them from the ground, but nothing.
Instead of checking your y velocity to see if you are grounded or not I recommend just making a rectangular area under your player character and check if it overlaps with the ground using Physics2D.OverlapArea. It's way less error prone.
Also, just because composite collider creates less shapes than tilemap collider doesn't mean it is less performant, the physics system doesn't iterate them one by one. Do yourself a favor and just use the tilemap collider instead.

Unity 2D games, I have troubles with 2D collliders

I have a lot of problems with some 2D colliders.
In the past, they work, but now they are horrible:))
I already try with Kinematics, OnCollision2DEnter, OnTriggerEnter etc.
The objects are already on the same layer, the same position on the Z-axis, they interact with each other but my game can't detect the collisions.
The rigidbody and collider don't have material but the objects have skins enter image description hereenter image description here
I've realized that the gravity scale of your ground object is above 1. You can make it 0 so it does not get affected by gravity. Gravity Scale is on your ground object's Rigidbody2D component right below angular drag. Although I could be wrong since what kind of collisions you are expecting and your movement code, that should help resolve your problems.

How to go about Manipulating a (capsule) collider in Unity3d C#

What would be the best way to adjust the height of a rididbody when for example my character goes into crouch position? would it be to get a reference to the rigid body and manually change the height etc, or would a mesh collider be a better option? Im open to any suggestions, I'm just curious what would be the best way to go about doing this.
Rigidbody itself doesn't trigger collisions, only colliders does so you should change the height of collider.
In my opinion cylinder collider is a better option then mesh collider because it produces less overhead, is easy to scale (for example by just changing height) and is quite good approximation of humanoid mesh.
So just keep a reference to cylinder collider attached to game object and changed height of collider whenever your character goes into crunch position.

How to Draw a Ray Along a Sprite in Unity

I am trying to cast a ray along a sprite in Unity. I have Created an empty GameObject and made it as Parent to different Textures of Head, Hand, Chest etc of a Character to easily Animate it, Now for Melee Combat I would like to Cast a Ray along the Hand Texture while it Animates the Attack but I am Unable to get the Centre of the Hand Texture in the Scene.
I am trying to Access the Sprite by the following Code
Sprite Hand = gameObject.GetComponentsInChildren <Transform> () [4].GetComponentsInChildren <Transform> () [0].gameObject.GetComponent <SpriteRenderer> ().sprite;
This Code is Working for accessing the Hand Transform I verified by Drawing a Ray from the transforms centre
Rather than trying to find it at run time, just link it together in the prefab/gameobject. It's pretty simple to just expose it in the inspector then drag the sprite to the exposed variable. You could do a sprite or a transform, in the example below I used a transform, but if you need to get more data from it then go on ahead and use a sprite.
public Transform Head;
public Transform Hand;
public Transform Chest;
Then you can get the position with Head.position
If you are looking to check for collisions consider placing a collider at each location, then you can toggle them on/off when you want (so the hand collider is disabled unless if the character is punching, then you turn it on for the duration (or part of it) of the punch.) Then you can have a component on just that collider for doing damage or finding a wall or whatever you are trying to accomplish.

Collision between a sphere and floor is inaccurate in unity

This is the setup I have. I am trying to make a game in unity which appears 2d but is actually 3d. I have a simple sphere and a floor, which is made up of cubes placed next to each other(the colliders overlap a little) with the same Y value and Z value. My 2d plane is in X-Y plane(Z being the depth).
Now in the script attached to the sphere, in the Update function, I have used rigidbody.addForce() in the +ve X axis function to move the sphere forward. I have attached rigidbody to the sphere and enabled gravity. The collider of the sphere is the default one.
Now the problem is:
When I run this scene. The sphere moves forward but at the intersection of the colliders, it jumps a bit(very less but still noticable) upward and loses its momentum. It happens at every intersection.
BUT this does NOT happen if I place the sphere on a floor made up of a SINGLE cube(a very long one).
Is this problem arising because of overlapping colliders? How do I solve this issue?
Thanks in advance
I don't think this is fixable. Unity physics is approximate, and even when physics material are set up to 1.0 bounciness, momentum doesn't stay constant — I learned it the hard way when I tried to develop a game depending on constant momentum, and had to write my own little physics simulation for that.

Categories