RigidBody and collision.contacts - unity3d

I'm trying to re-invent the wheel once again for a 2.5D platformer.
I tried some things with the CharacterController but it only send the "sides" info, not if it's coliding specifically left or right.
So now I'm working with RigidBody and the OnCollisionEnter() function
So thing is, all points returned by the collision.contacts seems to have some priorities. Here is what I have in game
the green box is the BoxColider
the red cubes are contact points from collision.contacts returned on collision from the RigidBody
As you can see, eveb if I touch a wall, all the contacts points are on the "ground" side of the Box.
If I jump :
I have point where I need them (here left side) because in air. But once I touch the ground I lose all left/right points from the returned array. All points go back to what we see in the first screenshot.
And so I have nothing to know when the Box is touched on the sides (and specifically left or right).
Any idea ? or better methods ?
Thanks !

I think you could try to do your stuff with
OnCollisionStay()
to get all the contacts points.
from http://unity3d.com/support/documentation/ScriptReference/CharacterController.html

Related

How do I make my square not flip over in unity?

I am learning to code and make games in unity, so I am very much a beginner. I made a little square jumping game in 2D that can jump, and move Left and Right. But for some reason the physics make it so that my square can flip over and it wont jump after it does. I have a GameObject that is named feetPos so that my square can jump, but as soon as the square flips over it wont jump again. is there any way to fix this? Or make my square not flip over?
(I don't know how to put link of the code)
Not exactly sure if this would help your specific issue. But to stop rotation of your square entirely once it jumps you could:
go to the Rigidbody2D Component for your square,
and then navigate to Constraints:
then selecting the 'Freeze Rotation' option for the z-axis of your square
you can find out more about RigidBody2D rotation on the unity manual docs for the different types of body types your Rigidbody2d component may use:

Unity Brick Breaker: Ball hitting in-between two bricks

I am making a 2D Brick Breaker game in Unity.
I have an issue with the scenario when ball hits in between two bricks. I have BoxCollider2D attached to all bricks and a CircleCollider2D attached to the ball. When the ball hits between 2 adjacent bricks, it bounces back in the same direction as if it hit the edge of the brick. There is no edge in between, but two adjacent bricks form a continuous surface. So, the ball should bounce off of the surface (in other direction) instead of bouncing back.
Anyone knows of any solution to tackle this problem? I asked this in the Unity Physics forums but didn't get any answer, so checking if anywhere here might have had this issue.
Thanks,
Mukul
I am guessing this could be the problem:
When your ball is hitting the bricks with a strong force, There is a chance it might move one of the bricks by a very very slight distance, even if the mass of the brick is much heavier.
This movement might cause an uneven surface, which is why the ball bounces back in the same direction.
Try adding a Rigidbody Component on every brick (if you have not done it already), and set its isKinematic property to true.
Let me know if this solves it.
Way 1:
Use one box collider for the wall, but not for every single brick.
This will fix your issue + will optimize your project.
Way 2:
You need to build wall programmaly and colliders in this case must be without spaces between them. This must fix the issue.
Way 3:
Make your own hitting wall logic.
OnColliderEnter you need to get balls velocity.
OnColliderEnd you need to set manually velocity.

Side scroller style scene with gravity

I've started learning sprite kit and I think I've got the basics but now I'm struggling with something.
I want to create a game that has a 'Streets of rage' type feel to it whereby the user can move up and down, but isn't jumping, they're still on a 2D plane. But I also want them to be effected by gravity e.g stairs etc. like the following picture.
Am I right in assuming that I should have my background image with colliders around the blue and brown edges, and then create a physicsbody collider located at the feet of my player/players so that it looks like they can move against the background, but when their feet reach the top it would stop?
Could I then place other obstacles like rocks etc on that path that they would be able to collide into, but that could also sit over the path and the sky? How would I handle the fact that these could be constantly colliding depending on the position?
I appreciate there isn't any code here, but I'm trying to understand the concept around this before I jump in coding a solution.
Thanks
I would use a categoryBitMask to separate the different planes of objects.
And I would play with collisionBitMask/contactTestBitMask depending from the plane the player is in, in addition to the z-order.
Thus you can have a rock that collides your player if they are both in the same line else the player would walk behind/front.

How to Topple a Cube over via Script

I just want to topple a cube over front side of it. But I can't figure out how can I do this. I'm making a game with dice. It won't be rolled, just rotate 90 degrees and move forward 1 unit. How can I do it? Is there an easy way with rigidbody or should I do it all manually changing position and rotation.
By the way, sorry for bad English.
Get an empty GameObject, lets call it `flipper', and send it to the bottom edge of the cube you want to be pivoted on.
Parent the flipper to the cube.
Rotate flipper by 90 degrees.
Unparent the flipper.
Repeat.

how to add frictionless effect to an object on a plane

I'm new to Unity and the is working on a personal project. In the following picture, you can see a blue plane in the middle, I want to use it as a ice plane and there should be no friction when user is walking on it. In another way, if I press 'w', the object should move forward until it hits an object. I know there's a built-in function called physic material, but it works only when the plane is tilt at some angle so that the object will slide down from the top to the bottom, but if the plane is placed in a horizontal level it will not work. Anybody has any suggestions for it, thanks.
Answering from my phone and off the top of my head, but look at using input.getaxisraw() to get your direction data then add forces. use triggers to stop the movement when you reach a trigger on another object. There were some good tutorials on Collisions and triggers on unity tutorials. To elaborate more, you can add colliders to the objects that you want your player object to interact with physically. So for your player object you can add code like:
OnTriggerEnter(collider c) {
// stop movement
}