What's the difference between Physics2D.OverlapPoint and Physics.Raycast? - unity3d

I have been looking at options to detect a clicked GameObject. For a 2D game, what is the difference between Physics2D.OverlapPoint and Physics.Raycast? Is there any advantage of one over the other, performance maybe? I get the same return value for both and both seem to have the same problems with overlapping sprites. I've been leaning towards using Physics.Raycast since I may want to move to a 3D to a top-down perspective in the future, any other considerations?

A picture is worth 1000 words:
Here is the raycasting:
Here is OverlapPoint, which checks if a collider overlaps a point in space.

OverlapPoint checks if a collider overlaps a given point in space while Raycast shoots a ray along a specific vector and returns anything it hits.
Very different concepts and not to be used interchangeably... even in 2D. You can fire a Raycast starting beyond your 2D scene and fire back toward the viewer (defined by the direction vector) or in front of the scene and fire away from the viewer. Different results will be returned as they ray will hit different colliders depending on z-order.

OverlapPoint is basically a Raycast except with no depth to it. You are right about using the Raycast if you plan on going 3D in the future. Also the returns are different, where Raycast returns you a bool and OverlapPoint returns you a Collider2D

Related

Unity: 2D character floating using BoxCollider2D

I'm pretty new to unity and I set up a simple scene with my character and the ground. I added a Rigidbody2D and a BoxCollider2D to the character and another BoxCollider2D to the ground. The boundaries of the Colliders fit the boundaries of the character / ground exactly, but my character is not falling on to the ground, when I hit play, but rather stops and floats a few pixels above it.
I've seen a lot of similar questions but the solutions didn't work for me (or maybe I didn't understand them right, as I mentioned above I'm new to Unity and thats my first game).
That's what the floating looks like:
I could obviously "fix" this by lowering the upper boundary below the actual boundary of the ground, like this:
But that's obviously not a good solution and I'd still like to know what is causing this bug and how to fix it.
Maybe gap has size of Default Contact Offset:
Set a proximity distance value for colliders to be considered in contact, even they are not actually in contact. Colliders whose distance is less than the sum of their contactOffset values generate contacts. This allows the collision detection system to predictively enforce the contact constraint even when the objects are slightly separated.
Caution: Reducing this value too far could cripple Unity’s ability to calculate continuous polygon collisions. Conversely, increasing the value too much could create artifacts for vertex collision.
You can find it in Edit > Project Settings > Physics2d .

Unity 3D object with Rigidbody sliding

I have a cube with Rigidbody attached to it would slide slightly whenever I pressed the play button. If I leave it for awhile, it would slide to other side of the screen.
Anyone know how to solve this problem without using the "freeze position"? I don't know what I messed up in my project...
This is what the object looks like:
Note: I need to use the gravity.
Thank you!
Heh! The solution here is:
You had a rigidbody on the floor :)
You don't do that :) Never.
If the "floor" surface is flat, then,
it will not slide.
You've got something strange going on, such as
"floor" is NOT flat
a feature like "Wind" turned on
perhaps other objects invisible in the scene you have forgotten about are nudging it
PhysX does not have a "mind of it's own". There is some simple reason it is moving.
Let's say the "floor" is indeed on an angle, so it SHOULD move, but you WANT it to NOT move.
What you obviously do:
Just as in the real world, put something there to stop it moving.
A small invisible wall will do the trick. That's PhysX!
Usually rigidbody sliding happens when a lower rigidbody has lower mass than a higher rigidbody (forcing down the lower rigidbody). Typical problem with player having say mass 80 jumping on a cube with mass 1. In this case the collision is so violent that the cube will probably fly out (not only slide).
The situation is very similar to the real world. Try to stay on a box of milk if you have 120 kg (ok, ok, 80 :) ).
When you try to eliminate this behavior, you need either increase the mass of the lower object or decrease the mass of the higher one or set the lower rigidbody to kinematic.
The solutions above is not proper way of solving the problem. Unity has more features of physics than mass. If you get sliding on movement or because of other objects you should add proper drag value on your rigidbody. For example , lets say you have blocks spawning over top of other blocks and this creates horizontal sliding. In my case I add drag of 1 to the objects which has mass of ~ 1 kg. It depends on the scene and you should try different values on your case. Do not use bigger values and angular drag if it is not important.

