- Home /
Instantiating a pattern in front of the player?
I would like to instantiate a pattern of bullets in front of the player, a pattern like so:
[] []
[] []
[] []
My code for the shooting script is this so far:
public class HoldShoot : MonoBehaviour
{
public HoldBullet hB;
public Camera cam;
public Transform player;
public float spacing = 1f;
private float distance = 3.0f;
void Update ()
{
if (Input.GetButtonDown ("Fire1"))
{
for (int y = 0; y < 3f; y++)
{
for (int x = 0; x < 2f; x++)
{
Vector3 pos = new Vector3 (player.position.x+x, player.position.y+y, player.position.z) *spacing;
Instantiate (hB, pos +cam.transform.forward *distance, cam.transform.rotation);
}
}
}
}
}
My code for the actual bullet is here:
public class HoldBullet : MonoBehaviour
{
public float speed = 3.0f;
void Update ()
{
StartCoroutine (Example ());
}
IEnumerator Example()
{
yield return new WaitForSeconds (2f);
transform.position += transform.forward * speed * Time.deltaTime;
yield return new WaitForSeconds (5f);
Destroy (this.gameObject);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag.Equals("Player"))
{
Debug.Log ("player hit");
}
else
{
Destroy (this.gameObject);
}
}
}
so right now the Bullet instance spawns but only aligns to one axis, Bullets do not orientate themselves based on player's forward or camera orientation, and my question would be how would one accomplish this?
try using quaternion.identity rather than transform.rotation when instantiating. Also make sure your rotations are zeroed out in your bullet prefab.
sorry about the late reply, and thank you for commenting
unfortunately no dice. the bullet prefab was already zeroed out and changing the rotation has the cubes not turning at all
Try creating a new empty object and putting it at the end of your gun barrel (making sure it's facing the right direction). Then create a new variable that finds the empty object and use that as your spawn point and rotation when you instantiate. Also try giving your bullet prefab a rigid body and using AddForce rather than transform.position to propel it forward.
an update has surfaced!
i figured out the reason it was rotating strangely was because when i was facing the x axis, it wasn't correctly identifying that the x axis i was facing now became the z axis, and vice versa.
I added an if statement that sort of makes it work correctly (it can definitely be better)
if ((cam.transform.forward.x >= 0 && cam.transform.forward.z >= 0) || (cam.transform.forward.x < 0 && cam.transform.forward.z < 0)) {
pos = new Vector3 (player.position.x + x, player.position.y + y, player.position.z) * spacing;
} else {
pos = new Vector3 (player.position.x , player.position.y + y, player.position.z + x) * spacing;
}
Instantiate (hB, pos +cam.transform.forward *distance, cam.transform.rotation);
if anyone has any idea how to make it better, you'd be very greatly appreciated! for now I'm gonna try some stuff on my own and update this question for future references
bingo! this suited my needs nicely
if ($$anonymous$$athf.Abs(cam.transform.forward.z) < 0.5f) {
pos = new Vector3 (player.position.x , player.position.y + y, player.position.z+x) * spacing;
} else {
pos = new Vector3 (player.position.x+x, player.position.y + y, player.position.z ) * spacing;
}
Instantiate (hB, pos +cam.transform.forward *distance, cam.transform.rotation);
Still have no clue as to why it was behaving so strangely, but this put it to a "good enough" status at the very least
Your answer
Follow this Question
Related Questions
Instantiate rotation not working 1 Answer
Instantiated bullets hitting GameObjects? 4 Answers
How to check who shot whom 3 Answers
Enter the Gungeon/Touhou style Bullet Patterns? 0 Answers
Photon Network instantiation 0 Answers