How to move an object without going through colliders - unity3d

I'm using Unity and i am doing a pong game. I would like to be able to move the paddles with the mouse key. I have tried just moving their position but that of course will simply "teleport" them through the colliders along the edge of the playing field. I tried using addForce() and making the rigidbody fixed in the x position, however, what happens is when the ball hits the paddle, it pushes it and the paddle snaps back. All of the ball's energy is lost (there is gravity in my game). How can i move this box collider but not let it over lap other box colliders while moving? Thanks!!!

Your paddle should be a kinematic (IsKinematic parameter) rigidbody (attached RigidBody2D) collider while the edges should just be a static collider. But, you should control the limits/edges of paddle movement within your script.
If you do things this way, your ball will naturally bounce of the edges and off your paddle. If, however, you want the ball to pass through the edges but notify you of doing so (e.g. lose condition), you should make the edges a static trigger collider (IsTrigger parameter).
Here's a detailed list with all interactions between different types of colliders: http://docs.unity3d.com/Manual/CollidersOverview.html. The messages generated are passed via 2 different functions: OnTriggerEnter2D and OnCollisionEnter2D.

Related

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 Making a prefab collide with surroundings, but not other objects of the same prefab

The picture below is my simulation, and the problem im facing is that they wont collide they way i want to.
I made them move randomly and i want this behavior.
The green balls are supposed to "bounce" off the outer grey walls, so collide with them (simulate physics)
The green balls are NOT supposed to bounce of each other, but only do triggerevents (so i know when they are on top of each other)
How do i do this. I have looked at multiple tutorials and i simply dont know what to do. As far as i understand, to make a collision, one of the objects has to have a rigidbody on, and the other a normal collider?
I have to tried to follow this overview. The balls are from the same prefab, so to get a trigger for them i have to pick either
static trigger collider, rigidbody collider trigger, or kinematic rigidbody trigger collider (as seem from the overview).
BUT if i pick any of those i cant get a collision with the walls? Do i have to do the wall collisions myself?
What you can do is make all the walls static colliders, and make a script on all the balls that check whether they hit a wall or a ball. and do actions that way.
Or
Make the walls check for a collision with the ball and add a force to the negative direction that they came from or something.(up to you how you want the balls to behave)
for example :
OnTriggerEnter(collision other)
if(other.transform.tag == Ball)
//Run Some Code here
// for example
BallRb other.GetComponent<RigidBody>();
BalRb.addForce //add the force that you want.

Unity Camera Bounces with Sphere Animation when trying to apply gravity with Rigidbody

I have an empty Object that has, among other things, a third and first person camera, and a sphere with a bouncing animation attached to it. I'm try to get it so that the sphere experiences gravity, but I'm having a lot of problems.
What I want is gravity on my sphere and the camera to not bounce with the ball.
Here a couple scenarios I've tried, and their outcomes:
1) When I attach a rigid body to the the Sphere (child of the empty Object) with apply root motion true or false, my character experiences no gravity. The ball bounces, but the camera follows the ball without bouncing with the ball. It also follows if i press the space button (to jump) The whole problem is i need gravity to fall back down when i jump. Right now i just go up.
2) When I attach a rigid body to the empty object (the parent), I gain gravity, but now my camera bounces with the ball, and if the ball tumbles and rotates, the camera rotates with it.
Is there a way to get the best of both worlds?
i.e: Ball experiences gravity, but the camera does not. The camera simply follows around the ball from first or third person view without bouncing.
Thanks so much
For me, I would like to create a Vector3D variable to record the offset between the ball and the camera. And you simply add the offset to your camera each time so your camera can chase the ball without bouncing with it.
For example:
Vector3D offset = cameraPos - ballPos;
//...
CameraPos = ballPos + offset;
If you don't want the camera moving with ball's gravity, you can block the change of Y axis.

Capsule Collider doesn't move smoothly

I have some issues using CapsuleCollider and RigidBodyController in a Player GameObject in Unity.
Using a CapsuleCollider as collider for the player, I noticed that the player doesn't move like a capsule or a sphere (in other words, I want that the movement should be like a rolling ball, with no stuttering), but its movements like more like a box pushed, with stops and starts, that make some little oscillations of the camera.
Any ideas, solutions or tips?
realy that is all based on how your moving the object, and the rigidbody2D settings of the object, I think using physics forces would roll your character like a ball or capsule, however just setting a velocity wont.

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.