- Home /
How to apply point value on local coordinate system rather than world coordinate system
Hi all, I am a beginner at Unity and I have a problem that I ve been struggling with for days. Through Websockets I get xy coordinates for certain positions. Now I have an object in my Unity scene which is like an anchor or reference point and has another coordinate system than the main camera (e.g. the x axe of the anchors coordinate system is pointing to the right whereas in the main cameras coordinate system x points forward) and would like to first instantiate an object (gameobject like a robot) at the received position and then move this object according to further received positions, most importantly with respect to the anchors coordinate system (which has another coordinate system than the main camera). So e.g. if i get the position (x,y)=(2,0), I would like to first instantiate the object at (2,0) with respect to the coordinate system of the anchor and then if I get (3,0) move it forward. It works when I child a object to another, but since the anchor is already a child of another object and if I child my robot to my anchor somehow its not working and gives me wrong positions when i run. I also tried to use anchor.transform.InversetransformPoint(receivedPositon) but this somehow also dont work. I hope for some replies and thanks in advance!
I'm not 100% clear, but I think I've figured out what you're describing. I'll need to you to correct my mental image of the scenario if I have this wrong. Without some code I have only assumptions to make, but we'll advance my following of your observations through dialog.
I'm missing explicit example, of both example data and actual object settings, along with the difference between observed results and expected results. I can only speak generally as a result.
What I sense are he issues of local coordinate systems. What I need to pin down is the orientation of the inco$$anonymous$$g data. Is the inco$$anonymous$$g data oriented in a way that matches the anchor? I would assume that is the purpose of the anchor.
Your description tells me the camera's coordinate system is oriented 90 degrees clockwise relative to the anchor. Your description of the anchor seems conventional, but then I only have the reference of the X axis, so I must assume Y and Z axis are otherwise still coincident. This brings to my $$anonymous$$d a question as to why there's an anchor if it is aligned with conventional and default global orientation, unless it is centered elsewhere, but that's not clear.
I sense, with some obvious lack of clarity from my viewpoint, that this is merely a matter of using localPosition and localRotation, but if that's not sufficient then I need to further explore as to why. Generally I'd expect that if the inco$$anonymous$$g data's orientation matches the use of the anchor, then all children of the anchor would be positioned through localPosition and rotated through localRotation, which automatically work through hierarchy to appropriate calculate the ultimate global position.
Hi @JVene, Thanks very much for your reply and time. Long time no hear! I am glad that you answered. Sorry that the above explainations were not clear, I will explain the whole setup. One aspect that makes the issue somehow tricky is that I start the application on a head mounted AR-device (the microsoft hololens), that means that the coordinate system of the main camera is depending on my heads position at the starting time of the app. The next important thing is, in my room there is a roboter with a marker on it. $$anonymous$$y app can scan this marker and based on the position of the marker it will place the anchor once (I will explain why to place an anchor later on). So the anchors coordinate system is depending on the position and orientation of the robot, and its marker, not the main camera. Assu$$anonymous$$g for example that at the start of the app, the robot and marker on it, is to my right and the robot x-axis(forward direction) is positive to my right, whereas my head is looking forward, thus the x axis of the main camera is pointing forward, we have two different coordinate systems, with both x axes perpendicular to each other. And unfortunately things will get a little more complicated (at least for me), because what I get is data from another computer(controlling the real robot),I will call it ROS, which itself has another coordinate system and provides data about the position of the robot in (x,y) coordinates with respect to its own coordinate system, without knowing anything about unity and hololens. $$anonymous$$y tasks is to synchronize all 3 coordinate systems so that the data I get from ROS, will display correctly in Unity/Hololens. Fortunately what I could get from ROS (also through websockets) is the distance between the robot and the origin of the ROS coordinate system. Thats why my idea was to scan the marker, get the position of the marker (= position of the real robot, cause the marker is on the robot) place a virtual robot on that marker position, then add the above mentioned distance between robo and origin to that marker position and place the anchor on that positon. This way the anchor will represent the origin of the ROS coordinate system. I have made my anchor child to the virtual robot so the coordinates I get from the robo will be displayed rightly cause now its with respect to the virtual robots coordinate system( which is also the real robo cause there is the marker on it, sorry that I repaet myself). Now the data I get is sensor data about the position of the real robot with respect to the ROS coordinate system. So thats why i have to apply that data to the anchor. So for example if I get from ROS (x,y) = (2,0), I would like to move the virtual robot to (2,0) with respect only to the anchors coordinate system. $$anonymous$$y idea was to child another virtual robot, lets call it robo2) to that anchor(which itself is a child of the first virtual robot) but that is not working somehow and gives me wrong results.
I hope the problem is more clear now and I am also glad to listen to any better idea on how to solve this problem!
Hi @JVene, I have thought about an idea. Is it possible to place an different coordinate system depending on the orientation of the marker? If I scan the marker, the object is just placed on that marker with some rotation to it with respect to the same main camera coordinate system. So ins$$anonymous$$d I would like to not use rotation but a new coordinate system. With other words I would like to rotate the coordinate system of my virtual robot object ins$$anonymous$$d of rotating only the object with respect to the main cams coord system. That would be a solution for the whole issue i think. Is this possible? EDIT: with rotation of the coord system, I mean that e.g. turn it from a left handed to a right handed. (rotating the axes) e.g. the x-axis of the old one is now the z-axis of the new one
EDIT: I tried some things out with parenting like you suggested and somehow it worked! I will do some more test and report in detail later!
Yes, that's the kind of thing misher is suggesting, because localPosition applied to a child object naturally produces a global 'version' in position.
What I'm contemplating is the basis of all of this. The relativity of the coordinate systems of the real robot, the Unity global 'world', the camera, the headset have been rolling in my own $$anonymous$$d. What's unfortunate is that I can think of about 6 solutions, and I ponder which one is the best foundation while driving (which isn't good, because I start to drive like I'm in a Unity scene).
Answer by misher · Sep 07, 2018 at 11:09 AM
Instantiate new objects as children to your anchor, and set their position using Transform.localPosition
instead of Transfor.position
Hi thanks very much for you answer! I will test some things out and report if it worked!
Your answer
Follow this Question
Related Questions
How to fix child gameobject position in relation to sphere parent transform. 1 Answer
Is it possible to rotate coordinate system instead of object? 1 Answer
Random Animation of gameObject Children 1 Answer
Update transform point 0 Answers
How to align & rotate objects to each other at run time? 1 Answer