How to have collision detection without physics in Unity? - unity3d

I am making a simple Unity 2D game. In this there are three gameOjects - 1) The Ground (a long rectangle)2) The player (a square)3) The coin (a circle)Currently i am using colliders on all three. These colliders help the player remain on the Ground and when the player collides with the coin i increment the score.But the problem is that when the player collides with the coin it behaves as if the coin is a physical object and it stops the player.I dont want thisI want that when the player collides with the coin there is no physical interaction between themI tried one thing - switching the colliders in player and coin to isTrigger, but in this case the player and the ground interact in the way they should (the player falls through the ground).What should i do??

Make the interactable objects triggers. That way, you can interract with them, without obstracting your path. To do that, go to the collider Component on the inspector and check the "Is Trigger" checkmark.
Then, go to your scripts and instead of the event OnColliderEnter use the event OnTriggerEnter

Related

How to seperate the collision detection in unity?

I'm new to unity and I'm making a 2d game. Game has an enemy which has a shield with it. The shield( child) has a shield manager script and a collider with it. When it collides with the player it pushed the player back and destroys itself.
And the Enemy(Parent) has an enemy controller script. It has a collider too. And the enemy contoller has an OnCollisionEnter2D function in it for detecting if the player attacks it. But the parents OnColliderEnter2D function being triggered in both of the colliders.
So shield are being destroyed but also the enemy are also too. The parent detects its child's collider.
How can I seperate them?
Is there a way to find which one of this colliders are colliding with the player?
You can put enemy and shield in different layers. And in layer collision matrix, you can disable collision detection between these two layers.
You can find layer collision matrix in Edit > project settings > physics

Prototype 1 Challenge in Unity - adding Rigidbody to game assets

I have added a Rigidbody to each of the obsticles in the Prototype 1 Challenge in Unity. The "player" (plane) game object already has a rigidBody attached. However, when I play the game the plane passes through the obsticles.
How can I make the obsticles solid. I want to use them as triggers to remove points from the player when they collide?
Suggestions:
If you use any collider as a trigger then other objects will pass through it no matter if they are colliders or triggers so just uncheck that property (in your case its box collider) uncheck all of 'is Trigger' then they will become the collider as you want them to be and after that you can have some script which detects collision to check if you collided with obstacle you remove the points from player.
Hope it works... Happy coding :)

Ignore collision in specific circumstances while still using it as a trigger

I am making a 2D platformer type of game. In this platformer there are both the player and "enemies". The problem arises because: I need both enemies and players to collide with the ground so they don't fall through it. I need to detect when a player collides with an enemy so i can register damage. I need the player to be able to walk through an enemy. Having colliders on the feet is not an option because the player may interact with it. What is the best way to approach this kind of specific collision detection?
You can use the Layer Collision Matrix (Edit -> ProjectSettings -> Physics2D) to define exactly which layers can collide with which other layers.
So I would simply use two colliders on the player:
one is not a trigger so it can collide with the ground. Put this on a layer playerGround which collides with the ground layer.
the other one is a trigger and can collide with the enemy layer. Name it e.g. playerEnemy or something.
Since each GameObject can only have one layer your colliders would have to sit on different objects e.g. like
player (Rigidbody2D)
|--GroundCollider(layer: playerGround)
|--EnemyCollider(layer:playerEnemy, isTrigger)
The enemy layer can collide with both the ground and the playerEnemy.
This way both can walk on the ground. Player can walk through enemies but you can use OnTriggerEnter to detect the collisions with enemies.

How to have a pick able power up that has gravity and is not pushed by player?

Im having trouble with colliders and rigid bodies in my game.
I have a powerup that when a player touches gives the player a weapon upgrade, this is simply achieved with onTriggerEnter2D() and setting the collider on the powerup as a trigger. But the problem with that is the trigger does not even detect the ground collider on which the player is running and it just falls through the ground.
When I disable the trigger on the collider, the powerup, when spawned, falls to the ground and stays on it as normal, but then the player can't pick it up as it is just pushed around rather than being run through.
I have a similar problem with my enemies that should kill the player on touch. When I set the collider on them as triggers then they kill the player as expected, but fall through the ground since they have trigger colliders. But when I remove the trigger option, the enemies run on the ground as normal but never touch the player as the colliders of the enemy and that of the player push each other away and the gameObjects never actually touch.
How to solve this?
Thanks
You can use the method OnCollisionEnter2D(Collision) to detect collisions with non-trigger colliders, instead of OnTriggerEnter2D(), and it will still allow you to have your objects collide with the floor and not fall through.

Collisions issues of CharaterController with other box colliders

I have a Player with a CharacterCollider & Coins with box collider. When my player collides with coin, then i m disabling coin in OnControllerCollideHit() with hit.gameObject.active = false (where hit = coin gameObject).
But still i m getting some back force or a kind of jerk when i collide with coin.
How can i remove that jerk or force on player collision with any coin box collider.?
I did a lot of research on Google & some forums, but can't find related to this issue.
Any Code will be appreciated.
Thankx
I resolved this issue with a trick.
I added an empty child GameObject with Box collider & Rigidbody in my Player GameObject & increase the collider area that cover my player collider.
This will make me to react before i collide with player collider. And i m handling Coin collision & other collision with empty GameObject collider.
I think this solution might probably help other people on Here. Gud Luck.
If you Don't use Trigger, I suggest check on trigger on Box collider in your Player Object. then OnTriggerEnter function will called when collide with coin.