Why is the position of my object different from the field of object?

I am writing a field of view method that is learned by Code Monkey. The field of view is made by mesh and I am designing the mesh. If my object (human) moves, the start of the field of view moves faster than object.

this is mistake happening

this is the scene

this is the script of field of view.

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

public class MyField : MonoBehaviour
{
    private MeshFilter _meshFilter;
    public float ViewAngle;
    public float ViewDistance;
    public int RayCount;
    private Mesh _mesh;
    private Vector3 _origin;
    public LayerMask LayerMask;
    private Transform _father;
    private void Awake()
    {
        _meshFilter = GetComponent<MeshFilter>();
        _father = transform.parent;
    }

    void Start()
    {
        _mesh = new Mesh();
        _meshFilter.mesh = _mesh;
    }
    
    void Update()
    {
        _origin = _father.position;
        float angle = ViewAngle;
        float AngleDecrease = ViewAngle / RayCount;
        Vector3[] vertices = new Vector3[RayCount + 2];
        Vector2[] uv = new Vector2[vertices.Length];
        int[] triangles = new int[3 * RayCount];
        vertices[0]=_origin;
        int triangleIndex = 0;
        Debug.Log(_origin);
        //draw mesh
        for (int i = 1; i < vertices.Length; i++)
        {
            Vector3 dir = new Vector3(Mathf.Cos(angle * (Mathf.PI / 180)), Mathf.Sin(angle * (Mathf.PI / 180)),0);
            RaycastHit2D raycastHit2D = Physics2D.Raycast(_origin, dir, ViewDistance, LayerMask);
            Vector3 vertice;
            if (raycastHit2D.collider == null) {
                vertice = _origin+dir * ViewDistance;
            } else {
                vertice = raycastHit2D.point;
            }
            vertices[i] = vertice;
            angle -= AngleDecrease;
            if (i >= 2)
            {
                triangles[0 + triangleIndex] = 0;
                triangles[1 + triangleIndex] = i-1;
                triangles[2 + triangleIndex] = i;
                triangleIndex += 3;
            }
        }
        _mesh.vertices = vertices;
        _mesh.uv = uv;
        _mesh.triangles = triangles;
    }
}

And I check out the position of my object. It is right. All the variables are initialized.

1 answer

  • answered 2022-05-04 13:48 Chuck

    I think the flaw here is that you're making the mesh on the MyField script, but you're basing the points based on the parent GameObject.

    If your parent GameObject is at <0,0>, then your mesh origin is at <0,0>. If your parent GameObject moves to <1,0>, then you're making your mesh origin at <1,0> but your Mesh is local. This means that you're actually setting your mesh to <1,0> + <1,0> = <2,0> in world coordinates.

    Consider offsetting all your points instead:

    void Update()
    {
        _origin = _father.position;
        float angle = ViewAngle;
        float AngleDecrease = ViewAngle / RayCount;
        Vector3[] vertices = new Vector3[RayCount + 2];
        Vector2[] uv = new Vector2[vertices.Length];
        int[] triangles = new int[3 * RayCount];
        //vertices[0]=_origin;
        vertices[0]=Vector3.zero;  // <------- THIS IS A CHANGE
        int triangleIndex = 0;
        Debug.Log(_origin);
        //draw mesh
        for (int i = 1; i < vertices.Length; i++)
        {
            Vector3 dir = new Vector3(Mathf.Cos(angle * (Mathf.PI / 180)), Mathf.Sin(angle * (Mathf.PI / 180)),0);
            RaycastHit2D raycastHit2D = Physics2D.Raycast(_origin, dir, ViewDistance, LayerMask);
            Vector3 vertice;
            if (raycastHit2D.collider == null) {
                //vertice = _origin+dir * ViewDistance;
                vertice = dir * ViewDistance; // <---- THIS IS A CHANGE
            } else {
                //vertice = raycastHit2D.point;
                vertice = raycastHit2D.point - _origin; // <----- THIS IS A CHANGE
            }
            vertices[i] = vertice;
            angle -= AngleDecrease;
            if (i >= 2)
            {
                triangles[0 + triangleIndex] = 0;
                triangles[1 + triangleIndex] = i-1;
                triangles[2 + triangleIndex] = i;
                triangleIndex += 3;
            }
        }
        _mesh.vertices = vertices;
        _mesh.uv = uv;
        _mesh.triangles = triangles;
    }
    

    Just a few changes here. Again, I think your issue is that you're defining a local object (the Mesh) in world coordinates when it actually exists in local coordinates. The changes I'm proposing here shift the mesh to local coordinates.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum