I’m struggling to make my bullet prefab move forward at a constant velocity when spawned. This is for a top down 2D shooter. Using AddForce doesn’t have the desired effect. Here is my code:
PlayerShooting
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerShooting : MonoBehaviour
{
public Transform firePoint;
public GameObject shellPrefab;
public float shotDelay = 2f; //Delay bewteen shots
private float timestamp;
void Update()
{
if (Time.time >= timestamp && (Input.GetButtonDown("Fire1")))
{
Shoot();
timestamp = Time.time + shotDelay; //Shot delay is used here
}
}
void Shoot()
{
GameObject shell = Instantiate(shellPrefab, firePoint.position, firePoint.rotation);
Debug.Log("Shooting");
}
}
PlayerBullet
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerBullet : MonoBehaviour
{
public float bulletSpeed;
private Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
rb.velocity = new Vector2(bulletSpeed, bulletSpeed);
}
}
When spawned, the bullet doesn’t move at all. I’ve been struggling with this problem for weeks. I’m self taught, so please bear with me.