Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problem setting vertices behind object in World Space

Discussion in 'Scripting' started by LordMikeVI, Feb 3, 2016.

  1. LordMikeVI

    LordMikeVI

    Joined:
    Jul 2, 2015
    Posts:
    15
    For a while now I want to create a wall behind my vehicle like in Tron.
    So i made a script that does exactly that but with wrong calculations.
    The Wall is set off to the front right as seen in the picture below: Screen Shot 2016-02-03 at 15.45.39.png

    As seen in the screenshot the wall script is attached to the wall Object that is a child of the hover craft.
    Here is the code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
    6.  
    7. public class WallScript : MonoBehaviour {
    8.  
    9.  
    10.     //public GameObject selectedobject;
    11.     public float height = 1;
    12.     private int z = 1;
    13.     private int i = 0;
    14.     private bool newcalc = false;
    15.     private Mesh mesh;
    16.     private int firstnormals = 1;
    17.  
    18.  
    19.     List <Vector3> vList = new List<Vector3>();
    20.     List <int>  triList = new List <int>();
    21.     List <Vector3> normalsList = new List<Vector3>();
    22.     List <Vector2> uvList = new List <Vector2>();
    23.  
    24.  
    25.  
    26. // Use this for initialization
    27. void Start () {
    28.  
    29. GetComponent<MeshFilter>().mesh = mesh = new Mesh();
    30.  
    31. vList.Add(new Vector3(0,0,0));
    32. vList.Add(new Vector3(0,height,0));
    33. vList.Add(new Vector3(0,0,0));
    34. vList.Add(new Vector3(0,height,0));
    35.  
    36. Debug.Log("Started");
    37.  
    38.  
    39. mesh.vertices = vList.ToArray();
    40.  
    41.  
    42. }
    43.  
    44. // Update is called once per frame
    45. void FixedUpdate () {
    46.  
    47.  
    48. if(vList[z-1] != transform.position + new Vector3(0,0,0)
    49.     || vList[z] != transform.position + new Vector3(0,height,0))
    50. {
    51.     vList.Add(transform.position);
    52.     vList.Add(transform.position + new Vector3(0, height, 0));
    53.     vList.Add(transform.position);
    54.     vList.Add(transform.position + new Vector3(0, height,0));
    55.  
    56.     mesh.vertices = vList.ToArray();
    57.     z += 4;
    58.     newcalc = true;
    59.  
    60.     Debug.Log("Noticed Vertices are not the same");
    61.  
    62. }
    63.  
    64. if(newcalc == true)
    65. {
    66.     triList.Add(i+0);
    67.     triList.Add(i+1);
    68.     triList.Add(i+4);
    69.  
    70.     triList.Add(i+1);
    71.     triList.Add(i+5);
    72.     triList.Add(i+4);
    73.  
    74.     triList.Add(i+6);
    75.     triList.Add(i+3);
    76.     triList.Add(i+2);
    77.  
    78.     triList.Add(i+6);
    79.     triList.Add(i+7);
    80.     triList.Add(i+3);
    81.  
    82.     mesh.triangles = triList.ToArray();
    83.  
    84.     i = i+4;
    85.     newcalc = false;
    86.  
    87. if(firstnormals == 1)
    88. {
    89.     normalsList.Add(new Vector3(0,0,1));
    90.     normalsList.Add(new Vector3(0,0,1));
    91.     normalsList.Add(new Vector3(0,0,1));
    92.     normalsList.Add(new Vector3(0,0,1));
    93.     firstnormals = 0;
    94. }
    95.  
    96.     normalsList.Add(new Vector3(0,0,1));
    97.     normalsList.Add(new Vector3(0,0,1));
    98.     normalsList.Add(new Vector3(0,0,1));
    99.     normalsList.Add(new Vector3(0,0,1));
    100.  
    101.     mesh.normals = normalsList.ToArray();
    102. }
    103. }
    104. }
    Does anybody know why it does that? The first two vertices in line 31-34 are set correctly but then the other ones created in line 51-54 are not where they should be. Also the whole wall rotates with my Object and I don't want that.
    Thanks in advance!
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    The vertices in a mesh renderer are always in local space. Your trail is moving along with the object. You add a point when the object is at (1,1), the point is at (1,1) in local space which probably becomes (2,2) in world space.

    Have the mesh renderer itself be on a separate object which never moves from (0,0).
     
  3. LordMikeVI

    LordMikeVI

    Joined:
    Jul 2, 2015
    Posts:
    15
    Thanks for the tip!
    But how do I make the Mesh Renderer render my wall if it's attached to the new Object and not the wall itself?
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    What I'm saying is don't have it attached to the new Object. This may involve splitting it into two scripts: the 'recorder' and the 'renderer'. They have references to each other, and on start, the renderer detaches itself from the parent and sets its position to (0,0,0). The renderer can check the recorder's position to update the wall mesh.
     
  5. LordMikeVI

    LordMikeVI

    Joined:
    Jul 2, 2015
    Posts:
    15
    Thanks!
    So I am planing on splitting the script as you said. Attaching the "Renderer" to an empty game Object on world Space (0,0,0). The "Recorder is still on my moving object." Does it matter if I give the Local Coordinates of the "Recorder " to the "Renderer" or do I have to transform them to World Space first?
    If I understood anything wrong please tell me. I am grateful for any other tips.
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Your renderer will want to have everything in world space.
     
  7. LordMikeVI

    LordMikeVI

    Joined:
    Jul 2, 2015
    Posts:
    15
    Okay worry for being away for such a long time but I tried to fix it but nothing seemed to work.
    I now split the script in two scripts. The wallRecorder and the wallRenderer.
    WallRecorder:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class WallRecorder : MonoBehaviour {
    6.  
    7.     public WallRenderer wrender;
    8.     public NewHover nh;
    9.     public Vector3 vs;
    10.     public Vector3 vz;
    11.    
    12.  
    13.     void Start () {
    14.    
    15.         vs = transform.TransformDirection(new Vector3(0,0,0));
    16.         wrender.renderingStart();
    17.     }
    18.    
    19.    
    20.     void Update () {
    21.    
    22.         if(nh.m_body.IsSleeping() == false)
    23.         {
    24.             Vector3 vz = transform.TransformDirection(new Vector3(0,0,0));
    25.            
    26.             wrender.renderingUpdate();
    27.         }
    28.     }
    29.    
    30.    
    31.    
    32. }
    33.  
    and the WallRenderer:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class WallRenderer : MonoBehaviour {
    6.  
    7.     public float height = 1;
    8.     private int i = 0;
    9.     private Mesh mesh;
    10.     private bool firstnormals = true;
    11.  
    12.  
    13.     List <Vector3> vList = new List<Vector3>();
    14.     List <int>  triList = new List <int>();
    15.     List <Vector3> normalsList = new List<Vector3>();
    16.     List <Vector2> uvList = new List <Vector2>();
    17.  
    18.     private WallRecorder wrecorder;
    19.  
    20.     // Use this for initialization
    21.     void Start () {
    22.    
    23.    
    24.    
    25.     }
    26.    
    27.     // Update is called once per frame
    28.     void Update () {
    29.    
    30.     }
    31.    
    32.    
    33.     public void renderingStart (){
    34.    
    35.         GetComponent<MeshFilter>().mesh = mesh = new Mesh();
    36.  
    37.         vList.Add(wrecorder.vs);
    38.         vList.Add(wrecorder.vs + new Vector3 (0,height,0));
    39.         vList.Add(wrecorder.vs);
    40.         vList.Add(wrecorder.vs + new Vector3 (0,height,0));
    41.  
    42.         Debug.Log("Started");
    43.  
    44.         mesh.vertices = vList.ToArray();
    45.     }
    46.    
    47.    
    48.     public void renderingUpdate (){
    49.    
    50.         vList.Add(wrecorder.vz);
    51.         vList.Add(wrecorder.vz + new Vector3(0, height, 0));
    52.         vList.Add(wrecorder.vz);
    53.         vList.Add(wrecorder.vz + new Vector3(0, height,0));
    54.  
    55.         mesh.vertices = vList.ToArray();
    56.        
    57.         newCalc();
    58.  
    59.         Debug.Log("Noticed Vertices are not the same");
    60.        
    61.     }
    62.    
    63.    
    64.     void newCalc () {
    65.    
    66.         triList.Add(i+0);
    67.         triList.Add(i+1);
    68.         triList.Add(i+4);
    69.  
    70.         triList.Add(i+1);
    71.         triList.Add(i+5);
    72.         triList.Add(i+4);
    73.  
    74.         triList.Add(i+6);
    75.         triList.Add(i+3);
    76.         triList.Add(i+2);
    77.  
    78.         triList.Add(i+6);
    79.         triList.Add(i+7);
    80.         triList.Add(i+3);
    81.  
    82.         mesh.triangles = triList.ToArray();
    83.  
    84.         i = i+4;
    85.        
    86.        
    87.         if(firstnormals == true)
    88.         {
    89.                 normalsList.Add(new Vector3(0,0,1));
    90.             normalsList.Add(new Vector3(0,0,1));
    91.             normalsList.Add(new Vector3(0,0,1));
    92.             normalsList.Add(new Vector3(0,0,1));
    93.             firstnormals = false;
    94.         }
    95.  
    96.         normalsList.Add(new Vector3(0,0,1));
    97.         normalsList.Add(new Vector3(0,0,1));
    98.         normalsList.Add(new Vector3(0,0,1));
    99.         normalsList.Add(new Vector3(0,0,1));
    100.  
    101.         mesh.normals = normalsList.ToArray();
    102.     }
    103. }
    104.  
    I get the error "NullReferenceException: Object reference not set to an instance of an object WallRecorder.Update() (WallRecorder line 22)"

    Also the wall is still being drawn as before.
    Can you maybe help me again? Thanks man!
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Either "nh" is null, or your NewHover's "m_body" reference is null.