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 Atarius · Jan 27, 2016 at 03:04 PM · transformmeshcoordinatesvertex

How to successfully transform local mesh vertex coordinates into world coordinates?

Here is what I'm trying to do. I load the mesh and randomly access a vertex from the mesh and extract it from the array. I then want to transform this vertex's location to world coordinates so that I can use it to move another game object in the scene to that location on the first object.

My code works, and even the scaling is alright I'm fairly certain, but the transformed coordinates are off by roughly +8 in the X direction and -12 in the Z direction.

Why is this happening? Here is my code:

using UnityEngine; using System.Collections;

public class Path : MonoBehaviour {

 GameObject model;
 public

 void Start ()
 {
     model = Resources.Load("Model") as GameObject;
     Vector3[] vertices = model.GetComponent<MeshFilter>().sharedMesh.vertices;
     var myIndex1 = Random.Range(0, vertices.Length);
     Vector3 p = vertices[myIndex1];
     print(p);
     Transform tr = model.transform;
     Vector3 V = tr.TransformPoint(p);
     print(V);

     iTween.MoveTo(gameObject, V, 15);

 }
 
 void Update () {
 
 }

}

Comment
Add comment · Show 1
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 Fattie · Jan 27, 2016 at 03:04 PM 0
Share

google for 100s of QA on this

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FortisVenaliter · Jan 27, 2016 at 07:08 PM

You're using model.transform to convert it into World Coordinates. But model comes from Resources, which means it's a prefab, which means it isn't in the scene, which means it doesn't have true local coordinates. Are you sure you don't want to use the local object's transform instead?

Comment
Add comment · Show 6 · 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 Atarius · Jan 27, 2016 at 08:09 PM 0
Share

Thank you for the helpful reply. You are correct, and what you said makes a lot of sense. This script is attached to GameObject1 because it is the script that will make it move in the game. However, I need the local coordinates of another object, GameObject2 in the scene first.

Is there a way I can access GameObject2's local transform using this same script attached to GameObject1?
Or is there a way I can send information across scripts so that I could attach a script to GameObject2 and to retrieve and then send the coordinates to GameObject1's script?

avatar image FortisVenaliter Atarius · Jan 27, 2016 at 08:14 PM 0
Share

Sure. You just need to add a class variable for the second GameObject, and then assign it in the inspector. Then you can use that reference to get the transform and apply whatever math you need.

avatar image Atarius FortisVenaliter · Jan 27, 2016 at 08:52 PM 0
Share

Sorry if this is noobish, I'm still learning here! xD

How would I do this with an object that is spawning in at run time from the prefab I mentioned earlier? Also, how do I assign class variables in the inspector?

Show more comments
Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Converting Mesh to Set of Discrete Coordinates 1 Answer

Create Transform to part of mesh? 0 Answers

Using compute shader, how to take every vertex of a mesh and make a cube spawn and move to each one? 0 Answers

Any script to 'fix' normals/winding order? 3 Answers

Calculating mesh over GameObject scale 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