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

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.

Related

How to have collision detection without physics in Unity?

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

In Unity 2D, how do I achieve Non-trigger collsion that doesn't "physically" move the rigidbodies?

I have a bullet game object and an enemy game object.
I want to have the ability of getting the collision point/contact, which's why I think I need the collider of the bullet to not be a trigger collider - this way, I want to have the bullet bounce off the enemy. At times, the bullet will need to bounce off of rectangular objects (not a problem), but I also want the bullet to bounce off of elliptic objects (the problem).
I want the bullet not to "physically" push the enemy, which is why, intuitively, I should set the bullet's collider to be a trigger collider.
When the bullet's collider is a trigger collider, I seemingly don't have a way to implement the bouncing.
But when the collider's not a trigger collider, it pushes the enemy object.
There's no code errors, but I think the code hasn't got anything to do with the problem.
I suspect the problem may have something to do with the Physics Collision Matrix.
EDIT
It seems that raycasting would be the solution to the problem, as Leoverload proposed. My problem became a different problem now, so I'll close off this thread, and open a new one.
Thanks for the help, #Leoverload ! :D
For the position of the hit it's very easy. You must have 2 colliders and 2 Rigidbody and then you can simply add a
Void OnTriggerEnter2D(Collision Coll) and inside it check for the tag : if(coll.compareTag("surface")) and inside you can get the position with collision.transform.position.
The collision matrix on the Physics menu only allows to remove Collision between certain layers, it's useful if you want the enemies not to push eachother!
If you don't want the player pushed you can change the collider to trigger, and destroy the bullet just after the collision with the same method as before with a void OnTriggerEnter2D and the compareTag. In this way it will look like it touches the enemy and explode, but it won't actually touch anything. If you want to add knockback to the player then you could do a simple AddForce based on the direction of the bullet and the work is done!
If you want to avoid the bullet to move walls you can set walls to static and it should work fine

How to move an object without going through colliders

I'm using Unity and i am doing a pong game. I would like to be able to move the paddles with the mouse key. I have tried just moving their position but that of course will simply "teleport" them through the colliders along the edge of the playing field. I tried using addForce() and making the rigidbody fixed in the x position, however, what happens is when the ball hits the paddle, it pushes it and the paddle snaps back. All of the ball's energy is lost (there is gravity in my game). How can i move this box collider but not let it over lap other box colliders while moving? Thanks!!!
Your paddle should be a kinematic (IsKinematic parameter) rigidbody (attached RigidBody2D) collider while the edges should just be a static collider. But, you should control the limits/edges of paddle movement within your script.
If you do things this way, your ball will naturally bounce of the edges and off your paddle. If, however, you want the ball to pass through the edges but notify you of doing so (e.g. lose condition), you should make the edges a static trigger collider (IsTrigger parameter).
Here's a detailed list with all interactions between different types of colliders: http://docs.unity3d.com/Manual/CollidersOverview.html. The messages generated are passed via 2 different functions: OnTriggerEnter2D and OnCollisionEnter2D.

Unity 2D: How would I go about making a block that shoots when the player touches it?

I am making a 2D platformer in Unity and I want to make a block that works very similarly to the shooting blocks from Kid Chameleon. When they are touched by the player, they shoot from what every direction the mark is on and the bullet can hurt the player and enemies, destroy crates and activate other shooting blocks. Can some one tell me how I can achieve this because I have no idea where to start?
For start you can use OnMouseDown() event for detecting a click(or touch) on an object and then you can Instantiate a bullet object from block and give it's rigidbody a velocity to move towards that. and you can use OnCollisionEnter2D(Collision2D c) event for every Gameobject that you want to detect collision and do your job.

Collision detection, without the physics?

I have enemies following a path in my game. Along the path I have turrets shooting at them. To get collision detection between the bullet of the turret and enemy i've added RigidBody2D, and Circle Collider 2D to the enemy, and Circle Collider 2D to the bullet.
When the bullet hits the enemy it shall not push the object, just destroy the bullet and take some of the health.
I feel Rigidbody2D is messing it up, it gives mass, drag and gravity to my enemy... but I dont need it. But it seems I do if I want to have collision detection bewteen two objects?
You need Rigidbody to detect collisions, however you are just triggering some actions (taking damage). That is why you can check isTrigger on your colliders, and then implement 'OnTriggerEnter2D' callback instead of 'OnCollisionEnter2D', which won't activate any physics.
See http://docs.unity3d.com/Documentation/ScriptReference/Collider2D.OnTriggerEnter2D.html for reference.