I have a 2D platformer and am using C#. And I’m trying to shoot my bullet prefab out of my gun. So basically, when I shoot my gun the bullet spawns but doesn’t move, it gets stuck in mid-air. And second the bullet sprite wont flip to go in the direction I shoot it, it only spawns in one direction. So my entire script that goes on my bullet prefab doesn’t work. And one of the reasons is because I have a reference problem, I don’t have a bullet prefab in my inspector (when I did, the bullet would fly off, it kind of worked. But also the bullet would move in the direction I would move, if I shot right and then walked left, the bullet would change directions and travel left). So my reference to the script for the bullet I have on a script attached to my player says that its null because it cant find the prefab or whatever. I don’t know what to do, I’ve given up, I’ve tried everything… Please help.
SCRIPT ATTACHED TO PLAYER:
public class Glock18 : MonoBehaviour {
public float fireSpeed = 0f;
public float fireRate = 0f;
public float damage = 10f;
public LayerMask whatToHit;
public Transform firePoint;
public Transform bulletTrail;
private WeaponPickUp weaponPickUp;
private BulletTrail bulletScript;
void Awake()
{
weaponPickUp = transform.root.GetComponent<WeaponPickUp>();
bulletScript = transform.GetComponent<BulletTrail>();
}
void Update()
{
if (weaponPickUp.glockE == true)
{
if (fireRate == 1 && Input.GetButtonDown("Fire1") || Input.GetKeyDown(KeyCode.L))
{
Debug.Log("is bulletScript null? " + (bulletScript == null));
Shoot();
}
}
}
void Shoot()
{
Vector2 firePointPosition = new Vector2(firePoint.position.x, firePoint.position.y);
RaycastHit2D hit = Physics2D.Raycast(firePointPosition, firePointPosition, 10, whatToHit);
Effect();
}
void Effect()
{
if (bulletScript)
{
Debug.Log("Move");
bulletScript.GetComponent<BulletTrail>().Move();
}
Instantiate(bulletTrail, firePoint.position, firePoint.rotation);
}
}
SCRIPT ATTACHED TO BULLET:
public class BulletTrail : MonoBehaviour {
public int moveSpeed = 0;
public bool startingRight = true;
private PlayerController playerController;
void Awake()
{
playerController = GameObject.FindObjectOfType<PlayerController>();
}
void Start()
{
if (playerController.facingRight)
{
startingRight = true;
}
else if (!playerController.facingRight)
{
startingRight = false;
}
}
public void Move()
{
if (!playerController.facingRight)
{
MoveLeft();
}
else if (playerController.facingRight)
{
MoveRight();
}
}
void MoveLeft()
{
if (startingRight)
{
Debug.Log("FlipL");
Flip();
}
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(Time.deltaTime * moveSpeed * -50, GetComponent<Rigidbody2D>().velocity.y);
Destroy(gameObject, 1);
}
void MoveRight()
{
if (!startingRight)
{
Debug.Log("FlipR");
Flip();
}
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(Time.deltaTime * moveSpeed * 50, GetComponent<Rigidbody2D>().velocity.y);
Destroy(gameObject, 1);
}
void Flip()
{
startingRight = !startingRight;
Vector3 scale = transform.localScale;
scale.x *= -1;
transform.localScale = scale;
}
}