Distance is always zero
My problem hits every enemy script I have, but for one specific script its fatal :D When I play it, it prints me, that distance is zero and setBurn sets to true. And I dont want that :D Does anyone know what the problem is and how to solve it?
GameObject player;
Rigidbody rb;
GameManager gameManager;
float distanceToSee, distanceToShoot, distance;
Vector3 direction;
float _speed;
bool setBurn = false;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
rb = this.gameObject.GetComponent<Rigidbody>();
distanceToShoot = 5; //UnityEngine.Random.Range(3,7);
distanceToSee = UnityEngine.Random.Range(20, 30);
gameManager = (GameManager)FindObjectOfType(typeof(GameManager));
}
// Update is called once per frame
void Update()
{
distance = Vector3.Distance(player.transform.position, this.transform.position);
direction = (player.transform.position - this.transform.position).normalized;
}
private void FixedUpdate()
{
if (distance < distanceToSee && distance > distanceToShoot && !setBurn)
{
Debug.Log("isnt set burn");
this.transform.LookAt(player.transform);
transform.position = Vector3.MoveTowards(this.transform.position, player.transform.position, 8 * Time.deltaTime);
}
if(distance < distanceToShoot)
{
Debug.Log(distance + " + " + distanceToShoot);
Debug.Log(distance < distanceToShoot);
if (!setBurn)
{
Burn();
}
}
if (setBurn)
{
rb.AddForce((direction - this.transform.position).normalized * Speed());
}
}
void Burn()
{
setBurn = true;
Debug.Log("setBurn is set to true");
}
float Speed()
{
_speed += _speed / 4;
return _speed;
}
Comment