Instantiate creates object at wrong position
Hey!
I got the following code:
using UnityEngine;
using System.Collections;
public class PlaceTower : MonoBehaviour {
public GameObject towerPrefab;
private GameObject tower;
void OnMouseUp () {
if (canPlaceTower ()) {
tower = (GameObject)
Instantiate(towerPrefab, transform.position, Quaternion.identity);
}
}
private bool canPlaceTower(){
return tower == null;
}
}
which is supposed to spawn a tower once you click on the white cube on the terrain: https://gyazo.com/aceb98fd0a1d79b62bcba6e48a7db4b6
Tower position: https://gyazo.com/23528d0b2a3ed889100eebc9dc10d3d9
The tower should be placed on the exact same position as the cube which it apparently is since the spawned tower has the same coordinates as the white cube but the tower is located somewhere else upp in the air.
Does someone know what the issue could be?
Answer by Frostblood · Apr 06, 2018 at 06:12 PM
I had the same problem and found out my prefab transform had a default position in it's transform which was causing the offset. You would think a prefab would always be at exactly where you spawn them but they actually add their transform position to the position you give them when instantiating. (To fix it just change the prefab transform position to 0,0,0).
Answer by ronny3050 · Jul 02, 2016 at 05:13 PM
The issue is your OpenSpot is located at y = 0 which is in the air, relative to your ground which has some negative y value. I suggest dragging the OpenSpot down until it hits the ground and then try spawning again.
Answer by yonoodle · Nov 12, 2016 at 08:37 AM
I just run into similar problem with wrong instantiate position , might not be the same issue as yours ,
I checked all hierachy are set to (0,0,0) and also all the animation begin with (0,0,0)
that solved my problem , which caused by editing the animation at non origin(0,0,0) position and the animator saved the displacement value
besure you click "apply change to Prefab" from "GameObject" menu after changing all position to 0,0,0
good luck!
Answer by shyxiaolong · Mar 15, 2021 at 09:06 AM
in my case i was initialise coordinates before use base class of instance. and and this led to different coordinates.maybe someone find here solve problem
Your answer
Follow this Question
Related Questions
How to move object after set it to certain position? [VR Rift] 0 Answers
How to make my Player smaller if he moves the y coordinate up? 1 Answer
X coordinates isn't the same on my gameobjects when on same position. 1 Answer
Getting position of a coordinate on the surface of a sphere 0 Answers
Converting mouse(touch) position with ScreenToWorldPoint doesn't work. 1 Answer