Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8492373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:42:00+00:00 2026-06-10T22:42:00+00:00

I am attempting to generate a mesh in unity from script. The mesh is

  • 0

I am attempting to generate a mesh in unity from script. The mesh is generated by raycasting out in a particular direction. Then getting the vertices from the hit point or from where the ray terminates. The mesh generates fine and is working well, however the mesh generates approximately 5 to 10 unity units above the location of the object with the attached script. I will attach me script below.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Torch : MonoBehaviour {

    public GameObject  lightmeshholder;

    private int RaysToShoot = 128;
    private float distance = 50;
    private Vector3[] vertices;
    private Vector2[] vertices2d;
    private int[] triangles;

    private Mesh mesh;

    private Texture2D texture;
    private int screenwidth;
    private int screenheight;
    private int grab = 0;

    private RaycastHit hit;


    // Use this for initialization
    void Start () {
    screenwidth = Screen.width;
    screenheight = Screen.height;
    texture = new Texture2D (screenwidth, screenheight, TextureFormat.RGB24, false);

    vertices = new Vector3[RaysToShoot];
    vertices2d = new Vector2[RaysToShoot];
    triangles = new int[(RaysToShoot) +1 ];

    mesh= lightmeshholder.GetComponent<MeshFilter>().mesh;

    }

    // Update is called once per frame
    void Update () {

        float angle =0;

            for(int i=0;i<RaysToShoot;i++){

                        float x = Mathf.Sin(0);
                        x=-5;
            if(Input.GetKey(KeyCode.P)){
                    x = 5;
            }
                        float y = Mathf.Cos(angle);
            if (angle <= 90){
                        angle += 2*Mathf.PI/RaysToShoot;
            }

            Vector3 dir = new Vector3(x,y,0);
        if (Physics.Raycast (this.transform.position, dir,out hit, distance)) 
        {
                Debug.DrawLine (this.transform.position, hit.point,new Color(1,1,0,1));
            Vector3 tmp = lightmeshholder.transform.InverseTransformPoint(hit.point);
            vertices2d[i] = new Vector2(tmp.x,tmp.y);

            }else{
            Vector3 tmp = lightmeshholder.transform.InverseTransformPoint(this.transform.position + dir*distance);
            vertices2d[i] = new Vector2(tmp.x,tmp.y);
            Debug.DrawLine(this.transform.position,dir * distance,Color.red,0); 
            }



        }

        // build mesh

    Vector2[] uvs = new Vector2[vertices2d.Length +1];
    Vector3[] newvertices = new Vector3[vertices2d.Length+1];


    for (int n = 0; n<newvertices.Length-1 ;n++) 
    {


            if(n==0){
            newvertices[0]=this.transform.position; 
                newvertices[1] = vertices2d[0];
                uvs[0] = new Vector2(this.transform.position.x,this.transform.position.y);
                uvs[1] = vertices2d[0];
            }else{
            newvertices[n+1] = vertices2d[n];   
                uvs[n+1] = vertices2d[n];
            }

            if(n==0){
                triangles[0] = 0;   
                triangles[1] = 1;
                triangles[2] = 2;
            }else if(n<newvertices.Length/3){
                triangles[n*3] = 0; 
                triangles[1+n*3] = n+1;
                triangles[2+n*3] = n+2;
            }

    }
        Mesh mesh = new Mesh(); 
        GetComponent<MeshFilter>().mesh = mesh; 
        mesh.Clear();
        mesh.vertices = newvertices;        
        mesh.uv = uvs;  
        mesh.triangles = triangles;
        mesh.RecalculateNormals();          
    }
}  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:42:01+00:00Added an answer on June 10, 2026 at 10:42 pm

    The hitpoints you’re getting from the raycasts are global. You can either:

    1) Make sure the mesh is in a gameobject that is positioned at 0,0,0. Whenever I create a dynamic mesh based on ray hitpoints I usually create a new parent-less gameobject at 0,0,0 and add the mesh to it. (instead of getting a container GameObject externally)

    2) Call lightmeshholder.transform.InverseTransformPoint on the hitpoints from the raycast before building the mesh

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to generate datamatrix barcodes from within itext. This works fine for
I am attempting to generate a list (from four tables) of products, classid, optionid,
I am attempting to do the following: Retrieve a generated pdf from a web
I am attempting to generate the body of an email in Outlook 2007 from
I'm attempting to generate a grid of divs five elements wide from the objects
I'm attempting to execute MSBuild from a NAnt script (which is called by CruiseControl.NET).
As a learning project I'm attempting to re-create the procedurally generated hills from Tiny
I am attempting to generate a potential card 'plays' list from a predetermined hand
I'm attempting to use svcutil.exe to generate -only- the service contracts (interfaces) from a
I am attempting to grab a date supplied via POST, then generate a list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.