0
votes

I am making a simple 2d shooter in unity. I have made a prefab for my weapons and a script to shoot. When I enter the game mode I can see the bullet objects being created, but the bullets don't show on the screen. When I pause it and enter the scene mode they show (most of the time) any idea what could be causing this?? Here is the various settings on my bullet : Bullet Settings

Thanks in advance for your help

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Weapon : MonoBehaviour
{
    //variable to handle what progectile to spawn
    public GameObject projectile;
    //from where does the bullet coem from
    public Transform shotPoint;
    //How much time should be taken between shots
    public float timeBetweenShots;
    //time to shoot another bullet
    private float shotTime;



    // Update is called once per frame
   private void Update()
    {
        //Callculating the the current mouse position to the current weapon position
        //Input.mouse position returns a screenpoint
        Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
        float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
        Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
        transform.rotation = rotation;

        //if the left mouse button is pressed
        if(Input.GetMouseButton(0)){

            //if the current time in the game is greater than the shot time
            if (Time.time >= shotTime)
            {
                //shoot projectile
                Instantiate(projectile, shotPoint.position, transform.rotation);
                //recalculate shot time current time + time between shots
                shotTime = Time.time + timeBetweenShots;
            }

        }
    }
}

2
if the bullets dont show its usually a layer/order placement issueBugFinder
Hey, thanks for your help. I just tried to order it at 3 (the same as my gun) but it didn't do anything. My gun is the highest order at 3. The other various body parts are between 0 and 2.KeithNolo
The background is layer -100KeithNolo
What is the shorting layer(the Name of the layer) of the bullate?Ankit
Normally in 2d z is nearly always 0 ..for everything and you use layers and orders to put what where.BugFinder

2 Answers

1
votes

Basically you are creating bullets inside of the weapon thats why you are not able to see your bullets. I create simple scene and adding sphere as a bullet then I realized sphere spawning in the gun(for me in the cube). If you will adding force everything will be okay.

Assume the shootPoint is somewhere over the gun barrel,

 Instantiate(projectile, shotPoint.position, transform.rotation);

You should give them force,

GameObject projectile= Instantiate (prefab, position, rotation) as GameObject;
 rb = projectile.GetComponent<Rigidbody>();
 rb.AddForce (direction * speed);

Dont forget to add rigidbody to the bullet

1
votes

Check the order layer if you're by mistake not covering your bullets with the background image. Just make your bullet prefab layer higher then background equivalent.

Moreover, move the background node out of the scope of Player (currently you have it in the Player node), it should be equally set as your Main Camera and Player nodes in the tree. Then check the layering order and make sure order value in background is the lowest of all of your sprites.