Bullet not travelling anymore

https://youtube.com/shorts/pV-pbxUKNkQ?feature=share

Hi dipusta, welcome back. Are you asking for help solving why the bullet is not moving? If so, change the category of this topic to Ask. Having it in Show will not let people know you need help. Also, please provide more information on what the problem is. The script for moving the bullet and a screen shot of the inspector with the bullet selected would be a good starting point. What did you change right before the bullet stop working?

Hi. Thanks for responding and thanks for pointing this out. I’m new to the discussion forum.

The script for moving the bullet is as follows:

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

public class Bullet : MonoBehaviour
{
[SerializeField] float bulletSpeed = 20f;
Rigidbody2D myRigidbody;
PlayerMovement player;
float xSpeed;

void Start()
{
    myRigidbody = GetComponent<Rigidbody2D>();
    player = FindObjectOfType<PlayerMovement>();
    xSpeed = player.transform.localScale.x*bulletSpeed;
    
    
}

void Update()
{
    myRigidbody.velocity = new Vector2(xSpeed, 0f);

}

void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "Enemy")
    {
        Destroy(other.gameObject);
    }
    Destroy(gameObject);
}

void OnCollisionEnter2D(Collision2D other)
{
    Destroy(gameObject);
}

}

I have uploaded a video of the issue on here https://youtube.com/shorts/pV-pbxUKNkQ?feature=share.

screenshot:

Right before it stopped working, I added the following code.

void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "Enemy")
    {
        Destroy(other.gameObject);
    }
    Destroy(gameObject);
}

void OnCollisionEnter2D(Collision2D other)
{
    Destroy(gameObject);
}

}

Prior to it not working, this is what the bullet action looked like. https://youtube.com/shorts/QZFROCho3vs?feature=share

also the code for firing it is in my player movement script

void OnFire(InputValue value)
{
if (!isAlive) { return; }
Instantiate(bullet, gun.position, transform.rotation);
}

you are destroying the bullet when it it enters the collision on the trigger and also when it collides with anything .

make sure to set up proper collision layers so its not destroyed when it collides with something you dont want to destroy it, from the video it looks like it is being destroyed immedietly by the player collision

Privacy & Terms