- Home /
I need help with the location of instatiated objects on the scene and correct interaction with this
Now I have script that creates mesh from the vertices I created. Than in this script I instatiate prefab with MeshRenderer and MeshFilter components on it and assign created mesh to this instatiated object. These intatiated-gameobjects should been teleports for player and when I enter into one gameobject it should teleport me to another. When I create instatiate-object I set it position to Vector3.zero for the correct location of mesh between created dots just like this
GameObject mesh = Instantiate(Mesh, Vector3.zero, Quaternion.identity);
But the problem is that position of all created gameobjects with meshes is (0,0,0) in global coordinations and when I try to teleport it always moves me to (0,0,0) position. I realize that reason of that is maybe in setting gameobject position to Vector3.zero but when I try to set position to another (such as first vertice or player transform) it moves Mesh far away from the desired position.
So my question is: how I can correctly locate instatiated gameobject so that it have global world coordinates? P.S Dot in red circle is one of the created vertices of which mesh is made. It position is (32, -19, -104), but position of instatiated object is (0,0,0)
Answer by jjrage · Aug 25, 2019 at 08:23 PM
I found a solution to the problem. The thing is, the every mesh was created at Vector3.zero
point and its looks like at the same time each mesh was in its own coordinate system with center in mesh position. When I tried to teleport to any mesh object it was moved my player to (0,0,0) global coordinates, not to particular mesh position that I needed. Solution was very simple: despite that every mesh has (0,0,0) coordinates (regardless of real global position of object) player object can teleport to mesh using it Collider position
Before:
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Player")
{
if(index == 0)
{
collision.gameObject.transform.position = portals.portals[1].transform.position;
}
if (index == 1)
{
collision.gameObject.transform.position = portals.portals[0].transform.position;
}
}
}
After:
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Player")
{
if(index == 0)
{
collision.gameObject.transform.position = portals[1].transform.GetComponent<Collider>().bounds.center;
}
if (index == 1)
{
collision.gameObject.transform.position = portals[0].transform.GetComponent<Collider>().bounds.center;
}
}
}
I can assume that this is due to mesh pivot point that needed to be recalculated or maybe any other reason
Answer by sacredgeometry · Aug 18, 2019 at 12:29 PM
Have you tried setting the position of the GameObject
to where you want it after instantiating it?
Example
GameObject mesh = Instantiate(Mesh, Vector3.zero, Quaternion.identity);
mesh.transform.position = new Vector3(12, 23, 43);
Note
Also You should not being using the meshes coordinate space to determine its local or world position. You should probably try to draw the mesh centred around the origin or at the very least around its point of rotation (I would argue that thats also a bad practice as you can emulate this with nested transforms anyway) then position the game object that the mesh is on instead.
If you are doing this procedurally adjusting your code would be necessary if you are using a 3D modelling suite then there is probably a feature that lets you do this built in. e.g.
Yes I'm already tried to set the position of the GameObject
after instatiating. For example I'm setting GameObject
position at position of first vertice (On first screen marked as "1") and it's actually setting but not quite at same spot. But when I'm setting GameObject
position at Vector3.zero
it position located on red cross (first screen). You can also look at both screenshot and make sure they have the same location but are in different places
Can you upload your mesh generation code please?
private void Create$$anonymous$$esh()
{
//get created dots positions
m_drawLine = gameObject.GetComponent<DrawLine>();
m_vertices3D = new Vector3[m_drawLine.m_dotPos.Count];
for (int i = 0; i < m_drawLine.m_dotPos.Count; i++)
{
m_vertices3D[i] = m_drawLine.m_dotPos[i];
}
//for creating mesh I used Triangulator class that i found on Unity wiki
Triangulator tr = new Triangulator(m_vertices3D);
int[] indices = tr.Triangulate();
//create and set vertices for mesh
Vector3[] vertices = new Vector3[m_vertices3D.Length];
for (int i = 0; i < vertices.Length; i++)
{
vertices[i] = new Vector3(m_vertices3D[i].x, m_vertices3D[i].y, m_vertices3D[i].z);
}
//create "$$anonymous$$esh" GameObject with $$anonymous$$eshRenderer and $$anonymous$$eshFilter components
GameObject mesh = Instantiate(Resources.Load("$$anonymous$$esh"), Vector3.zero, Quaternion.identity) as GameObject;
//create new $$anonymous$$esh and assign properties
m_msh = new $$anonymous$$esh();
m_msh.vertices = vertices;
m_msh.triangles = indices;
m_msh.RecalculateNormals();
m_msh.RecalculateBounds();
$$anonymous$$eshFilter meshFilter = mesh.GetComponent<$$anonymous$$eshFilter>();
$$anonymous$$eshRenderer meshRenderer = mesh.GetComponent<$$anonymous$$eshRenderer>();
$$anonymous$$eshCollider meshCollider = mesh.GetComponent<$$anonymous$$eshCollider>();
meshCollider.shared$$anonymous$$esh = m_msh;
meshRenderer.material = m_material;
meshFilter.mesh = m_msh;
//ResetDots clear array of dots so we can create another $$anonymous$$esh with new vertices
ResetDots();
}