Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
(1 edit)

I understand what you said a little bit, but I'm having trouble trying to implement it. I moved the shooting stuff and now the bullets just spawn and sit. From the tips you gave it sounds like I'm supposed to call something in the Bullet script, but I don't know what. I decided to use the old input system for getting keys pressed because the new one is just giving me trouble. My player is the one shooting the bullets, here is the code that I have for the Player script

using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
    [SerializeField] float runspeed = 10f;
    [SerializeField] GameObject bullet;
    [SerializeField] Transform gun;
    [SerializeField] float bulletSpeed = 10f;
    [SerializeField] float fireDelay;
    private float lastFire;
    
    Vector2 moveInput;
    Rigidbody2D myRigidbody;
    Rigidbody2D bulletRigidBody;
    bool isAlive = true;
    void Start()
    {
        myRigidbody = GetComponent<Rigidbody2D>();
    }
    void Update()
    {
        if(!isAlive) { return; }
        Run();
        if(Time.time > lastFire + fireDelay) { return; }
        Fire();
    }
    void OnMove(InputValue value)
    {
        moveInput = value.Get<Vector2>();
    }
    void Run()
    {
        Vector2 playerVelocity = new Vector2 (moveInput.x * runspeed, moveInput.y * runspeed);
        myRigidbody.velocity = playerVelocity;
    }
    void OnShoot(InputValue value)
    {
        if(!isAlive) { return; }
        Instantiate(bullet, gun.position, transform.rotation);
    }
    void Fire()
    {
        if (Input.GetKey(KeyCode.UpArrow)) 
        {
            Shoot(Vector2.up);
            lastFire = Time.time;
        }
        else if (Input.GetKey(KeyCode.RightArrow)) 
        {
            Shoot(Vector2.right);
            lastFire = Time.time;
        } 
        else if (Input.GetKey(KeyCode.DownArrow)) 
        {
            Shoot(Vector2.down);
            lastFire = Time.time;
        }
        else if (Input.GetKey(KeyCode.LeftArrow)) 
        {
            Shoot(Vector2.left);
            lastFire = Time.time;
        }
    }
    void Shoot(Vector2 direction)
    {
        bulletRigidBody.velocity = new Vector2(direction.x * bulletSpeed, direction.y * bulletSpeed);
    }
}
// the text after this comment is the code in my Bullet script
using UnityEngine;
public class Bullet : MonoBehaviour 
{     
    private void OnTriggerEnter2D(Collider2D other)      
    {         
        if(other.tag == "Enemy")         
        {             
            Destroy(other.gameObject);         
        }         
        Destroy(gameObject);     
    }      
    private void OnCollisionEnter2D(Collision2D other)      
    {         
        Destroy(gameObject);      
    }
}
(+1)

Unless I'm overlooking something you haven't assigned a value to bulletRigidbody so that's why it isn't doing anything. Without testing it myself you need something like:

GameObject bullet = Instantiate(//instantiate code)

Rigidbody2D bulletRigidbody = bullet.GetComponent<Rigidbody2D>()

And then set its velocity.