Hi, I am trying to fire a projectile that should move in a direction until it gets destroyed depending on mouse position.
The following code works but the bullet stops at where I clicked, how can I make this bullet continue on its path until destroyed?
public class BulletStats : MonoBehaviour
{
public int Damage = 1; // Damage inflicted
public bool isEnemyShot = false; // Projectile from player or enemies?
public Camera camera; // camera object
private float speed = 5f; // bullet speed
private Vector2 clickPos; // bullet direction
void Start()
{
camera = (Camera) GameObject.FindObjectOfType(typeof(Camera)); // get the camera
// get world location of click
clickPos = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y));
Destroy(gameObject, 20); // give bullet 20 seconds lifetime
}
void Update()
{
// move the bullet in the right direction
transform.position = Vector2.MoveTowards(transform.position, clickPos, speed * Time.deltaTime);
}
}