It looks like you're new here. If you want to get involved, click one of these buttons!
So im making this 2d infinite runner game that has an enemy which shoots at player.
this is the code im using for the bullet:
public class EnemyBullet : MonoBehaviour
public float speed = 20f;
public int damage = 40;
public GameObject impactEffect;
private Vector3 target;
private Transform player;
private Vector3 bulletTrans;
private Rigidbody2D rb;
// Use this for initialization
player = GameObject.FindGameObjectWithTag("Player").transform;
bulletTrans = new Vector3(transform.position.x, transform.position.y, transform.position.z);
rb = GetComponent<Rigidbody2D>();
target = new Vector3(player.position.x, player.position.y,player.position.z);
void OnTriggerEnter2D(Collider2D hitInfo)
PlayerStats playerhit = hitInfo.GetComponent<PlayerStats>();
if (playerhit != null)
private void FixedUpdate()
rb.velocity = (target - bulletTrans).normalized * speed;
Instantiate(impactEffect, transform.position, transform.rotation);
the problem that im having is that the speed of the bullet changes depending of the distance between the enemy and player.
i just want the bullet to go straight to the position where the player was in the time the bullet was instantiated.