hi guys,I have a problem, basically I would like to take the global position of “Start_Point” but by taking it before instantiating the object i end up with the local position (in reality it would be the global one compared to the prefabs, but all are at 0,0,0 so the local one corresponds to the global one), so is there a way to take the global position with respect to the scene before instantiating the object?
lastObstacleSpawned = Instantiate(botton_Obstacles[i],
spawnPoint(botton_Obstacles[i].Find("Start_Point").transform),
Quaternion.identity);
private Vector3 spawnPoint(Transform starPoint)
{
Vector3 startGloabal = starPoint.position;
Debug.Log("StartPoint: " + startGloabal); // Result: (-7.35,-3.34) Same local position
Vector3 endPoint = lastObstacleSpawned.Find("End_Point").position;
Debug.Log("EndPoint: " + endPoint); //Result: (60,-1) Taken after initializing the object, corresponds to the global position
return endPoint;
}
All good, but my problem is another, I’ll try to explain myself better. I have to instantiate different obstacles, my idea was to have two points on each obstacle, one marking the end and one the beginning. I can easily assume the position of the end point of the previous obstacle relative to the one to be instantiated, but I cannot correctly assume the position of the starting point of the object to be instantiated. I was wondering if there was a way to take the position of a prefab object even before it is instantiated
Does the position of the prefab help you here at all?
I would just make the starting point the root game object of the prefab. Thus, you only just need to instantiate it at the end point of a piece. Then you can just have a custom component that references the transform of the end point.
My fault. I said that badly. The “End_Point” and the “Star_Point” are children of each prefabs called obstacle positioned along the x-axis, one at the beginning of the obstacle and one at the end. I need to instantiate the prefab obstacle only if the distance between the “Start_Point” of the obstacle to be instantiated and the “End_Point” of the previous instantiated obstacle must be a positive number, to do this I need to know the global position of the “Star_Point” which, however, the prefab to which it belongs must still be instantiated. I can get the global position of the “End_Point” because the previous obstacle is already initialized.
We understand your problem, and we have given you two solutions. One literal solution (from Kurt), and one way of approaching the problem from a different angle (from myself). Rewording your problem doesn’t change our advice.
Solved, sorry if I kept trying to explain myself better, but I don’t understand English well and I had already tried to do what you wrote before making the post, I simply got something wrong which led me to look for a more complicated solution than was actually needed. Thanks you for help me.