0
\$\begingroup\$

This is what I have achieved(I think)I created a cube with meshfilter, boxcollider, rigidbody(in 2D platform) in unity. It creates a raycast towards each of the vertices in the scene(platforms, boxes and anything having box collider with it). But, I am having a problem as how to fill a color between any two raycast I created. My game scene:-Color between green rays

My script:-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Lights : MonoBehaviour
{
    public GameObject[] sceneObjects;
    public float offset = 0.01f;
    RaycastHit hit, hit2;
    // Start is called before the first frame update

    void Start()
    {
    }


    // Update is called once per frame
    void Update()
    {
        Vector3 myPos = this.transform.position;
        for (int i = 0 ; i< sceneObjects.Length; i++){
            Vector3[] mesh = sceneObjects[i].GetComponent <MeshFilter>().mesh.vertices;

            for (int j=0; j<mesh.Length; j++){
                    Vector3 vertiLoc = sceneObjects[i].transform.localToWorldMatrix.MultiplyPoint3x4 (mesh[j]);

                    Physics.Raycast (myPos, new Vector2 (myPos.x - vertiLoc.x - offset, myPos.y - vertiLoc.y - offset),out hit, 100);
                    Physics.Raycast (myPos, new Vector2 (myPos.x + vertiLoc.x + offset, myPos.y + vertiLoc.y + offset),out hit2, 100);
                    Debug.DrawLine (myPos, hit.point, Color.green);
                    Debug.DrawLine (myPos, hit2.point, Color.blue);
                    Debug.Log (vertiLoc);
            }

        }  

    }


}

This is what I need

\$\endgroup\$
7
  • \$\begingroup\$ We don't. Raycast is a sensing method, not a drawing method. You can use Debug DrawRay to visualize what the ray is doing in the editor, but this is not shown in the built game that you ship. It looks like you want to create a visibility polygon as explained here. For that we'd usually build the polygon as a Mesh made of triangles. How have you tried doing this with the Mesh class so far, and where have you run into trouble we can help you with? \$\endgroup\$ – DMGregory Apr 23 '20 at 11:43
  • \$\begingroup\$ The link you just gave me says:- Calculate the angles where walls begin or end. Cast a ray from the center along each angle. Fill in the triangles generated by those rays. I think I have finished the two above process and I am left with the final one i.e. to fill in the triangles generated by those rays and that is exactly what I am asking. @DMGregory \$\endgroup\$ – Sabin Pyakurel Apr 23 '20 at 12:08
  • \$\begingroup\$ So how have you tried building your visibility polygon mesh using those raycast hits so far? Remember to search for past Q&A covering this topic too \$\endgroup\$ – DMGregory Apr 23 '20 at 12:13
  • \$\begingroup\$ I updated my question if interested. And the truth is I have tried only much as you have seen. Whenever I try to google this question, any blog and website just tells that draw a raycast and explains how it is done. And when the part of filling the triangles comes in play, the blog and websites just write "Fill the triangle" like the link you have sent me. It looks to me that filling in the triangle between two raycast line must be easier and that is why, they haven't mentioned it and that is the reason no one asked this. (or maybe I am dumb) \$\endgroup\$ – Sabin Pyakurel Apr 23 '20 at 12:21
  • \$\begingroup\$ I don't see any attempt in this code to build a mesh with the triangles you've found, using Unity's Mesh class. Can you please add that part and show us where you're stuck? \$\endgroup\$ – DMGregory Apr 23 '20 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.