The scene is that the player can automatically shoot enemies if close enough. The shooting will be lasted if the enemy in player's range. The issue is that every bullets just cloned but can't move.
Also, to some reason, i didn't add rigidbody for the bullet prefeb.
Here's my code:
if (Vector3.Distance (player.position, enemy.transform.position) < PLAYER_ATTACK_DISTANCE) {
if (Time.time > nextFire) {
nextFire = Time.time + fireRate;
GameObject b = Instantiate (bullet, transform.position, transform.rotation) as GameObject;
b.transform.position = Vector3.MoveTowards (transform.position, enemy.transform.position, Time.deltaTime * moveSpeed);
}
}
↧