void OnPlayerEntityDeath(Entity source, Dictionary <Entity, int> culprits) { // Find current player entity in playerVehiclePrefabs. // Obtain the name of the current player, minus any potential post affix "(Clone)", to find its prefab index. string oldName = playerEntity.gameObject.name; oldName = oldName.Replace("(Clone)", ""); // Obtain reference to the current player's prefab to respawn. int prefabIndex = playerVehiclePrefabs.GetPrefabNameIndex(oldName); playerPrefab = playerVehiclePrefabs.GetPrefabAtIndex(prefabIndex); // Start the respawn timer if one set. if (timer) { timer.gameObject.SetActive(true); timer.StartTimer(respawnDelay); } // Otherwise, just respawn player now. else if (playerPrefab) { playerEntity = Instantiate(playerPrefab, originalPosition + Vector3.up * .5f, originalRotation) as Entity; } }
// Cycle vehicle demonstration... void CycleVehicle() { // Obtain reference to old vehicle. Vehicle oldVehicle = playerDriver.vehicle; // Obtain the name of the old vehicle, minus any potential post affix "(Clone)", to find its prefab index. string oldVehicleName = oldVehicle.gameObject.name; oldVehicleName = oldVehicleName.Replace("(Clone)", ""); // Obtain the prefab index of the next vehicle to instantiate (+1 of the old index). int nextVehiclePrefabIndex = playerVehiclePrefabs.GetPrefabNameIndex(oldVehicleName) + 1; // Save position and velocity of the old vehicle. Vector3 position = oldVehicle.transform.position + Vector3.up * .5f; Rigidbody oldRb = oldVehicle.GetComponentInChildren <Rigidbody>(); Vector3 velocity = (oldRb != null ? (oldRb.velocity.sqrMagnitude < .01f ? Vector3.forward : oldRb.velocity) : Vector3.forward); // Get the old vehicle entity if one exists so we can save Entity health, shield, etc. Entity oldEntity = oldVehicle.GetComponent <Entity>(); // Disable old vehicle to avoid interaction with new one. oldVehicle.gameObject.SetActive(false); // Instantiate next vehicle Vehicle newVehicle = Instantiate(playerVehiclePrefabs.GetPrefabAtIndex(nextVehiclePrefabIndex), position, Quaternion.LookRotation(velocity)) as Vehicle; // Copy rigidbody velocity if the new vehicle has one Rigidbody newRb = newVehicle.GetComponentInChildren <Rigidbody>(); if (newRb) { newRb.velocity = velocity; } // Obtain Entity on new vehicle Entity newEntity = newVehicle.GetComponent <Entity>(); // If old and new vehicles are Entities (typically the case), copy status. if (oldEntity && newEntity) { newEntity.health = oldEntity.health; newEntity.shield = oldEntity.shield; newEntity.GainExperience(null, oldEntity.experience); } // Send event for new vehicle to set it as the player vehicle. Events.setPlayerVehicle(newVehicle, false); // Destroy old vehicle. Destroy(oldVehicle.gameObject); }