unity-game-enginedistancedestroybullet

Unity destroy bullet after distance


I want to make an FPS in Unity and I came across weapon range. I want an assault rifle to have 100m range so when I shoot a bullet from it the bullet should destroy after 100m. How can I do that? I use raycasts for direction with hit.point (with max distance) to know where my bullet hit. I know there is the function Destroy(seconds) which destroys the object after a float number of seconds. How can I destroy a bullet not after some time, but after some distance? Thanks in advance for every answer! :)


Solution

  • its really easy:

    use Vector3.Distance between initial position and the position bullet (transform.position) to calculate the distance .

     `Vector3.Distance(initialposition, transform.position)`
    

    if > specified distance you destroy object

    you could use another function: (transform.position - initialposition).magnitude

    maybe better in performance if needed (i dunno)