- Home /
Using gameObject.translate moves it farther than expected
I have a setup using ngui where I want to use the camera to make a gui, but whenever i called Translate on a child of "UI Root" it moves very far.
My current setup have my "UI Root" set at the origin, and other menu's move the camera left or right to navigate menus. Whenever I select a menu (move right and move a menu into where im going to move my camera), I call translate on the child gameobject of "UI Root" that I want to select as a menu to display and navigate to.
My problem is when I call translate(screenSize*numberToMultiply) it move it over a lot, and it seems to negate it. I call:
uiObject.transform.Translate((screenResolutionWidth*offsetFromTitleMenu), 0,0);, where my width is 350 and my offset is 1, and it moves it to -56000 on the x axis (it is expected to move on only the x-axis).
Why would the child object move so far when I tell it to translate only 350 units left or right? I also look at the scale and multiply the 350 by the scale to compensate, but this also move it the -56000 units. Why would it use this?
It is 1.0. When it is different (I check in the inspector), I multiply this scale by the value I want to move by (as this s the screen width and also the world units) i want to move over by.
I call this once during a function call when a button is clicked (using NGUI on Onclick method)
Well you shot down my idea, and probably @fafase's as well. Just for testing purposes, try removing your Translate() and replacing it with:
transform.position += Vector3.right * 350;
if it is Javascript you can do this ins$$anonymous$$d:
transform.position.x += 350;
Note these are bit different than Translate() since Translate() by default uses local coordinates. But it might give more insight into the problem.
Just for kicks, add a Debug.Log() to make sure the code is only getting called once.
The debug log is only outputting once. I want to use translate. The position setting works fine for what I want, but I want to tween this over x number of seconds and translate is easiest for this.
i use this following code:
guiCam.transform.Translate((screenWidth*sCam),0,0, Space.World);
where guiCam is my gui camera that I move, and sCam is the current scale of my camera. Tbis should work, but doesn't. If I change Space.World to Space.Self, it doesn't change anything.
is OnClick method working when you press down or when you press?
Answer by 1337GameDev · Dec 18, 2013 at 07:41 PM
I solved the problem. The parent object had a scale of 0.000625, which made me moving 350 units into a lot further (as translate calculates to world coords and scales up to scale 1.0).
I just multiplied the parent gameobject's scale against how many units I wanted to move, and it positioned the child where I wanted it.
I wish unity would have this kind of information in the documentation.
Your answer
Follow this Question
Related Questions
Translate object on y axis 0 Answers
How to translate object using script? 1 Answer
How to move a specific texture from a shader? 0 Answers
Moving Object by touching it? 1 Answer
How can I move a corresponding object. 2 Answers