- Home /
How to get an object's transform and move an object to that position?
PLEASE MAKE ALL SCRIPTS IN C-SHARP!!
Hello people of Unity! I am trying to create a FPS, and I need your help! I have a bullet and a bullet spawn point ready to go. The bulletSpawn is the parent object for the bullet, and I need a script (for the bullet) to access the bulletSpawn, and get the position, then move the bullet to the bulletSpawn's position.
The bulletSpawn is the parent. The bullet is a simple mesh sphere.
if the bullet is a child of the spawn point then just set it's transform to Vector3(0, 0, 0)
Child transforms are relative to the parent transform.
Answer by aldonaletto · Jun 26, 2017 at 11:38 PM
If the bulletSpawn is already the parent of the bullet, simply get a reference to it in Start (bullet script):
Transform daddy;
void Start(){
daddy = transform.parent;
}
But, if there's no previous connection between the bullet and the bulletSpawn object, you can create a field in the bullet script and drag the bulletSpawn on it in the Inspector, then save the bullet prefab:
public Transform daddy; // drag here the bulletSpawn object
Anyway, move the bullet to the parent position with:
transform.position = daddy.position;
Another alternative would be to set the bullet's localPosition to (0,0,0), as suggested by @jwizard93 in his comment.
transform.localPosition = Vector3.zero;
Your answer
