Unity : How to handle compound colliders 2D physics - unity3d

I´m making a small game where the basic game mechanic is dragging things around the level . My Player G.O. is the one who drags the other objects around the level . This G.O. (the player GO) has a rigidBody2D component , and in order to drag other objects , I turn the dragged object into a child of the player , so that I only move the player through the rigidBody component and the dragged object moves along the player object .
The problem is that whenever something that should kill the player by contact makes contact with the object that is being dragged , the death of the player still triggers even though the player has not make contact with the hazardous object to begin with . This is obviously not the intended result , if something that is meant to kill the player makes contact with the dragged object but not the player , the player should not be killed . How can I fix this?
EDIT : By killing I mean destroying the player GO.

If you delete your parent GameObject, all its child GameObjects will delete with it too. In order to not get your dragged objects deleted, you should remove them somewhere else before deleting the parent GameObject. You could do that by:
YourChildGameObject.transform.SetParent(OtherParentGameObject, false);
Also, this link could help you.

This is a problem with collisions.
Set different layers to player and enemies.
Go to Edit -> Project Settings -> Physics. On the bottom, you'll see a list of collisions. You can check which layers collide with which layers there. Set the "enemy" layer to collide only with the "player" layer.
Another way to do this is by checking what is the enemy colliding with OnCollisionEnter or OnTriggerEnter (depending on how you're doing it). You can do it like this:
//You can change this to OnTriggerEnter and "Collision" with "Collider"
void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "player")
{
//Kill the player
}
}
Good luck!

Related

how to detect a player in unity when a spotlight hits the player

I'm making a game in unity where I added a spotlight that is constantly rotating at the x-axis and I want the light to detect my player when the light hits the player
i tried raycasting but its just a single line so there is no accuracy
You can have a cone mesh
-> use it as mesh in MeshCollider trigger without any mesh renderer
-> now you basically have an invisible collider object which is a trigger so other objects can still just pass it without actually colliding
-> however, now you can simply check for collider overlapping using e.g.
private void OnTriggerEnter(Collider other)
{
if(other.TryGetComponent<Player>(out var player)
{
Debug.Log("Hey {player}, I see you!");
}
}
This would be your pre-filter for checking if any further work is required at all - if there is no player at all within this trigger then there is nothing further to check anyway.
Then further checks could e.g. include also checking if there are multiple hit objects, if only the player is in there also nothing else to do as you know you already see the player. If a wall object is closer than the player only then you can do further raycasting checks if maybe you see any edge of the player etc
For example for staters making a raycast on each edge of the player and check if one of those hits the player and not the wall

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 :)

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

Unity OnTriggerEnter2D sometimes doesn't work

I have problem with triggers in my 2D game in Unity. I want to make enemy die when he triggers with player's weapon. The problem is there are two colliders attached to enemy (tagged "Enemy"):
one is box2d collider which is used as normal collider
second is sphere collider which is set as trigger and is used in script to check whether there is a player in range
I got sword object, which has sprite renderer, box collider (set as trigger) and script:
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Enemy")
{
if(!other.isTrigger)
{
Debug.Log ("enemy");
Destroy (other.gameObject);
}
}
}
Screenshot of scene:
http://i.stack.imgur.com/eVtRX.jpg
Screenshot of Enemy gameObject:
http://i.stack.imgur.com/9R5a6.jpg
So in general it sometimes works, but sometimes doesn't. When I disable sphere collider at enemy, everything works great, but I need to have it to check if there is player in range. How can I fix it?
you should Make sure two things in OnEnterCollider2D
1) Make sure both the gameobjects participating in OnEnterCollider2D must not be destroyed. If one has to be destroyed then it should be destroyed with some time later.
2) Make sure one of the game object participating in collisions has to have rigidbody attached with isKinematic unchecked.

Collision of object with player

So I have a bit of an issue with some unity scrip. I have a moving object, a ceiling to be specific, and I have it lower and 'crush' the player. However, it's not working. The ceiling moves, but nothing happens when it collides with the player. It used to work if the player was moving when it hit them but it isn't any more and I haven't changed any of the script so I don't know why that's happening.
Interesting suggestion from your teacher. I would put a character controller on your player, which you seem to have already. Then I would add the collision code:
void OnCollisionEnter(Collision c)
{
// die.
}
Then add a box Collider and Rigibody to the roof. I would also add this code above to the roof.