Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jjrage · Aug 17, 2019 at 09:06 PM · instantiatetransformpositionvector3instance

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? alt text 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)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

alt text


screenshot-2019-08-18-at-133537.png (33.9 kB)
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jjrage · Aug 18, 2019 at 03:05 PM 0
Share

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 alt text alt text

q2.png (372.6 kB)
q1.png (352.2 kB)
avatar image sacredgeometry jjrage · Aug 18, 2019 at 04:47 PM 0
Share

Can you upload your mesh generation code please?

avatar image jjrage sacredgeometry · Aug 25, 2019 at 08:32 PM 0
Share
 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();
     }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

184 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instance child global position 1 Answer

NGUI - Vector 3 Position different from position 1 Answer

split vector3 coordinates in to x,y,z 1 Answer

Trouble setting up a vector 3. 1 Answer

Why can't I instantiate an object that is +5 in the x and z axis? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges