How to prevent character from going through walls. Unity - c#

I'm new to unity here and I have a capsule for a player and a moving wall. When ever the player touches the wall, it can just go through and walk out the other side, and I kind of don't want that. The screenshot below demonstrates what I mean.
Player in moving wall
I tried to use rigid body and collisions to help give me a solution, but that didn't stop the problem. I'm now not really sure what the best thing is to do.

Rigidbody should be able to do the trick. You said that it did not work, so here are a few things you need to make sure you have done for the Rigidbody collisions to work properly:
Give the player a Rigidbody component
Give the player a collider (best use capsule collider)
Give the wall a collider (it looks like a box collider would work best)
When you move the player, move them with the function Rigidbody.MovePosition(newPos) or by setting the velocity with Rigidbody.velocity = new Vector3(newVelocity)
The "Is Kinematic" checkbox of the player's Rigidbody component is un-checked (set to false)

Related

Player is moving through the Wall in Unity 3D

I am making a simple game where the Player moves only Horizontally and the Enemies spawn from front. I created a Wall at the Left and Right side of the Player. But, Player keeps going through the Wall even when I have added Rigidbody component to Walls and Player. I used Cube gameobject to make a wall. Please help. Thank you!
Do you have collider on walls and on player?
If walls and player have colliders, check are theese colliders turned on and not triggers. (They should be turned on and checkbox "Is trigger" should be false)
If previous steps completed and it still doesn't work, check Edit -> Project Settings -> Physics -> Layer collision matrix. And make sure that walls' layer collides with player's layer
I have NavMesh agent on my player and what helped me:
Mark walls (and other static objects) as static in inspector
Go to Window->AI->Navigation and bake the scene
Add Box Collider to the object to make it collide with the walls.

Use IsTouching() in a trigger collider

I have this code for destroying an enemy when player touches it:
It works when the collider has isTrigger set to false. I've read the documentation and I think it's because it uses the physics engine to check whether player is touching or not.
Is there an easy way to avoid this, I like the cleanliness of isTouching and I need it to have a trigger collider so it doesn't affect the player movement.
If there isn't, is there a way to use the collider I already have to check for the collision with player?
I did it with OnTriggerEnter2D as jmalenfant suggested.

Colliders not working in Unity3D 2D Project

I have 2 colliders, its a game where I have a ball, and a Labyritnh. The ball should pass through the labyrinth and collides with the labyrinth's walls.
But I have a problem, the physic aren't working that well, the collider don't work.
I have a rigidbody and a collider in both of these objects.
Also, I have a problem with choosing box or edge colliders.
Because it is a labyrinth, I need to design the colliders with some complex shapes.
I have used edges colliders and box colliders but the ball is passing through all these colliders like nothing, It seems like its not working.
Can someone help me?
you need a rigid body object attached to at least one of them for the colloders to work.
make each wall individually and use rectangel colliders. add a rigidbody component, and set that rigid body to kinematic. should work then.

Collision between Box Collider 2D and Polygon Collider 2D not working

Currently I am working on a networked 2d platform game.
My player is an empty object which serves as a parent to my actual graphics etc. The empty object has a rigidbody (3d) attached to it as it needs to have one in order to use the configurable joint component ( note: I move my player around using this 3d rigid body).The player body however, a child to the graphics, has a box collider 2d attached to it as it after all it is a 2d game and has to collide with other 2d objects.
My platforms have polygon collider 2ds attached to them (which is connected to a platform effector if that is worth mentioning).
Now, even though both my player body (which is a child to graphics which is a child to my player object) and my platforms have 2d colliders on them they do not collide and can simply go inside each other (neither of them is marked as isTrigger).
In order to solve this I thought I would add a rigidbody 2D to the player body and see if that would do anything. Now upon adding the rigidbody 2D collisions did work but as soon as I made the rigidbody 2D have all position and rotation constraints ticked or as soon as I made it kinematic or static it would cease to collide with my platforms. The problem is I need to have the rigidbody 2D be static or not be able to move as I am currently moving my player object using the rigidbody (3d) attached to it and do not want any additional movement of the player body upon colliding with objects.
I know this is quite a lot of information, so if you have any questions or would like further information just comment and I will be quick to respond. Thank you :-)!
Edit:
2d ray casts are also unable to hit the player body box collider 2D
Edit 2:
So to recap:
If either the player body or the platform has a rigidbody 2d that is not static (dynamic) and can move collisions work. However currently I only have a rigidbody 2D on my player body which has to be static though (as explained earlier) as well as a box collider 2d. My platform(s) on the other hand currently only has a polygon collider 2d as I do not see why it would need a rigidbody 2D.
A static collider won't detect the collision with another collider if it doesn't have a rigidbody, if it is static or if it has a kinematic rigidbody. At least one of your two objects needs to not be static and to have a rigidbody which is not kinematic to be correctly detected. So you do need to add a Rigidbody2D to your platform.
When you have a doubt about why a collider wouldn't collide with another one, always refer to this page of Unity's documentation which sums up which kind of collider will collide with another one.
Rigidbody2D can't collide with Rigidbody3D, here's a workaround though
http://answers.unity3d.com/questions/580769/can-rigidbody-2d-collide-with-3d-colliders.html

Making a gameObject push the player

I am working on a Unity 3D game for Oculus and I have problems with making my objects to apply physics on a player. So getting rid of a CharacterController and using something like a rag doll is not an option.
I am using OVRPlayerController, that has a Rigidbody with mass 1 and a box collider on it. My gameObject has a Rigidbody of mass 100, and a box collider. But when the object hits the player it just goes through it, whereas I want it to push the player in x direction.
I tried using onColliderHit but it doesn't even recognize the collision between the player and the object, so I checked box collider on the object to be a trigger and I use OnTriggerEnter() to recognize the collision.
I tried to translate the player's position on collision, but player gets positioned to weird places out of my map for some reason. Here is what I use:
info.transform.Translate(new Vector3( -0.5f, 0.0f, 0.0f));
info.transform.rotation = Quaternion.identity;
I also tried to manually set the x position of the player but this doesn't work, and I know I am not supposed to do it.
I searched for answers for a long long time, so please don't answer to this with something like "oh, have you tried googling it, there are a lot of similar questions" etc.
It should be detecting the collision just fine, as long as one of the box colliders has a non-kinematic (Is Kinematic = false) rigidbody attached.
Make sure Is Trigger is false, and you specified a material for your collider.
Also try messing with the other properties on the rigidbody, such as Collision Detection and Mass. The unity docs indicate that your mass should be no more or less than 100 times that of other rigidbodies.

Categories

Resources