Welcome to to world of game development and working with Unity! A note on forum etiquette: in the future, don’t post screenshots of code, copy the text of the code and place it in code tags in your post. Screenshots can be hard to read for different people on different devices, and image hosting sites usually don’t host the images forever, which can break the post for people years down the line. Using code tags properly
One problem I notice is in your start method, when you are calculating offsets, you are checking the distance from a mesh vertex to your transform position. A vertex from a mesh is a point relative to that object’s transform, while your transform position is a point relative to the world origin. We would say “the vertex is specified in object-space” and “position is in world-space”. Because those two vectors are in different spaces, computing the distance between them doesn’t work; the Distance method assumes they are in the same space and calculates the distance in that space. To see that, place your game object at (0,0,0) and Debug.Log the distance on line 27, after you calculate. Play the game and note the values. Then, move the object to (10,0,0) and play again. You should see that the distances aren’t the same.
To fix that, you need to convert the vectors you are comparing into the same space. In your case, you want to convert into local space, since that’s what the vertices are already in. Fortunately, converting transform.position into the transform’s local space is easy: it’s (0,0,0). By definition, the transform’s position is the origin of its local space. So now, you would write float distance = Vector3.Distance(Vector3.zero, meshVerticies_);
. This can be simplified to just float distance = meshVerticies*.magnitude;
. The magnitude of a vector is just the length of the vector, which is also a point’s distance from the origin. If you do the distance check test again, starting at (0,0,0) and trying again at (10,0,0), you should see that the results are the same each time now.*_
1 Like