- Home /
Spawning prefab on x axis inside the camera
Hi I want to spawn my prefab on a random x axis point inside the camera. But my problem is, only half of the prefab is inside the camera. Here are some photos you can understand. My camera is vertical by the way
Also my code:
public GameObject enemyObject;
float spawntime = 2f;
// Use this for initialization
void Start () {
Invoke ("spawnEnemy", spawntime);
}
// Update is called once per frame
void Update () {
}
void spawnEnemy()
{
Vector2 min = Camera.main.ViewportToWorldPoint (new Vector2 (1,1));
Vector2 max = Camera.main.ViewportToWorldPoint (new Vector2 (0,0));
GameObject enemy = (GameObject)Instantiate (enemyObject);
enemy.transform.position = new Vector2 (Random.Range (min.x, max .x), max.y);
}
}
I could not find a solution on the internet and I really don't know what to do. Please help
,
Answer by Neamtzu · Feb 12, 2018 at 04:06 PM
The problem is that the object pivot is in the center the object. You can try to move the pivot, to be at the bottom of the sprite or you can add half of the object size to the object position.
Answer by erenyelken · Feb 13, 2018 at 01:44 PM
How can I change the pivot point? I searched about changing pivot point. Most suggested way is to make the gameobject as child of another empty gameobject. But mine is a prefab which spawns. Also it did not work when I tried to assign a parent to prefab. Where can I change it? @Neamtzu