[SOLVED] localScale movement in 3d world space coordinates WITH scaling
So, I have been doing a LOT of testing on snapping objects together in realtime, and incorporating scaling of all the 3d vectors at the same time while having them try to still snap respective to scale.
This, as many I have read about, has been a big issue since devs seem to scale in script based on localScale (easy to do). Then, there is talk about transferring local to world scale, pos, etc.
That actually became confusing to me, even though I love math and use it every single day in hobby. So, I decided to use the numbers and might have found an easy way to figure how to do what I need to do and it might be helpful if anyone can prove it works (in science, 3 of the same conclusions are required to confirm a theory, so Ill take at least one if possible lol).
Here is the idea: 2 game objects Each have the ability to detect 6 axis collisions (take the cube for example since its an easy poly with hard corners). So, placing one "cube" to the left of another, it will detect a collision at a specified distance. All good. Now, we can use that collision to move the active object to the inactive one (i.e. snapping effect).
To do this, we need world coordinates since local coordinates are a division of such in the open world of unity3d as I have found. But, there might be a most simple way even with scale in play.
Each cube say is vector3(1,1,1) localScaled. So, snapping would be easy using the TranformPoint option in Unity. It would tough edge to edge perfectly since both are 1:1.
When scaling however, things start to either oerlap, or not reach far enough DUE TO the scale differences.
Can anyone verify this is a solution: Take the distance from centerpoint of the inactive object to the edge of collision (i.e. 1/2 dist of axis). THen, I find that the active object uses center point also, and if you go from centerpoint to centerpoint, and active object is larger, the active object will overlap, right? ANd if smaller it will not even reach inactive obj.
So, just find the 1/2 axis magnitude of inactive, and the active object can be calculated to snap that distance +/- (depending on left/right relationships) added to 1/2 axis of the active object? That way, the active object will hit its centerpoint on the edge of the inactive object, and then move the additional 1/2 axis magnitude of itself in the same direction to be finally on the edge of the inactive object, smooth as butter, no matter the scale.
Here is a diagram I made that should show what this means:
What are the thoughts on this and does this make sense? I am not great at explaining things but Ill answer anything confusing or questionable. Just wanted to try to make something easier and think I found "a" way as far as scripting goes, I can only assume. SO, what would be a good formula that would fix the example in the pic to get A next to B regardles of scale, using the distance lines I shown? Because, I found that "locally" scaling, causes this diagram to happen in "world" positioning.
Hope that makes sense :/
Answer by sniper02311 · Jan 19, 2019 at 04:47 AM
ANSWER: I found out how to do this just using the TransformDirection formula and adding scale into its Vector 3 positioning.
So, in the pic above, and this also takes into account both scale factors of local objects transform, that the formula to make A next to B and stay there even if scaled (e.x. in the front of B on blue axis), the formula would be:
objA.transform.position = objB.transform.position +objB.transform.TransformDirection(new Vector3(0, 0, (-objB.transform.localScale.z/2) - objA.transform.localScale.z/2));
if gameObject A is calle objA, and gameObject B is called objB.
Hope this helps someone someday.
Your answer
Follow this Question
Related Questions
How to reset camera rotation/ position after "Free look"? 0 Answers
Creating a camera controller for isometric view - Positioning the camera 1 Answer
Check if rotation if facing towards a vector3 0 Answers
transform.forward with rotation offset variable? 1 Answer
Resetting my cannon fire to its original transform 0 Answers