- Home /
my object isnt instantiating in the right place
i need my campfire to spawn in front of the player but instead it spawns behind and really far away please help here is my script:
#pragma strict
var campfirePrefab : Transform;
var player : GameObject;
private var canBuild : boolean = true;
function Start()
{
renderer.material.color = Color.green;
renderer.material.color.a = 0.5;
}
function OnTriggerEnter(col : Collider)
{
if(col.gameObject.tag == "Terrain" || col.gameObject.tag == "Tree")
{
renderer.material.color = Color.red;
renderer.material.color.a = 0.5;
canBuild = false;
}
}
function OnTriggerExit (col : Collider)
{
if(col.gameObject.tag == "Terrain" || col.gameObject.tag == "Tree")
{
renderer.material.color = Color.green;
renderer.material.color.a = 0.5;
canBuild = true;
}
}
function Update()
{
if(Input.GetKeyDown("b") && canBuild == true)
{
Instantiate(campfirePrefab, player.transform.position + Vector3(0, 0, 2),Quaternion.Euler(270, 200, 200));
player.GetComponent(Crafting).campFire.SetActive(false);
}
}
Answer by J-R-Wood · Oct 25, 2014 at 10:34 PM
Just reading what you said up there i would suggest putting an empty gameobject where you want the fire then put te empty gameobject inside the player and put this script on the empty gameobject var Blood : GameObject; function Start () { Instantiate(Blood, transform.position, transform.rotation); } then just make your camp fire a prefab and put the camp fire prefab on the gameobject var for this script
what will happen is the fire will spawn/Instantiate where ever the empty gameobject is; considering you just put the empty gameobject under the player so that the player is the parent object the empty gameobject will follow the player, hence spawning is exactly in front of the player whenever the application starts, if you want to change when it spawns just throw in a if statement or change the function all together
PS. im karma point poor, to help with my huge ego or maybe narcissism please up vote my answer
Answer by robertbu · Oct 25, 2014 at 08:44 PM
Assuming the you've authored your character correctly so that the side facing positive 'z' when the rotation is (0,0,0) is the front, you can do:
Instantiate(campfirePrefab, player.transform.position + player.transform.forward * 2.0, Quaternion.Euler(270, 200, 200));
'transform.forward' is the local forward of an object transformed into world space.
i get errors from this like insert a semicolon but i have