- Home /
Multiple object prefab instantiates too far down
Hi. I have a prefab with 12 cubes that all have box colliders and the prefab also have a box collider. I also have a script which instantiates objects where the mouse-click happened. Now, if I instantiates the first prefab which is the one with the 12 cubes it instantiates it far down in the terrain, although the terrain have a terrain collider. When I pick my 2nd or 3d prefab which is only has one object it works. It instantiates on the terrain. I've also checked in the log if it's something wrong with my mouse input, but it's the same for all objects.
Script:
using UnityEngine;
using System.Collections;
public class placeObject : MonoBehaviour {
public Player player;
public Transform Object1;
public Transform Object2;
public Transform Object3;
public Camera Eye;
public int chosenObject = 1;
public int ChosenObject { get{ return chosenObject; } set{ chosenObject=value; } }
// Use this for initialization
void Start () {
//GameObject Eye = GameObject.Find("Camera");
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown("1")){
Debug.Log("Key 1 pressed, outputs small village");
chosenObject = 1;
}
else if(Input.GetKeyDown("2")){
Debug.Log("Key 2 pressed, outputs Cylinder");
chosenObject = 2;
}
else if(Input.GetKeyDown("3")){
Debug.Log("Key 3 pressed, outputs Sphere");
chosenObject = 3;
}
}
void OnMouseUp ()
{
Ray ray = Eye.camera.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 2000))
{
if(chosenObject == 1){
if(player.Money > 100){
player.Money -= 100;
//player.Population += 30;
Debug.Log("Chosen object is 1 and will output a small village. Merry christmas!");
Debug.Log (hit.point);
Instantiate(Object1, hit.point, Quaternion.identity);
}
else {
Debug.Log ("Too poor");
}
}
else if(chosenObject == 2){
if(player.Money > 200){
player.Money -= 200;
Debug.Log("Chosen object is 2 and will output a cylinder. Merry christmas!");
Debug.Log (hit.point);
Instantiate(Object2, hit.point, Quaternion.identity);
}
else {
Debug.Log ("Too poor");
}
}
else if(chosenObject == 3){
if(player.Money > 300){
player.Money -= 300;
Debug.Log("Chosen object is 3 and will output a sphere. Merry christmas!");
Instantiate(Object3, hit.point, Quaternion.identity);
}
else {
Debug.Log ("Too poor");
}
}
else {
}
Debug.Log("Prefab cloned!");
}
}
}
How do I make my prefab instantiate on top of the terrain instead of under it? It looks like it is a evenly space between because if I instantiate it a little bit under it instantiates a bit more down than it was on the higher point.
Thank you.
It outputs the same coords as the one that works, so yes.
It which case, it sounds like the centre point of your prefab is off. Drag the prefab onto the stage - where does the movement control appear?