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 /
  • Help Room /
avatar image
0
Question by cancub · Feb 20, 2017 at 12:31 AM · c#meshposition

Why does it matter if I use one GameObject's position or another's when drawing a mesh?

So I have the following code:

 private GameObject wheel;
 private GameObject barrel;
 
 
 // Use this for initialization
 void Start () {
 
     barrel = new GameObject ("barrel");
     barrel.AddComponent<MeshFilter>();
     barrel.AddComponent <MeshRenderer>();
     barrel.GetComponent<MeshRenderer> ().material = barrelMaterial;
 
     wheel = new GameObject ("wheel");
     wheel.AddComponent<MeshFilter>();
     wheel.AddComponent <MeshRenderer>();
     wheel.GetComponent<MeshRenderer> ().material = wheelMaterial;
 
     DrawCannon ();
 }
 
 private void DrawCannon() {
     // we need to draw both the cannon barrel and the wheel
 
     // start with the wheel
 
     // how detailed we want the wheel to be
     List<Vector3> wheelVertices = new List<Vector3> ();
     int wheelVerticesCount = 20;
     float radius = 0.14f;
 
     int multiple = isBallCannon ? -1 : 1;
 
     // move the wheel to the position that we'd like it to be
     barrel.transform.position = new Vector3 (multiple * 10f, -4f + radius,-0.2f);
     wheel.transform.position = new Vector3 (multiple * 10f, -4f + radius,-0.2f);
 
     float rads = Mathf.PI * 2 / (float)wheelVerticesCount;
 
     // add the center vertex
     wheelVertices.Add(transform.position);
 
     for (int i = 0; i < wheelVerticesCount; i++) {
         // add the next vertex along the circle 
         wheelVertices.Add(transform.position + radius *
                     new Vector3(Mathf.Cos(i*rads),Mathf.Sin(i*rads),0));
     }
 
     wheel.GetComponent<MeshFilter> ().mesh.vertices = wheelVertices.ToArray ();
 
     // now that we have all the vertices of the circle object, we can draw them
     int[] wheelTriangles = new int[wheelVerticesCount*3];
 
     int j = 0;
     for (int i = wheelVerticesCount; i > 0; i--) {
         wheelTriangles [j] = i;
         if (i > 1) {
             wheelTriangles [j + 1] = i - 1;
         } else {
             wheelTriangles [j + 1] = wheelVerticesCount;
         }
         wheelTriangles [j + 2] = 0;
         j += 3;
     }
 
     wheel.GetComponent<MeshFilter> ().mesh.triangles = wheelTriangles;
 
 
     // now draw the barrel, it should just be a rectangle
 
     List<Vector3> barrelVertices = new List<Vector3> ();
 
     // we want this barrel to be just a bit taller than the wheel and about 3 time as long
     barrelVertices.Add(new Vector3(barrel.transform.position.x-3*radius,barrel.transform.position.y + radius*1.2f,0f));
     barrelVertices.Add(new Vector3(barrel.transform.position.x+3*radius,barrel.transform.position.y + radius*1.2f,0));
     barrelVertices.Add(new Vector3(barrel.transform.position.x-3*radius,barrel.transform.position.y - radius*0.9f,0));
     barrelVertices.Add(new Vector3(barrel.transform.position.x+3*radius,barrel.transform.position.y - radius*0.9f,0));
 
     barrel.GetComponent<MeshFilter> ().mesh.vertices = barrelVertices.ToArray ();
 
     // now that we have all the vertices of the circle object, we can draw them
     int[] barrelTriangles = new int[6] {0,1,2,1,3,2}; 
 
     barrel.GetComponent<MeshFilter> ().mesh.triangles = barrelTriangles; 
 
 }

This code is running in a script attached to an empty GameObject that is positioned at the origin. You'll note that to draw the wheel I work with the creating object's position and that seems to place the wheel where I want it to be. However, when I create the barrel and use what I actually think the position should be as a starting point for drawing the vertices, the barrel's actual position appears to be doubled in each of the axes, as can be seen in the image here:

alt text

When I print out the positions of both the wheel and the barrel GameObjects, their positions are shown to be exactly the same, which makes sense because that's the position I gave them in the code. So what's going on with the barrel? When I again use the creating GameObject's position to find the barrel's vertices, everything is fine. Neither is a child object of any other object, which is verified by looking at the heirarchy.

capture.png (164.4 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by cancub · Feb 20, 2017 at 12:45 AM

I'm an idiot. When you move the position of the object, you no longer need to reference the origin when looking at the placement of vertices. Since the parent object was at the origin, I was adding (0,0,0) to the vertices, which is the correct thing to do after having moved the object (or just not add anything at all).

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

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

316 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 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

Draw torus with a spot in the center 1 Answer

Changing a mesh with C# and Resource.Load() 0 Answers

Change player position based on loaded scene 1 Answer

How does physics work in this code? 0 Answers

How do i use lines to calculate triangles on mesh?? 0 Answers


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