- Home /
controlling rigidbody 2d
Hi,
I've tried a lot of ways without success, please help me!
I need to move and object that is falling from the top of the screen due to gravity Scale (it has a rigidbody2D and a boxcollider2d components attached).
I've created two GUI.Button , a left and a right arrow.
and the idea its very simple, when the user taps one of those , the object has to move -1 or 1 (depending on which one the user taps) in the x axis, so i can chose where the object will land.
as i said i've tried a lot! still don't know what am i doing wrong. I succeeded when trying with an object that hasn't any component attached, using :
myobject.gameObject.transform.Translate (-1,0,0); (for the left arrow)
but with it comes to objects affected by physics it does not work, but it compiles! so i've got no idea what do i have to change!
please help me!!
thanks, my code is this one:
function OnGUI () {
if (GUI.Button (new Rect (10,10,60,30), "boton")) {
var obj_to_move = falling_objs.chosen_obj.gameObject;
Debug.Log (obj_to_move.gameObject.name);
obj_to_move.gameObject.transform.position.x = falling_objs.chosen_pos;
Debug.Log (obj_to_move.gameObject.transform.position.x);
obj_to_move.gameObject.transform.Translate (-1,0,0) ;
}
}
please note that im using 3 variables, obj_to_move that its the gameObject that is falling due to gravity, and is the one i need to move.
the other 2 variables are static, defined in other script called falling_objs
chosen_obj its the object picked randomply from an array.
and chosen_pos its the position.x of this chosen_obj
i get the name of the chosen_obj each time i press the button, and also the obj_to_move.position.x that its = to chosen_pos.
this tell me that there is no problem locating the object that i want to move, BUT IT DOES NOT $$anonymous$$OVE AT ALL!
i've tried a lot, here i post this script cause it worked with an object that hasn't components attached, is the closest result that i've got.
Please help me, i've been trying for a looong time now.
BerggreenD$$anonymous$$, how can i get paid support then?
I'm not asking anyone to work, just to tell me what am i doing wrong, or which rode to take so i can read about it and then try to apply it.
thanks.