Instantiate without camera following
Hello! I'm trying to Instantiate a prefab while the player is moving. The instantiating is working at any position but the clone prefab follows the camera and doesn't stay on the position. I tried debugging it by checking the transform.position and it doesn't change so I think it's because it's attached to the screen itself and not to the game(I hope I'm making any sense). So here is my code:
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
Touch touch = Input.GetTouch(0);
Vector2 touchPos = Camera.main.ScreenToWorldPoint(touch.position);
if (touch.position.y > 280 && touch.position.y < Screen.height - 290)
{
Instantiate(hole, touchPos, Quaternion.identity);
}
}
I think maybe it's because of the ScreenToWorldPoint?
Answer by eratchet · Sep 09, 2020 at 04:12 PM
If i understand everything, you are trying to instantiate something by touching the screen, u want the new object to be where we touched(on the world). First as u said mayb the Vector2 is causing the problem cause it need a vector3 (public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);) But I thought unity could convert a vector2 to a vector3 (z is set to 0 for example ?) Does the new obj is the child of your cam ? try to set his parent to null Or what u can do is use :
GameObject Object = Instantiate(Object original, Transform parent, bool instantiateInWorldSpace); with instantiateInWorldSpace to true, and use Object.transform.position to set it in place. Hope this helped ^^
Sadly, my gameObject isn't the child of my cam. it instantiates outside of it.
Do u have a Screen to show it ? Does instantiateInWorldSpace of instatiate helped ? Never tested it but mayb mark the gameObject as static so it does not move, and if something is trying to make it move while it is static, it can cause an error somewhere in console so u can watch it
Hello! thank you for your time I was able to make it work by just attaching the moving script to the spawned object since I think no matter what I do the spawned object must go with the flow with the background because that's whats moving. Im really bad at explaining but that's it thank you again!