- Home /
Instantiating an object to the location of child of another object.
I have a number of different platform Prefabs, and each has two empty objects(PointA and PointB) as childs, which are located at each end of the platforms. I tried to Instantiate a platform to PointB of another platform, but it doesn't seem to instantiate at the correct location.Please forgive my ignorance, as I'm still new at this. Here's what my code looks like: public class PlatformSpawn : MonoBehaviour {
public Transform [] platforms;
public int platformNumbers = 0;
private Vector3 nextSpawnLocation;
private Transform currentPlatform;
private Transform nextPlatform;
void Start () {
if (platformNumbers < 3) {
Invoke ("Spawn",0f);
}
}
//Spawning Function
void Spawn () {
if (platformNumbers == 0) {
currentPlatform = platforms [Random.Range (0, platforms.Length)];
nextPlatform = platforms [Random.Range (0, platforms.Length)];
Instantiate (currentPlatform, new Vector3 (0f, 0f, 0f), Quaternion.identity);
platformNumbers++;
nextSpawnLocation = currentPlatform.FindChild ("PointB").transform.position;
Instantiate (nextPlatform, nextSpawnLocation + nextPlatform.transform.position - nextPlatform.FindChild("PointA").position, Quaternion.identity);
platformNumbers++;
print (nextSpawnLocation);
print (currentPlatform.FindChild ("PointB").transform.position);
}
}
}
Answer by Owen-Reynolds · Jun 18, 2015 at 03:36 PM
Looks like you don't know about localPosition, and the math is backwards?
This (edited to length) line in Instantiate:
nextSpawnLocation+nextPlatform.position-nextPlatform.FindChild("PointA").position
looks like you're trying to use PointA as an offset. "Spawn at nextLocation, but shifted by PointA." An easier way to get that is to use localPosition instead of position. Otherwise, the math seems backwards. Shouldn't it be pointA-platformPosition?
If things rotate, then the two are different. LocalPosition is in the parent's local coords, whereas child.pos-parent.pos is always in global coords. But only if parents are rotated, does this matter.
I'm also a little confused by "trying to Instantiate at point B." You can just use pointB.position directly. Even for children, position is the actual, world position. LocalPosition is the one that changes up if you're a child. But if that was what you really wanted, you wouldn't have used all that math?
Hi Owen, thank you for your answer. Yes, I am trying to use PointA as an offset. I have tried using localPosition to get the distance between the parent object and PointA (child) and add it to where I want to spawn the next platform. However, it seems like its giving the same results. I have also tried changing the math to pointA - platformPosition to no avail. One thing I found weird after trying to figure out the problem, is that when I look in the inspector for the position of the children in the platform object(parent), the positions are irrelevant to the parent's position, despite the description of the inspector saying its displaying the object's localPosition. I wonder if this explains why the platforms are not instantiating in the position I want.
Answer by Feiser · Jun 19, 2015 at 06:32 PM
After a bit of testing, I have found the answer. The reason why the platforms are instantiating at a spot different than what I intended was because I thought the parentObeject's position is the centre point of the parentObject and all its children (that's what it shows in the game scene when I select the parentObject). When actually, the position of the parentObject is basically the centre point of just the parentObject by itself (without the children). Forgive my naivety.
Your answer