Instantiate in wrong position
I have a missile launcher and want to fire missile from is. The script I have for it is:
void Update () {
if(Input.GetKeyDown(KeyCode.Mouse0)) {
Transform start;
if (right) {
start = gameObject.transform.Find("FireRight");
} else {
start = gameObject.transform.Find("FireLeft");
}
Instantiate(prefab, start.position, start.rotation);
right = !right;
}
}
FireRight
and FireLeft
are empty objects for the position relative to the launcher.
The problem is that the missiles appear in wrong position. They appear a bit up and to the right where they should. The distance between the missiles is right, but the initial position is not, does anyone knows what could be the problem?
Are you sure the pivot of the prefab is correctly positionned ?
$$anonymous$$ore over, your code will be more efficient like this :
public Transform[] SpawnPoints ;
private int currentSpawnIndex = 0;
void Update () {
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0))
{
Instantiate(prefab, SpawnPoints[currentSpawnIndex].position, SpawnPoints[currentSpawnIndex].rotation);
currentSpawnIndex = (currentSpawnIndex + 1) % SpawnPoints.Length ;
}
}
In the Inspector, Drag & Drop the FireRight and FireLeft transforms in the array SpawnPoints
.
Your answer
Follow this Question
Related Questions
find whether the spawning space is free or not? 0 Answers
Placing a generated object in the center of a plane.. 1 Answer
Make Parent Follow Child and Vice Versa? 1 Answer
Object not where it is supposed to be 1 Answer
Navmesh point 0 Answers