Get all hit objects between two moving points? - unity3d

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

Related

Unity 2D physics is not colliding when player moves fast

A 2D circle shaped player has the following code
Vector2 target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = target;
which sets the player's position to the mouse position, and when it collides with enemies they get destroyed (simple concept).
The problem is that the enemies do not recognize collision when player passes through them quickly so they are not destroyed. This problem frustrated me I don't know why collision doesn't work when an object passes through another object quickly .
Is there a solution? Or it is just UnityEngine Maximum Performance
Thanks in advance.
I recommend you to see this link.
It explains much about Collision in unity.It looks similar to your game also.
You could also calculate distance between two objects and when the
distance is equal to zero you may destroy that object too.
Note1: this destroys the object only when it is coinciding with another object.
Or by calculating the outer distance between two objects when it is
equal to zero you destroy that object.
Note2: this destroys the object when two objects are starting to collide.

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

Collision Detection of objects in certain area of camera in Unity3d

I am creating a 3rd person action game where the player is a helicopter and he can shoot other objects while moving. The problem is I am trying to find the enemy objects who are inside a circle in the center of the camera and I need to track them and shoot them.
Raycast wouldn't help as i need a thicker raycast and so I tried spherecast and capsulecast.
I have a GUI element which gives the player idea on where he can shoot.When using Spherecast or Capsulecast, It is working when the enemy is near but when the enemy is far behind I guess the spherecast becomes small while traveling along z and doesn't hit the object most times.
if (Physics.SphereCast (startPoint, 1f, transform.forward, out hit)) {
if (hit.collider.CompareTag ("Shootable") ){
Debug.Log(hit.collider.name);
Destroy(hit.collider.gameObject);
}
}
I have seen raycast from camera and so i was wondering if there is something to do like circlecast from the camera which would be appropriate for this. If not how can I proceed?
Any help is really appreciated.
If you want to detect whether enemies lie within a conical area in front of your camera, using a SphereCast or RayCast will not be able to meet your needs.
Instead, you might consider checking the angle between an enemy's relative position and your camera's forward vector, to see if it is below a particular value, and hence within the cone.
For a 60-degree field of view, and assuming you store your enemy Transform components in an array/List, your code might look like:
foreach (Transform enemy in enemies){
if (Vector3.Angle(transform.forward, enemy.position - transform.position) < 30){
Destroy(enemy.gameObject);
}
}
Hope this helps! Let me know if you have any questions. (Answer adapted from this Unity question.)

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

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

Check if camera if facing a specific direction

I'm working to let a character push objects around. The problem is that as soon as he touches an object he starts moving it, even when the touch was accidental and the character isn't facing the direction of the object.
What I want to do is get the direction the object when a collision happens, and if the camara is really facing that direction allow the player to move it.
Right now I only managed to get the direction of the object, but I don't know how to compare that with the direction of the camera.
This is what I'm trying now:
void OnCollisionEnter(Collision col) {
float maxOffset = 1f;
if (col.gameObject.name == "Sol") {
// Calculate object direction
Vector3 direction = (col.transform.position - transform.position).normalized;
// Check the offset with the camera rotation (this doesn't work)
Vector3 offset = direccion - Camera.main.transform.rotation.eulerAngles.normalized;
if(offset.x + offset.y + offset.z < maxOffset) {
// Move the object
}
}
You can try to achieve this in several different ways. And it is a bit dependent on how precise you mean facing the box.
You can get events when an object is visible within a certain camera, and when it enters or leaves using the following functions. With these commands from the moment the box gets rendered with that camera (so even if just a edge is visible) your collision will trigger.
OnWillRenderObject, Renderer.isVisible Renderer.OnBecameVisible, OnBecameInvisible
Or you could attempt to calculate whether and objects bounding box falls within the camera's view frustum, there for you could use the following geometry commands
GeometryUtility.CalculateFrustumPlanes, GeometryUtility.TestPlanesAABB
Or if you rather have a very precise facing you could also go for a Physics.Raycast So then you will only trigger the event when the ray is hitting the object.
Hope this helps ya out.
Take a compass obj and align it to ur device sorry object than when you move it you can always know where it points to.
theoretically it should work but maybe your object just moves around because of a bug in your simulator motion engine.