protected virtual void FixedUpdate() { if (IsDead) { return; } steering = Vector3.zero; aceleration = Vector3.zero; if (!IsReachedTarget) { if (TargetEntity != null) { steering += steerBh.Seek(this, TargetEntity.Position) * SeekingWeight; } else { steering += steerBh.Seek(this, target) * SeekingWeight; } if (OnObsAvoidance) { neighbours = Owner.GetNeighbours(this); steering += flockBh.Separation(this, neighbours) * Separation; steering += flockBh.Alignment(this, neighbours) * Alignment; steering += flockBh.Cohesion(this, neighbours) * Cohesion; } } else { DetectEnemy(); } // obstacle avoidance test if (AgentRigid.velocity.sqrMagnitude > MinVelocity) { DetectBoxLenght = avoidanceBh.CalculateDetectBoxLenght(this); obstacles = StoredManager.GetObstacle(this); steering += avoidanceBh.GetObsAvoidanceForce(this, obstacles) * AvoidanceWeight; } aceleration = steering / AgentRigid.mass; Vector3 tempVel = TruncateVel(AgentRigid.velocity + aceleration); if (!float.IsNaN(tempVel.x) && !float.IsNaN(tempVel.y) && !float.IsNaN(tempVel.z)) { AgentRigid.velocity = tempVel; } RotateAgent(); if (!IsReachedTarget) { IsReachedTarget = CheckReachedTarget(); if (IsReachedTarget) { AgentRigid.velocity = Vector3.zero; } } }
private void FixedUpdate() { steering = Vector3.zero; neighbours = StoredManager.GetNeighbours(this); steering += steerBh.Seek(this, target.Position); // steering += steerBh.Arrive(this, target.Position); steering += flockBh.Separation(this, neighbours) * separation; steering += flockBh.Alignment(this, neighbours) * alignment; steering += flockBh.Cohesion(this, neighbours) * cohesion; rigid.velocity += steering / rigid.mass; transform.forward += rigid.velocity; #if UNITY_EDITOR Debug.Log("steer: " + steering + " velocity: " + rigid.velocity + " max speed: " + maxSpeed * rigid.velocity.normalized); #endif }