Unity have a class for holding and changing value of a gameObject that is transform, by this class you can change position, rotation, and size of a gameObject. transform have three methods for changing position of a gameObject. Let's take a look at all three methods -
1. transform.Translate() Method
First method that transform class have is transform.Translate(). This method is useful when you want to move your gameObject to particular position so this method directly move your gameObject to given position.
By calling this method you have to enter x, y, and z coordinate in Translate method as seen above in code example transform.Translate(1,1,1); . as soon as we run our scene it will change the position of gameObject in which this script is attached.
alternatively you can also pass a vector in this method.
Changing position gameObject other than attached script
to change position of other gameObject, you can declare GameObject and then call this by using declared gameObject name adding dot "." and then method cube_object.transform.Translate(pos);
now drag and drop gameObject you want to move.
2. tansform.position
This method can change position of gameObject according to world position instead of local position means if gameObject is a child of other gameObject then its position will be according to world position.
You have assign a vector value with it.
if you want position of a gameObject then you can declare a vector and assign it by GameObject.transform.position it will return a vector value.
if you want to value only one axis x, y, or z then you can declare a float and assign it by GameObject.transform.position.x you can get value by change x to y, or z for getting your desired value.
You can also use this method for changing position at fixed value from it current position then you can use following code.
cube_object.transform.position+=new Vector3(1,0,0); this will increase position of x axis of gameObject by 1 unit. so += add value of x, y and z in its current position.
3. transform.localPosition
This method is very useful because it change position locally so if gameObject is a child object then it will change position according to its local transform instead of world transform.
You can also increase or decease position of gameObject from its current position to particular unit cube_object.transform.localPosition += pos;
For accessing current position in a vector or particular axis you can use following code
d/w transform.position and transform.localPosition
As above we have seen two methods one is transform.position and other is transform.localPosition both are same and work same but both have some differences.
There a two type of position coordinate in unity one is world position and other is local position.
World Position :- When you create a gameObject it have a default transform component attached to it. If gameObject is not a child object then it is its world postion.
Local Position :- Now we create an other gameObject gameObject and make it a child of any other object whose postion is not (0,0,0) then we notice that coordinate in tansform in inspector window are changed which respect to it parent object so now it is its local position.
so transform.position set coordinate according to world position which is sometime is our desired result but sometime not.
and transform.localPosition chnage coordinate according to it local position due to it parent gameObject.
Comments
Post a Comment