- Home /
why do my instantiated prefabs spawn depending on the position / rotation of the globe?
i have a globe which spins by its self and when the player clicks and drags the globe they can drag it into what ever position the wish. i have it where when a specific button is pressed, buildings are instantiated onto the globe but they dont always spawn in the right orientation on the globe i.e. on their side, upside down etc. i have a feeling its to do with the position of the camera but im not too sure! how do i fix this? i am a bit of a beginner so i am unsure where i have gone wrong.
here is my rotate script where the player can click and drag the globe
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rotateEarth : MonoBehaviour
{
Vector3 mPrevPos = Vector3.zero;
Vector3 mPosDelta = Vector3.zero;
public EarthSpinScript earthSpin;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
mPosDelta = Input.mousePosition - mPrevPos;
if (Vector3.Dot(transform.up, Vector3.up) >= 0)
{
transform.Rotate(transform.up, -Vector3.Dot(mPosDelta, Camera.main.transform.right), Space.World);
}
else
{
transform.Rotate(transform.up, Vector3.Dot(mPosDelta, Camera.main.transform.right), Space.World);
}
transform.Rotate(Camera.main.transform.right, Vector3.Dot(mPosDelta, Camera.main.transform.up), Space.World);
}
mPrevPos = Input.mousePosition;
earthSpin.spinEarth = false;
earthSpin.spinEarth = true;
}
}
here is my instantiate script
public class Spawner : MonoBehaviour
{
public Transform[] spawnLocations;
public GameObject[] WhatToSpawnPrefab;
public GameObject[] WhatToSpawnClone;
public bool alreadyInstantiated;
public void Update()
{
SpawnBuilding();
void SpawnBuilding()
{
if (Input.GetKeyDown("1") && !alreadyInstantiated)
{
alreadyInstantiated = true;
GameObject go = Instantiate(WhatToSpawnPrefab[0], spawnLocations[0].transform.position, Quaternion.Euler(85, 90, 360)) as GameObject;
go.transform.parent = GameObject.Find("Globe").transform;
}
else
{
alreadyInstantiated = false;
}
}
}
}
and here is my earth spinning script
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class EarthSpinScript : MonoBehaviour
{
public float speed = 10f;
public bool spinEarth = false;
//globe spins
void Update()
{
if (spinEarth)
{
transform.Rotate(Vector3.up, speed * Time.deltaTime, Space.World);
}
}
public void StopSpinning()
{
spinEarth = false;
}
public void StartSpinning()
{
spinEarth = true;
}
}
hopefully you can spot something in my scripts because I'm puzzled!
thank you in advance!
Answer by Magso · Jul 10, 2019 at 09:58 PM
Where did you get Quaternion.Euler(85, 90, 360)
from? This needs to be set to a rotation based on the camera or the spawnLocation. If the spawnLocation is at the correct rotation use
spawnLocations[0].transform.rotation
or if you want to use the cameras rotation, allow for the angle offset the camera might have e.g
Quaternion.Euler(cameraObject.transform.eulerAngles + new Vector3(-15, 0, 0))
Your answer
Follow this Question
Related Questions
How to make objects spawn at the same time without colliding ? 1 Answer
[SOLVED]: Bullets passing through platforms - Brackeys Tutorial 1 Answer
Instantiate not working? 0 Answers
How to instantiate a sprite after it has been destroyed? 2 Answers
how do I make it so my building prefab instantiates only once 2 Answers