// Update is called once per frame void Update() { float distancePerFrame = speed * Time.deltaTime; RaycastHit hit; Ray ray = new Ray(transform.position, transform.forward); if (Physics.Raycast(ray, out hit, distancePerFrame, coverLogicLayerMask)) { BulletThroughCoverLogic cover = hit.transform.gameObject.GetComponent <BulletThroughCoverLogic>(); if (cover.CheckBullet(this)) { OnCollisionWithCover(cover.gameObject); } } if (Physics.Raycast(ray, out hit, distancePerFrame, physicalObstaclesLayerMask)) { GameObject obstacle = hit.transform.gameObject; if (obstacle.layer == soldiersLayer) { OnCollisionWithSoldier(obstacle); } if (obstacle.layer == wallsLayer) { OnCollisionWithWall(obstacle); } return; } transform.position += transform.forward * distancePerFrame; }
void Update() { foreach (Waypoint wp in waypoints) { wp.Reset(); if (wp.isNotReachable) { continue; } // check health packs { Ray ray = new Ray(wp.position + Vector3.up * 100, Vector3.down); wp.isHealthPack = Physics.Raycast(ray, float.MaxValue, healthPacksLayerMask); } // check cover { Vector3 origin = agentState.lastEnemyPosition; origin.y = 0.0f; float distanceToEnemy = Vector3.Distance(wp.position, origin); origin.y = 1.7f; // height of soldiers' heads Vector3 direction = wp.position - origin; direction.y = 0.0f; Ray ray = new Ray(origin, direction); float raycastDistance = distanceToEnemy; RaycastHit hit; do { if (Physics.Raycast(ray, out hit, raycastDistance, layerMask)) { int layer = hit.transform.gameObject.layer; if (layer == coverLayer) { BulletThroughCoverLogic coverLogic = hit.transform.gameObject.GetComponent <BulletThroughCoverLogic>(); if (!coverLogic.CheckIfPointInCover(agentState.lastEnemyPosition)) { wp.isBehindCover = true; } if (coverLogic.CheckIfPointInCover(wp.position)) { wp.isInCover = true; } // continue ray over cover Vector3 oldOrigin = ray.origin; ray.origin = hit.point + ray.direction.normalized * 0.1f; raycastDistance -= Vector3.Distance(ray.origin, oldOrigin); continue; } else if (layer == wallsLayer) { wp.isBehindWall = true; } } break; } while (true); } // check agent distances { wp.directDistanceToAgent = Vector3.Distance(wp.position, agentState.position); Waypoint agentWp = GetNearestWaypoint(agentState.position); if (agentWp.distancesToOtherWaypoints.ContainsKey(wp)) { wp.movementDistanceToAgent = agentWp.distancesToOtherWaypoints[wp]; } else { wp.movementDistanceToAgent = wp.directDistanceToAgent; } } // check enemy distances { wp.directDistanceToEnemy = Vector3.Distance(wp.position, agentState.lastEnemyPosition); Waypoint enemyWp = GetNearestWaypoint(agentState.lastEnemyPosition); if (enemyWp.distancesToOtherWaypoints.ContainsKey(wp)) { wp.movementDistanceToEnemy = enemyWp.distancesToOtherWaypoints[wp]; } else { wp.movementDistanceToEnemy = wp.directDistanceToEnemy; } } // check weight wp.weight = weightFunction(wp); } if (normalizeWeights) { NormalizeWeights(); } }