/// <summary> /// This function fires a ray forward and compares the tag to check if target has been hit /// </summary> public void Fire() { //DebugText.AddDebugText("firing"); if (Physics.Raycast(gun.transform.position, gun.transform.forward, out RaycastHit hit)) { if (hit.transform.CompareTag("target")) { DebugText.AddDebugText("hit target"); target.targetHit = true; } } }
/// <summary> /// This function defines the actions the agent can take based on the action vector. /// </summary> /// /// <para> /// The bot can take 3 actions, move vertically, horizontally or fire. Currently rewards are not being /// given for firing, only aiming. /// /// Rewards are given as follows /// +100 -> lining up with the target /// +1 -> moving towards target /// -10 -> moving out of wall bounds /// -0.001 -> moving away from target /// -0.001 -> every frame /// -0.5 -> if too much time has passed, deduct points and reset /// </para> /// /// <param name="vectorAction">The vector containing the </param> public override void AgentAction(float[] vectorAction) { // Actions, size = 3 // Based on values, move the aim or fire float vertical = vectorAction[0]; float horizontal = vectorAction[1]; float fire = vectorAction[2]; // If automatic aiming is not enabled if (!gunComponent.perfectAim.perfectAimmer) { if (vertical > 0.5) { gunComponent.MoveGun(1); } else if (vertical < -0.5) { gunComponent.MoveGun(6); } if (horizontal > 0.5) { gunComponent.MoveGun(4); } if (horizontal < -0.5) { gunComponent.MoveGun(3); } if (fire > 0.9f) { gunComponent.Fire(); } } // Rewards // if correctly moving aim towards target if (aiming) { AddReward(0.1f); } else { AddReward(-1f); } if (timeUp) { DebugText.AddDebugText("done cause time"); timeUp = false; AddReward(-0.1f); Done(); } // Limit the aim to only wall if (Physics.Raycast(gunObject.transform.position, gunObject.transform.forward, out RaycastHit hit)) { if (hit.transform.tag != "target") { if (hit.transform.tag != "wall") { //DebugText.AddDebugText("going out of bounds"); AddReward(-100f); Done(); } } if (hit.transform.CompareTag("target")) { AddReward(50f); Done(); } } // encourage bot to finish the level quickly AddReward(-0.1f); //Debug.Log("Reward" + GetReward()); }