- Home /
GameObjects being instantiated on top of each other
I want my character to move and place cubes behind itself as it walks. When I step on a cube that already exists another one is instantiated on top of it. How do I go about changing this?
//#pragma strict
var endCube = gameObject;
var prefab = gameObject;
var collide : boolean = false;
function OnCollisionEnter(coll: Collision){ //Colliding with object?
if(coll.gameObject.tag=="floor"){
collide=true;
Debug.Log("collision On");
}
else{
collide=false;
Debug.Log("collision off");
}
}
function OnCollisionExit(coll: Collision){
collide=false;
Debug.Log("Stay");
}
function Update () {
myPosition = transform.position;
if(Input.GetButtonDown("up")){
transform.position.y = transform.position.y+1;
Instantiate(prefab, transform.position, Quaternion.identity);
}
if(Input.GetButtonDown("down")){
transform.position.y = transform.position.y-1;
Instantiate(prefab, transform.position, Quaternion.identity);
}
if(Input.GetButtonDown("left")){
transform.position.x = transform.position.x-1;
Instantiate(prefab, transform.position, Quaternion.identity);
}
if(Input.GetButtonDown("right")){
transform.position.x = transform.position.x+1;
Instantiate(prefab, transform.position, Quaternion.identity);
}
if (Input.GetKeyDown (KeyCode.Space)){
Instantiate(endCube, transform.position, Quaternion.identity);
}
Debug.Log(myPosition);
/* if (collide == false){
Instantiate(prefab, transform.position, Quaternion.identity);
Debug.Log("collide = false");*/
}
If each of your objects have trigger colliders, then you could set bool cubesNearby = true; during OnTriggerEnter/Stay/Exit. Altneratively, as you instantiate, record the objects in an array, then check through the array looking for the distance (Vector3.Distance) between the player and the cube - if any cube too close, then set a bool cubesNearby = true, that way.
Query the scene with a SphereCast and see if something already exists there before creating a cube.
Documentation links > CheckSphere, SphereCast, SphereCastAll
Answer by Serinx · Nov 13, 2014 at 02:36 AM
Similar to @richyrich's idea, you could have a list of Vector3's that store the positions of the cubes you have already placed:
private List<Vector3> BlockPositions = new List<Vector3>;
Before moving and placing a block you could check to see if the position already exists. If it doesn't then move and place the block and also add the new position to the list.
if(Input.GetButtonDown("up")){
transform.position.y+=1;
If (!BlockPositions.Exists(transform.position))
{
//Path is clear, move the player, instantiate a cube and add the position to the list
Instantiate(prefab, transform.position, Quaternion.identity);
BlockPositions.Add(transform.position);
}
else
{
//Path is not clear, move the player back
transform.position.y-=1;
}
}
Not in a position to test the code but you get the general idea.
Note: This method would require that your character/blocks are always in the exact position!
Good luck!
Answer by richyrich · Nov 13, 2014 at 10:39 AM
If (!BlockPositions.Exists(transform.position))
1) Assumes that each block is no more than 1 unit in height - maybe it is
2) Assumes that the player position starts from and can only exist in whole numbers rather than floats (Vector3 = float, float, float) this seems less likely
{
//Path is not clear, move the player back
transform.position.y-=1;
}
3) This now means that once you have laid one cube in a location, the character is frozen
Yes I did make those assumptions based on the fact that the character moves 1 unit when the button is pressed, moving the player back was just a roundabout way of not letting him move where blocks are already placed. If this assumption was incorrect then he can remove this part :)