Unity: 2d GameObject Ricochet - unity3d

How can I make a 2D GameObject Ricochet off a wall or surface with the same angle it used to hit it? Can you just point me to the right direction or how I should do it?

Create your bouncing game object as a sprite. Add a 2D collider component and a rigidbody2D component. Add a physics material into the material property of the rigidbody and adjust settings to taste. Your object should move with physics and bounce off things when it collides with them.

Related

Unity Collider doesn't work for box and circle collider

I created two game object.
One is box and the other one is a ball. Then I added collider them( box collider and circle collider) to collision them. But it's not working. When I hit play button, the ball just falling to eternity ^^.
I searched on the internet and looked for possible solutions and they didn't work.
*Is trigger unchecked.
*Attached rigidbody2d to the ball.
Any help will be appreciated. Thanks for your interest
The BoxCollider and CircleCollider2D interact with physics in different dimensions, box collider physics are calculated in 3D while circle collider physics are calculated in 2D. In Unity, 3D physics and 2D physics are calculated separately so collisions between 2D and 3D colliders isn't possible. You will have to replace your box collider with a BoxCollider2D or replace your circle collider with a SphereCollider. Best of luck!

Unity - Can I add CharacterController component to 2D sprite in 2D game?

Up to this point I've been making 3D games in Unity and I've been using CharacterController.Move() for moving player most of the times. I'm currently making a 2D platformer which is the first time I'm doing a 2D project in Unity. I'm trying to figure out what would be the best way to move characters around. I think that it's better to write my own controller than to use rigidbody for movement. Rigidbodies may give nice smooth movement, but own controller gives me more flexibility and control over how moving characters work.
My question is: can I add CharacterController component to my 2D player and use its Move() method to move player? Or perhaps CharacterController should be used only for 3D games?
At the end the position of a gameobject is always given by the transform.position.
RigidBodies (RigidBody2D in your case) will handle the transform on physic update loop but at the end the transform position will be changed.
So if you don't want to use physic you can just set the target position to the transform position.
transform.position = newPosition;
If you want to use a rigidbody you can set the position like this
rigidBody2D.MovePosition(newPosition)

OnMouseDown() doesn't work when object's collider is colliding with another collider on the mouse position

I think the title speaks for itself. I'm using 2D Colliders, and they're on the same Z position. OnMouseDown() doesn't get triggered if you're clicking another object's collider as well. This is pretty annoying. Thoughts?
At least for me it gets triggered always on one of the objects, but I cannot choose which one.
As a workaround you can add a child gameObject with 3D collider (for example Box Collider). The child is needed because both 2D and 3D colliders cannot be attached to same gameObject. Make the 3D collider expand towards camera a little bit. In that case the child gameObjects OnMouseDown() is always called.

Unity 4.5 Mesh Collider Not Interacting with Circle or Box Collider 2D

I am new to unity and I am working on a 2D game. Currently, I am having trouble getting two colliders to interact when one of them is a mesh collider and the other is a box or circle collider. I was originally working to get the Unity Sample Assets 2D character to interact with a mesh terrain. When I "played" the game, the circle collider attached to the legs of the character was falling through the mesh terrain. I have simplified the problem and created two cubes:
One cube I upload and keep the 2d box collider and add a rigid body to
The second cube I delete the 2d box collider and add a mesh collider
I place the second cube under the first cube and hit "play". The top cube falls through the bottom box. When I replace the bottom cube's mesh collider with a box collider and hit "play" it correctly collides and stops on the box. I'm guessing I'm making the same mistake in this simplified example as I am in the more complicated 2D Character scenario. Do you have any suggestions of what I am doing wrong? I have tried making the mesh collider convex (although I believe this should only be necessary between two mesh colliders?). I have also ensured that the z position is the same as well as the layers of the two objects.
You cannot collide a 3D object against a 2D.
void OnCollisionEnter2D(Collision2D coll)
{
// Code here is clueless about 3D.
}
API Reference.
Sent when an incoming collider makes contact with this object's
collider (2D physics only).
You could cheat a little. Before Unity had 2D colliders what people would do is create a very thin box collider3D, which in your case should work.

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.