- Home /
Instantiating a object in a certain position..
Ok, well I have this really simple C# script and there is this one stupid litttle thing I can't figure out, yes I have search the web. But I could not find any answers..
using UnityEngine;
using System.Collections;
public class Terrain : MonoBehaviour {
public Transform[] trees;
public Transform user;
public Transform terrain;
private bool getTerrain = true;
private bool terrainComplete = false;
void Update(){
if(getTerrain == true)
{
Instantiate (terrain, transform.position, transform.rotation);
Instantiate (user , transform.position , transform.rotation);
getTerrain = false;
terrainComplete = true;
}
}
}
What I would like is that when the terrain is made, the user spawns. But I wanted it to be in the middle of the terrain, not the edge where the user just... fall off.
I have tried doing stuff like Vector3(50, 2, 50) but somehow, that does not work in C#. how else could I make the user spawn in the middle of the terrain. by the way, the terrain is positioned at 0, 1, 0.
Answer by aldonaletto · Sep 10, 2011 at 06:55 PM
You could use the terrain collider.bounds.center to define the user position:
... private float offset = 2.0f; // define the height the user will be spawned at
void Update(){ if(getTerrain == true) { Transform terr = Instantiate (terrain, transform.position, transform.rotation); Vector3 pos = terr.collider.bounds.center + offset*Vector3.up; Instantiate (user, pos, transform.rotation); ... Or even define the spawning point with Vector3, as you already tried (but place a new keyword in front of it):
void Update(){ if(getTerrain == true) { Transform terr = Instantiate (terrain, transform.position, transform.rotation); Instantiate (user, new Vector3(50, 2, 50), transform.rotation); ...
thanks, it worked! I never really used the word "new" before! that's why I never thought of that!