Search Unity

Mesh is moving faster than the game object is attached to.

Discussion in 'General Graphics' started by davidson27, Mar 3, 2021.

  1. davidson27

    davidson27

    Joined:
    Feb 10, 2018
    Posts:
    18
    I have been following this tutorial to make a 'field of view cone' for NPCs. The tutorial was made for 2D games but adapting it to 3D has been fairly straightforward except for this problem I cannot seem to solve.

    The problem happens whenever the game object is not centered at (0,0,0) the mesh will be much further in any direction than the physics and debug rays. I have attached some pictures showing what happens when you move the game object and the code I'm working with below.

    Is there something that governs the movement of the mesh on the game object that I am not aware of?




    Here is the game running when the game object is at the origin.

    Screen Shot 2021-03-02 at 7.29.01 PM.png

    This happens when I move the game object in the negative X direction

    Screen Shot 2021-03-02 at 7.29.51 PM.png


    This will happen when I move the game object in the positive Z direction

    Screen Shot 2021-03-02 at 7.29.12 PM.png


    This will happen when I move the game object in the positive Y direction

    Screen Shot 2021-03-02 at 7.29.31 PM.png

    The fov is made from an empty game object that is given a script, a material, a mesh filter, and a mesh renderer.
    Screen Shot 2021-03-02 at 7.34.36 PM.png


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FieldOfView : MonoBehaviour {
    6.    
    7.     [SerializeField] private LayerMask layerMask;
    8.     private Mesh mesh;
    9.     private Vector3 origin;
    10.     private float startingAngle;
    11.     private float fov;
    12.  
    13.     private void Start()
    14.     {
    15.         mesh = new Mesh();
    16.         GetComponent<MeshFilter>().mesh = mesh;
    17.         fov = 90f;
    18.  
    19.     }
    20.     private void  LateUpdate()
    21.     {
    22.         int rayCount = 50;
    23.  
    24.         float angle = startingAngle;
    25.         origin = transform.position;
    26.         Vector3 eyeHeight = Vector3.zero; //new Vector3(0, 2.5f, 0);
    27.         float angleIncrease = fov/rayCount;
    28.         float viewDistance = 10f;
    29.  
    30.         Vector3[] vertices = new Vector3[rayCount + 1 + 1];
    31.         Vector2[] uv = new Vector2[vertices.Length];
    32.         int[] triangles = new int[rayCount * 3];
    33.  
    34.         vertices[0] = origin;
    35.  
    36.         int vertexIndex = 1;
    37.         int triangleIndex = 0;
    38.  
    39.         RaycastHit raycastHit;
    40.         for (int i = 0; i <= rayCount; i++)
    41.         {
    42.             Vector3 vertex;
    43.             // RayCast(Origin, Direction, MaxDistance, LayerMask, QueryTriggerInteraction)
    44.             Physics.Raycast(origin + eyeHeight, GetVectorFromAngle(angle), out raycastHit, viewDistance, layerMask);
    45.  
    46.             Debug.DrawRay(origin + eyeHeight, GetVectorFromAngle(angle)*viewDistance,Color.blue,0,true);
    47.             if(raycastHit.collider == null)
    48.             {
    49.                 // object was not hit
    50.                 Debug.Log("No Hit");
    51.                 vertex = origin + GetVectorFromAngle(angle)* viewDistance;
    52.             }
    53.             else
    54.             {
    55.                 // object was hit
    56.                 Debug.Log("Hit");
    57.                 Debug.Log(raycastHit);
    58.  
    59.                 // raycastHit.point.y = 0;
    60.                 //vertex = raycastHit.point
    61.                 vertex = Vector3.Scale(raycastHit.point, new Vector3(1,0,1));
    62.             }
    63.             vertices[vertexIndex] = vertex;
    64.  
    65.             if(i > 0)
    66.             {
    67.                 triangles[triangleIndex + 0] = 0;
    68.                 triangles[triangleIndex + 1] = vertexIndex -1;
    69.                 triangles[triangleIndex + 2] = vertexIndex;
    70.  
    71.                 triangleIndex += 3;
    72.             }
    73.            
    74.             vertexIndex++;
    75.             // this is not += because increasing in angle in unity goes clockwise instead of counterclockwise
    76.             angle -= angleIncrease;
    77.         }
    78.  
    79.  
    80.         triangles[0] = 0;
    81.         triangles[1] = 1;
    82.         triangles[2] = 2;
    83.  
    84.         mesh.vertices = vertices;
    85.         mesh.uv = uv;
    86.         mesh.triangles = triangles;
    87.     }
    88.  
    89.     Vector3 GetVectorFromAngle(float angle)
    90.     {
    91.         float angleRad = angle * (Mathf.PI/180f);
    92.         return new Vector3(Mathf.Cos(angleRad), 0, Mathf.Sin(angleRad));
    93.     }
    94.  
    95.     float GetAngleFromVectorFloat(Vector3 direction){
    96.         direction = direction.normalized;
    97.         float n = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
    98.         if (n < 0 ) n += 360;
    99.  
    100.         return n;
    101.     }
    102.     //when on a NPC object the NPC will call this function
    103.     public void SetOrigin(Vector3 origin)
    104.     {
    105.        
    106.         this.origin = origin;
    107.     }
    108.  
    109.     //when on a NPC object the NPC will call this function
    110.     public void SetAimDirection(Vector3 aimDirection)
    111.     {
    112.         startingAngle = GetAngleFromVectorFloat(aimDirection) - fov / 2f;
    113.     }
    114. }
     
  2. rjonaitis

    rjonaitis

    Unity Technologies

    Joined:
    Jan 5, 2017
    Posts:
    115
    Physics.Raycast and Debug.DrawRay uses world space coordinates. Mesh Vertices use object space coordinates. So when you move the object away from 0,0,0, the vertices are being offset by the object to world transformation.
     
    davidson27 likes this.
  3. davidson27

    davidson27

    Joined:
    Feb 10, 2018
    Posts:
    18
    That is good information to know, thanks. Is there a way to force the use of object space or world space when desired?
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352