- Home /
I need help with a top down 2d shooter
I've followed this Brackeys tutorial to a tee: https://www.youtube.com/watch?v=LNLVOjbrQj4&t=1s
But when I shoot, the bullets don't appear on screen. They show up in the Hierarchy but not on screen. Would really appreciate the help! Here is my code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Shooting : MonoBehaviour { public Transform FirePoint; public GameObject bulletPrefab;
public float bulletForce = 20f;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
void Shoot()
{
GameObject bullet = Instantiate(bulletPrefab, FirePoint.position, FirePoint.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(FirePoint.up * bulletForce, ForceMode2D.Impulse);
}
}
Hi. Your instantiating code looks fine afaik. Does your bulletprefab have a model? Is the position correct/is it off screen?
Hi. The bullet can't be on screen at the beginning. If you quickly scroll through the link I put at the top (or go somewhere around the 10 $$anonymous$$ mark) you'll see that it has to be a prefab and then gets referenced in the code. I saw some people in the comments saying that this didn't work anymore, but saw no answers. Do you recon that unity has changed something?
Dont know about a change, can you upload your project somewhere and link it? I'll have a look at it tomorrow, that would probably be the easiest way
Answer by SebastianTarrant · Oct 08, 2020 at 05:07 PM
The problem was that the Bullet Prefab didn't have a sprite. So adding one fixed the whole problem. Hope this can help someone else!
Answer by l3mon · Oct 07, 2020 at 05:33 PM
When they are in the hierachy, they're there - which is the good news.
Simple way to check would be to select the object and press 'F' in the editor to focus on the object. Otherwise, you could also just check the transform in the editor or in the debugger when you instantiate.
Hi. Focused on the bullet and it's "there" on screen but just flies out of "view" of the main camera. Still can't see it though. Could you please elaborate your ideas? Thanks
The problem was that the Bullet Prefab didn't have a sprite. So adding one fixed the whole problem.
Answer by enestelli · Oct 07, 2020 at 08:03 PM
Could the up of your Firepoint point to the z-axis? Maybe the bullet you created moves in the z axis and is therefore we can't see it? Can you try new Vector3 (0,1,0) or shortly Vector3.up instead of FirePoint.up?
Hi. I wouldn't think so as the fire point is where the bullet originally spawns and is a game object in itself. Unless you think changing this would work anyway?
The problem was that the Bullet Prefab didn't have a sprite. So adding one fixed the whole problem.
Your answer
Follow this Question
Related Questions
How to make shooting in top down 2d shooter game? Unity 2d 1 Answer
Shooting a bullet object toward the mouse position in a 2D. 1 Answer
How do I stop a 2D projectile when it reaches a point 3 Answers
How to remove "lines" when using noise setting with cinemachine virtual camera 2 Answers
Target Lookat not working . I want Z axis to rotate only. 2 Answers