M
M
Mahmudi2021-09-24 14:30:49
Unity
Mahmudi, 2021-09-24 14:30:49

The == operator does not evaluate to true, so my projects fly indefinitely. How to fix?

public class Bullet : MonoBehaviour
{
    public float speed;
    Vector3 bulletEndPos;
   
    [SerializeField] Controller player;
    
    void Start()
    {
        player = GameObject.Find("Player").GetComponent<Controller>();
        
        bulletEndPos = transform.position + transform.forward * player.trailDistance;
        
    }
    
    void FixedUpdate()
    {        
        transform.Translate(Vector3.forward * speed * Time.deltaTime);

        if (transform.position == bulletEndPos)
        {
             Destroy(this.gameObject);
        }

    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
freeExec, 2021-09-24
@Mahmudi

It is necessary not to compare the exact coordinates, but the distance at which the bullet flew away.

S
SagePtr, 2021-09-24
@SagePtr

If you are comparing real numbers, they are unlikely to be equal due to errors in the calculations. In such cases, check that the difference between both numbers is close to zero, for example, < 0.000001 (the number of zeros - depending on the accuracy you need)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question