Trouble With Colliders - unity3d

I am currently making a 2D game and I am trying to make my character die whenever his head touches an object, however, he will be walking on the same kind of objects. How do I differentiate when his feet touch something as compared to when his head touches something? Would I use two separate box colliders? If so, how would I code that?

Instead of using colliders to detect when the player's head touches the ceiling, I would use one collider for the whole body. Then, use Physics.Checkbox to determine whether anything is colliding with the player's head. For the calculation, you can specify a layer to omit, and naturally one would omit the player's collider.
So, your code might look something like this:
if(Physics.Checkbox(center, extents, rotation, playerLayerNumber)) {
//Handle player death here...
}
The only problem being, of course, that your player will die when anything touches their head, if you've got other objects in your scene. Hope I could help!

Related

How to deal with nested rigidbody?

I can't seem to wrap my head around the following, I have a simple spaceship with 2 turrets:
My 'Player' the white ship has a rigidbody. The turrets have not.
I'm moving and rotating my player via the rigidBody, however I would like to also rotate the turrets via the physics system.
I thought about adding a rigidBody2D to the turrets, however that was not recommended from my understanding reading posts online. This is because rigidbodies in children would affect the parent in unforseen ways.
So dropping that idea, How can I move my turret and having it be affected by physics?
Some sidenotes:
I want the turrets to be influenced by physics. Lets say i try to rotate my turret, but it hits a wall, I want it to resist. Lets say some debris hit my turret's barrel, I want it to move in the opposite direction.
Although i could move the turret outside of its parent's container (the ship) I do ofcourse want it to 'stay on the ship in game'. The turret is part of the ship, its mounted to the hull or fuselage if you will. So it should move with the ship and keep its position.
As mentioned by derHugo joints where up for the task, although i had to restructure a bit, here is the outcome:
I added an empty transform to hold the ship and its sub-components, so turrets are no longer part of the ship parent, so that i can add a rigidBody to these child objects now.
Then I added a hingejoint and rigidbody to the player ét voila, it now works:
I still need to take a good look at how joints work exactly but this seems to work.

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

Collision Detection Between Two Objects in a Subview

I got the following question. I have two objects on my Subview. One of them is a player who is able to move on tiles. The other one is a wall. The player should not be able to move through or on the wall. Right now the wall is seen as nothing or a background (not a obstacle).
One idea I have is saving all the coordinates of the walls and checking if the coordinates of the player match the coordinates of a wall and undo the movement. In my eyes this idea is pretty inefficient and could probably done better.
Are there any other solutions?
If this is a game, use Sprite Kit, which gives you collision detection and automatic "bounce" behavior. If it is momentary animation, use UIKit Dynamics, which gives you collision detection and automatic "bounce" behavior. Otherwise, you just have to implement collision detection yourself (by looking to see whether the frame of one view intersects the frame of another) and perform the "bounce" yourself.

unity3d player gameobject "hear" sounds

I have a question that is a little... complex
basically I want a gameObject (an enemy) "listen" to the sound of the player (footsteps, door opening, gunfire etc)
I could do this just fine by using:
>Collider[] hitColliders;
>hitColliders = Physics.OverlapSphere(transform.position, 10f,enemyLayer);
>
>for(int i=0;i<hitcolliders.Length;i++){}// Call Enemy Hear Player Function
However, the problem starts here:
I want the "sound" to be blocked by walls. At first, I thought I could use Physics.Linecast to do this. However, this means the sound could be blocked by a mere piece of pole, because Physics.Linecast only shoots a single, straight line.
What I want is player C could not hear anyone at all, but player A and B could still hear each other.
I hope you guys understand my question.
You could place the objects you want to block the soundwave on a specific layer. And just make the raycast ignore all other layers.
Leave all this Physic and other graphical stuff behind.
Create a separate logic for this behavior.
Create a big chess board (where x,y will be x,y position in you game) and add only sound-generators, sound-listeners and sound-obstacles.
Update this chess board each frame and create sounds, fly sounds, block sounds and listen to sounds and act appropriate as the real life actors.
This way you can add parameters to each obstacle, sound-generator and sound-listener so that all be configurable.

Detect collision with enemies head with cocos2d

I want to detect a jump on an enemies head (like in super mario). How can I do this?
If you didn't understand what I mean just say something :)
I dont need to use cocos2d but it would be better with that :)
I'm not familiar with cocos2d so I can't help with specifics but:
If you can determine the player collided with an NPC, just check if the bottom of the player is what collided with the top of the NPC. If it did, he must have landed on his head.
To be extra sure, you could also check if the player's velocity states that he's moving down but that's probably not necessary.