Find all objects between player and camera

2019-09-12 03:15发布

I am using an orthographic projection for camera to follow a player. I would like to find all gameobjects between the player and the camera so I can change the opacity so they are partially transparent while blocking the camera's view. I read about raycasting, but it seems it would give only the first object between the player and camera. What approaches are there to accomplish this?

标签: unity3d
1条回答
小情绪 Triste *
2楼-- · 2019-09-12 03:52

Just use Physics.RaycastAll like this:

public class CameraScript : MonoBehaviour
{
    //Attach this script to the camera//

    public GameObject player;

    void Update()
    {
        float dist = Vector3.Distance(transform.Position, player.transform.position);
        RaycastHit[] hits = hits = Physics.RaycastAll(transform.position, 
                                                      transform.forward, 100.0F);
        foreach (RaycastHit h in hits)
        {
            //Change the opacity of the of each object to semitransparent.
        }
    }
}
查看更多
登录 后发表回答