Get all hit objects between two moving points?

I want to set transparent all trees which are between player and camera, my game is top down, and vector between camera and player changes. So, how to Raycast between two points and also get all objects that are hit by ray? I know there is Linecast for raycast between two points, but it returns only first object and RaycastAll on the other hand can be casted only in specific direction... Any idea how to cast ray between player and camera and get all hit objects?
Although Physics.RaycastAll() doesn't appear to immediately meet your needs, you can easily adapt it to give you what you want.
If you perform a raycast from the player in the direction of the camera, and limit it to only the distance between the player and the camera, then you effectively only cast a ray between the two positions and will only get object between them.
Here's how I suggest you approach it:
float distToCamera = Vector3.Distance(camera.transform.position, player.transform.position);
Vector3 dirToCamera = camera.transform.position - player.transform.position;
RaycastHit[] hits;
hits = Physics.RaycastAll(player.transform.position, dirToCamera, distToCamera);
Hope this helps! Let me know if you have any questions.
A quick search and look at this and use it on your trees and when they became visible to the camera and after that do what ever you want with objects
note : this event can be fire with any camera rendering those objects so beware of which camera you are using to render trees is right

Unity 2D Bounce Back moving object when colliding with another object

I have an object that, after receiving its respective input, it moves this way:
mov = new Vector3((Input.GetAxis("Horizontal") * vel), 0, 0);
transform.position += mov;
But, I want it to bounce back, once it collides with an object.
I´ve made the procedures already (OnCollisionEnter2D(Collsion2D col){bla bla...}), but I need help with what happens on the collision (bouncing back the object)....
I´ve tried giving the collided object a bouncing material, but it just slows it a bit, my guess is that because of the constant force given by the acceleration.
Greetings.
If you move the object with transform.position what you are doing is basically a "teleport" so it will ignore the bouncing material. If you want it to bounce you have to write the physics code to detect a collision and change the movement or you can do addforce to move the object and it will detect collisions and react automatically.
you are teleporting the object at the current time. instead you should use the Rigidbody.addForce this will add a force in the specified direction thus if you do the opposite direction will "bounce" of the object. Another option would be to create a physics material then not bother with the code.
You are not using materials, right?
See if the content of this post may help you, the OP is using a formula using Raycast and the answer guides him to use the Raycast with Layers Maks:
2D bouncing formula doesn't work properly
There is this one also with fixed angles (like Pong), but it uses material (with values: friction: 0, bounciness: 1):
https://gamedev.stackexchange.com/questions/70294/get-gameobject-to-bounce-of-colliders
But if nothing makes sense and you are going crazy and might want to start from zero, there is this official video tutorial on bouncing and sliding in 2D:
https://unity3d.com/learn/tutorials/modules/beginner/2d/sliding-bouncing-2d

Unity: Ball falls through object that is rotating around another object

Imagine you have a ball falling due to gravity. When it encounters a rotating "cube" object, then you would expect it to bounce off of the object. However, in my case if the cube is rotating fast, the ball goes through it, but if the cube is rotating slowly, the ball hits it and bounces away as expected.
I am using RotateAround() inside the Update() method to achieve the "cube" object's rotation. I tried setting the ball's collision detection to Discrete, Continuous, and Continuous Dynamic with no luck.
My goal is to make the ball bounce away no matter how fast the "cube" object is rotating around another object.
Is there something else I should look into?
Thanks!
You can try lowering the Fixed Timestep value under Edit > Project Settings > Time.
Be aware that this will affect the performance of the game as you're calculating physics more often.
Documentation: http://docs.unity3d.com/Manual/class-TimeManager.html
Also, I assume that you have box and sphere colliders as opposed to mesh colliders? The former are more efficient at detecting collisions.