- Home /
Game Objects same transform.position value, sitting on different places
So in my game, I have a bunch of parent gameobjects which I'm trying to move via script, they are instantiated and start moving to their final position which is x: 0 y: -6. The issue is, every parent object which is being moved goes to a different place when I set their transform to y: -6. I added a screenshot so it gets clearer.
I'm not an expert but I believe that this is caused because these gameobjects have children, so it changes their transform positions in a way I can't understand, is there any way that I can access a "global" position and move the gameobjects towards it? My current script responsible for moving the bricks:
public class MoveBrickSc : MonoBehaviour
{
WavesScript waveSc;
GameObject finalPosObj;
Vector3 finalPos;
float speed;
// Start is called before the first frame update
void Start()
{
waveSc = GameObject.FindObjectOfType<WavesScript>();
finalPosObj = GameObject.Find("FinalPos");
finalPos = finalPosObj.gameObject.transform.position;
speed = waveSc.moveSpeed;
}
// Update is called once per frame
void Update()
{
transform.position = Vector3.MoveTowards (transform.position, finalPos, speed * Time.deltaTime);
}
}
Could you clarify what's going wrong? You're moving transform.position towards finalPos, it's not possible for your transform to get a different end position unless it's a child object, in which case it'd show local coordinates. I'm unable to replicate your result.
Answer by Bunny83 · Nov 25, 2019 at 12:21 AM
We don't see your gizmo editor setting. Make sure you have it set to "pivot" and not to "center". Keep in mind that the position of your gameobject is always relative to the pivot.
Also note that you grab the position of your "FinalPos" gameobject in Start. If the position changes afterwards it won't affect your position stored in the variable "finalPos".
We can't really say much more about your issue since a lot details are missing which only you can check in your project.
Sorry, I'm not sure where to check the gizmo editor setting you said. And I dont think that grabbing the position in Start should be a problem since FinalPos gameobject never moves during the game/ after Start() .
I'm talking about this one:
See this documentation page for additional reference
Answer by aminbenslimen00 · Nov 24, 2019 at 10:02 PM
use transform.localposition instead of transform.positon
let me know if that fix it
Transform.localPosition: "Description Position of the transform relative to the parent transform.
If the transform has no parent, it is the same as Transform.position."
I dont think that would work since I'm moving the parent object and not child.