Instantiating an object from main camera

Hi, when an enemy collides with a game object I want to instantiate a prefab from the main camera position and then move towards the target that has collided with it. How is the best way to do this is C sharp?

1 Answer

1

Use a variable to store the camera so when the enemy collides the gameObject you get the position.

cameraPosition = myCamera.transform.position;

Instantiate(prefab, cameraPosition, Quaternion.identity);

to move forward you have to use the localposition from the prefab. I don't know if this is going to work, but try something like this:

prefab.transform.localPosition += transform.forward * Time.deltaTime * 5;