0
votes

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?

2

2 Answers

1
votes

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.

0
votes

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