- Home /
I need to summon projectiles
I cant figure out how to make the prefab instantiate facing the player, then fire.
int randomO;
float randomY;
float randomX;
public GameObject projectilePrefab;
public Transform spawner;
public Transform player;
public float WaterSpeed;
private float time;
public float maxtime;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
randomO = Random.Range(1, 5);
switch(randomO)
{
case 1:
randomY = 62.9f;
randomX = Random.Range(-83.9f, 83.1f);
break;
case 2:
randomY = -66.8f;
randomX = Random.Range(-83.9f, 83.1f);
break;
case 3:
randomX = -107.9f;
randomY = Random.Range(48.7f, -58.7f);
break;
case 4:
randomX = 100.6f;
randomY = Random.Range(48.7f, -58.7f);
break;
}
transform.position = new Vector2(randomX, randomY);
if (time <= 0.0f)
{
//Heres where i want to instantiate the object.
time = maxtime;
}
}
private void FixedUpdate()
{
if (time >= 0.0f)
{
time -= Time.deltaTime;
}
}
Answer by stephen_gregory · Nov 18, 2020 at 06:59 PM
I didn't test that but I think you can rotate the prefab in the prefab mode then save then should work. Didn't test that but I think it should work.
Or if you want to change rotation via script maybe you can use
public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);
And set the rotation , something like this
Instantiate(cubePrefab, Vector3.zero, Quaternion.Euler(45, 0, 0));
Answer by Omti1990 · Nov 18, 2020 at 10:46 PM
I'd go with something like this:
if (time <= 0.0f)
{
//Heres where i want to instantiate the object.
GameObject myProjectile = Instantiate(projectilePrefab, spawner.position, spawner.rotation , spawner)
myProjectile.transform.LookAt(player.position);
time = maxtime;
}
You could probably also do this by replacing spawner.rotation with player.rotation and multiplying that with some Quaternion to turn the game object the way you want.
Your answer
![](https://koobas.hobune.stream/wayback/20220613020038im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The bullet only moves in one direction and does not rotate with object. Code: 1 Answer
(2D) Face Object in Direction of Travel 1 Answer
What is going on with Zxing Libary in portrait mode? 0 Answers
projectiles not correctly aiming in Unity2D 0 Answers
Is it better to use ParticleSystems on one GameObject OR instantiate ParticleSystems? 1 Answer