- Home /
How to get offset postition after rotation ? (Spent hours figuring it out)
Hi , I have a spawnPoint where I spawn gameobjects in columns with offset. I am able to spawn them with the right offset when the spawnPoint has no rotation. But run into trouble when the object has rotation. How to get the direction of offset when the spawnPoint has rotated. Please refer image and my code below. At it for hours:(
Answer by alonesaddev · Feb 05, 2021 at 10:17 PM
@gamezuv You probably tried to offset that using global positioning.
transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + offset);
I guess it was something like that. In that type of situation I recommend using position it like that:
transform.position = transform.forward * offset;
Answer by matthewroxby · Feb 05, 2021 at 10:03 PM
If your roads stay the same, try having an empty gameobject at every spawn point. You can then use the transform.forward of the empty to get your offset direction. Remember to have the empty's arrow pointing towards the direction of the road :)
Answer by unity_ek98vnTRplGj8Q · Feb 05, 2021 at 10:17 PM
@matthewroxby has a good suggestion, but if all you know is the offset you can calculate this more generically. Just rotate the offset vector by the rotation of the spawn object,
//Offset is the offset you would use to spawn on the unrotated spawn point
//For example, this could be (2, 0, 0)
SpawnObject(Transform parentObject, Vector3 offset, GameObject objectToSpawn){
Vector3 rotatedOffset = parentObject.rotation * offset;
Instantiate(objectToSpawn, parentObject.position + rotatedOffset);
}