void OnTriggerEnter(Collider other) { if (destroyed == false) { ShotSphere shot = other.attachedRigidbody.GetComponent <ShotSphere>(); // did the rock get hit with a shot if (shot) { // does the shot have the shooter set so we can reward them if (shot.shooter) { // did a player shoot us, give them some points RocketSphere rocketPlayer = shot.shooter.GetComponent <RocketSphere>(); if (rocketPlayer) { rocketPlayer.points += points; } // did an AI shoot us, give them some points RocketSphereAI rocketAI = shot.shooter.GetComponent <RocketSphereAI>(); if (rocketAI) { rocketAI.points += points; } } } GameObject explosion = Instantiate(explosionPrefab, rock.transform.position, Quaternion.identity) as GameObject; NetworkServer.Spawn(explosion); destroyedRocks++; currentRocks--; // Debug.Log("OntriggerEnter Rocks " + currentRocks + " total " + totalRocks + " destoyed " + destroyedRocks); // let all the clients know the current rock counts for music // IMPORTANT!!!!, this must be called before the detroy or the rpc will never go out. rpcSetRockStats(currentRocks, totalRocks, destroyedRocks); destroyed = true; NetworkServer.Destroy(transform.gameObject); if (rockSpherePrefab) { for (int i = 0; i < pieces; i++) { GameObject newRock = Instantiate(rockSpherePrefab, rock.transform.position, rock.transform.rotation) as GameObject; NetworkServer.Spawn(newRock); } } } }
void OnTriggerEnter(Collider other) { // Rocket is already destroyed, ignore if (!rocket.activeSelf) { return; } ShotSphere shot = other.attachedRigidbody.GetComponent <ShotSphere>(); // did a shot hit us if (shot) { if (shot.shooter) { if (shot.shooter == transform.gameObject) { // ignore getting hit with our own shots return; } // did we get shot by another player, give them some points RocketSphere rocketPlayer = shot.shooter.GetComponent <RocketSphere>(); if (rocketPlayer) { rocketPlayer.points++; } // did we get shot by an AI player, give them some points RocketSphereAI rocketAI = shot.shooter.GetComponent <RocketSphereAI>(); if (rocketAI) { rocketAI.points++; } } } // Spawn an explosion at player position on all clients GameObject explosion = Instantiate(explosionPrefab, rocket.transform.position, Quaternion.identity) as GameObject; NetworkServer.Spawn(explosion); // deactivate the ship on all clients RpcMySetActive(false, Quaternion.identity, false); // spawn a new ship, do this on the local player client and it will re-enable on all clients rpcSpawnShipDelay(); }
void OnTriggerEnter(Collider other) { ShotSphere shot = other.attachedRigidbody.GetComponent <ShotSphere>(); // did a shot hit us if (shot) { // make sure the shooter still exists if (shot.shooter) { // ignore our own shots if (shot.shooter == transform.gameObject) { return; } // did a player shoot us, give them some points RocketSphere playerShooter = shot.shooter.GetComponent <RocketSphere>(); if (playerShooter) { playerShooter.points += 2; } // did a AI shoot us, give them some points RocketSphereAI rocketShooterAI = shot.shooter.GetComponent <RocketSphereAI>(); if (rocketShooterAI) { rocketShooterAI.points += 2; } } } // Spawn an explosion at rocket position on all clients GameObject explosion = Instantiate(explosionPrefab, rocket.transform.position, Quaternion.identity) as GameObject; NetworkServer.Spawn(explosion); // let the AI know we finished transform.gameObject.GetComponent <RocketAIAgent>().EpisodeEndBad(); // destory the ship on all clients destroyed = true; NetworkServer.Destroy(transform.gameObject); }
public override void OnEpisodeBegin() { base.OnEpisodeBegin(); // set the team ID to random so AI's don't avoid shooting each other to avoid a team loss, // not sure if this can be done in the other function or not so I'll put in both places just to be sure GetComponent <BehaviorParameters>().TeamId = (int)Random.Range(0.0f, 100.0f); raySensor = transform.gameObject.GetComponentInChildren <RayPerceptionSensorComponent3D>(); rocket = transform.gameObject.GetComponent <RocketSphereAI>(); rb = transform.gameObject.GetComponent <Rigidbody>(); if (rocket) { lastPoints = rocket.points; } else { lastPoints = 0; } }
public override void OnActionReceived(ActionBuffers actionBuffers) { float reward = 0.0f; base.OnActionReceived(actionBuffers); RocketSphereAI rocket = transform.gameObject.GetComponent <RocketSphereAI>(); if (rocket) { // I tried give little rewards for things I wanted the AI to do but ended up // removing these as they did not help and could result in an AI doing things just // for the little rewards rocket.horizontalInput = Mathf.Clamp(actionBuffers.ContinuousActions[0], -1f, 1f); // little reward turning //SetReward(0.001f * Mathf.Abs(rocket.horizontalInput)); rocket.verticalInput = Mathf.Clamp(actionBuffers.ContinuousActions[1], 0f, 1f); // little reward thrusting //SetReward(0.001f * rocket.verticalInput); if (actionBuffers.ContinuousActions[2] > 0.0f) { rocket.fireInput = true; } else { rocket.fireInput = false; } // this is the only reward that matters, and in fact I just want to indicate a hit for 1.0 and // NOT scale this reward depending on what was hit. if (rocket.points > lastPoints) { // big reward if we hit something reward = 1.0f; // 3.0f * (rocket.points - lastPoints); SetReward(reward); Debug.Log("Reward for points " /* + rocket.points + " last points " + lastPoints + " reward " */ + reward); lastPoints = rocket.points; } // More little rewards that weren't really helpful /* * if (rocket.fireInput) * { * // little reward for shooting * reward = 0.01f; * SetReward(reward); * //Debug.Log("Reward for shooting " + reward); * } * else * { * // little negative reward for not shooting * reward = -0.001f; * SetReward(reward); * //Debug.Log("little negative reward for not shooting " + reward); * } */ } // I tried to keep the AI from sitting and spinning which was an aretifact of feeding the AI // too much info above. This seemed to result in depressed AI that would sit and spin even when I // made this negative reward grow exponentially. Negative reward that was consitant seems to be // desired by the AI than trying to figure out too much input and not getting enough positive rewards /* * if (rocket.countRotations != lastCountRotations) * { * // negative reward for excessive rotations * reward = -0.01f * math.abs(rocket.countRotations); * SetReward(reward); * if (reward < -0.1f) * { * //Debug.Log("Negative reward for excessive rotations " + rocket.countRotations + " reward " + reward); * } * * lastCountRotations = rocket.countRotations; * } */ // This reward resulted in slower and more controllable rockets but is not adventagous when // you need to move faster to get out of the way or find a new rock when there are not many left /* * if (rb) * { * if (rb.angularVelocity.magnitude < 0.02f) * { * // negative reward for not moving * reward = -0.01f * (0.02f - rb.angularVelocity.magnitude); * SetReward(reward); * //Debug.Log("Negative Reward for not moving " + rb.angularVelocity.magnitude + " reward " + reward); * } * * if ((rb.angularVelocity.magnitude >= 0.02f) && (rb.angularVelocity.magnitude <= 0.5f)) * { * // reward for moving * reward = 0.01f * (rb.angularVelocity.magnitude - 0.02f); * SetReward(reward); * //Debug.Log("Reward for moving " + rb.angularVelocity.magnitude + " reward " + reward); * } * * if (rb.angularVelocity.magnitude > 0.5f) * { * // negative reward for moving too fast * reward = 0.01f * 2.0f * (2.0f * 0.5f - rb.angularVelocity.magnitude); * if (reward > 0.0f) * { * //Debug.Log("Decreasing reward for moving " + rb.angularVelocity.magnitude + " reward " + reward); * } * else * { * //Debug.Log("Increasing negative Reward for moving too fast " + rb.angularVelocity.magnitude + " reward " + reward); * } * SetReward(reward); * } * } */ // This set of rewards was actually my first glimpse of something that looked resonable but // ultimately removing all this resulted in an even more impressive AI if (raySensor) { if (raySensor.RaySensor != null) { if (raySensor.RaySensor.RayPerceptionOutput != null) { if (raySensor.RaySensor.RayPerceptionOutput.RayOutputs != null) { if (raySensor.RaySensor.RayPerceptionOutput.RayOutputs.Length > 0) { /* * if (raySensor.DetectableTags.Count > 0) * { * // reward being near something * reward = 0.001f * raySensor.DetectableTags.Count; * SetReward(reward); * //Debug.Log("Reward for being near something " + raySensor.DetectableTags.Count + " reward " + reward); * } */ /* * if (raySensor.RaySensor.RayPerceptionOutput.RayOutputs[8].HasHit) * { * if (rocket.fireInput) * { * // reward pointing the ship right at something and shooting * reward = 0.2f; * SetReward(reward); * //Debug.Log("reward pointing the ship right at something and shooting " + reward); * } * else * { * // negative reward for pointing the ship right at something and not shooting * reward = -0.05f; * SetReward(reward); * //Debug.Log("negative reward for pointing the ship right at something and not shooting " + reward); * } * } * else if (raySensor.RaySensor.RayPerceptionOutput.RayOutputs[6].HasHit || raySensor.RaySensor.RayPerceptionOutput.RayOutputs[10].HasHit) * { * if (rocket.fireInput) * { * // reward pointing the ship at something and shooting * reward = 0.1f; * SetReward(reward); * //Debug.Log("reward pointing the ship at something and shooting " + reward); * } * else * { * // negative reward for pointing the ship at something and not shooting * reward = -0.025f; * SetReward(reward); * //Debug.Log("negative reward for pointing the ship at something and not shooting " + reward); * } * } * * if (raySensor.RaySensor.RayPerceptionOutput.RayOutputs[7].HasHit || raySensor.RaySensor.RayPerceptionOutput.RayOutputs[9].HasHit || raySensor.RaySensor.RayPerceptionOutput.RayOutputs[11].HasHit) ||{ || if (rocket.verticalInput > 0.2f) || { || // reward for thrusting away from something || reward = 0.01f; || SetReward(reward); || //Debug.Log("reward thrusting away from something " + reward); || } || else || { || // negative reward for not thrusting away from something || reward = -0.01f; || SetReward(reward); || //Debug.Log("reward thrusting away from something " + reward); || } ||} */ // this reward was not as good as I had hoped and did not use it much /* * if (raySensor.RaySensor.RayPerceptionOutput.RayOutputs[0].HasHit || raySensor.RaySensor.RayPerceptionOutput.RayOutputs[1].HasHit || raySensor.RaySensor.RayPerceptionOutput.RayOutputs[2].HasHit || raySensor.RaySensor.RayPerceptionOutput.RayOutputs[3].HasHit || raySensor.RaySensor.RayPerceptionOutput.RayOutputs[13].HasHit || raySensor.RaySensor.RayPerceptionOutput.RayOutputs[14].HasHit || raySensor.RaySensor.RayPerceptionOutput.RayOutputs[15].HasHit || raySensor.RaySensor.RayPerceptionOutput.RayOutputs[16].HasHit) ||{ || if (rocket.horizontalInput > 0.2f) || { || // reward turning either away from or towards something on the side || reward = 0.01f; || SetReward(reward); || //Debug.Log("reward turning either away from or towards something " + reward); || } || else || { || // negative reward for not turning when something is on the side || reward = -0.01f; || SetReward(reward); || //Debug.Log("negative reward for not turning when something is on the side " + reward); || } ||} */ } } } } } }