// Use this for initialization private void Start() { //If this camera weight affects player 2 if (this.playerThatCanFollow == Players.P2) { //Finds the static reference for the player 2 camera this.cameraRef = FollowCameraWeights.p2GlobalReference; } //If this camera weight affects anyone else else { //Finds the static reference for the player 1 camera this.cameraRef = FollowCameraWeights.p1GlobalReference; } //If this object always needs to be tracked by a player, adds their weight to the designated camera if (this.alwaysRemainOnCam && this.cameraRef != null) { FollowCameraWeights.AddWeightedObject(this, this.playerThatCanFollow); } //Makes sure the Drop Distance is greater than the Add Distance to prevent errors with the weight system if (this.dropDistance <= this.addDistance) { this.dropDistance = this.addDistance + 1; } }
//Adds and drops this object from the designated camera when it gets in or out of range and returns a bool based on if this object is being tracked by the camera private bool HandleCamera(GameObject cameraObj_, bool onCamera_) { //If the designated camera doesn't exist, nothing happens if (cameraObj_ == null) { return(false); } bool isOnScreen = onCamera_; //If this object is already being tracked by the designated camera, finds out if it should be dropped if (onCamera_) { //Tracks the distance between only X and Y coords if (this.track2D) { Vector2 thisObj = new Vector2(transform.position.x, transform.position.y); Vector2 camObj = new Vector2(cameraObj_.transform.position.x, cameraObj_.transform.position.y); float dist = Vector2.Distance(thisObj, camObj); //If the distance is greater than the drop distance, it won't be tracked anymore if (dist >= this.dropDistance) { FollowCameraWeights.DropWeightedObject(this, this.playerThatCanFollow); isOnScreen = false; } } //Tracks the distance between X, Y, and Z coords else { float dist = Vector3.Distance(transform.position, cameraObj_.transform.position); //If the distance is greater than the drop distance it won't be tracked anymore if (dist >= this.dropDistance) { FollowCameraWeights.DropWeightedObject(this, this.playerThatCanFollow); isOnScreen = false; } } } //If this object isn't being tracked by the designated camera, finds out if it should be added else { //Tracks the distance between only X and Y coords if (this.track2D) { Vector2 thisObj = new Vector2(transform.position.x, transform.position.y); Vector2 camObj = new Vector2(cameraObj_.transform.position.x, cameraObj_.transform.position.y); float dist = Vector2.Distance(thisObj, camObj); //If the distance is less than the add distance, it will be tracked if (dist <= this.addDistance) { FollowCameraWeights.AddWeightedObject(this, this.playerThatCanFollow); isOnScreen = true; } } //Tracks the distance between X, Y, and Z coords else { float dist = Vector3.Distance(transform.position, cameraObj_.transform.position); //If the distance is less than the add distance it will be tracked if (dist <= this.addDistance) { FollowCameraWeights.AddWeightedObject(this, this.playerThatCanFollow); isOnScreen = true; } } } return(isOnScreen); }
// Use this for initialization private void Awake() { //If this camera follows player 2 if (this.playerToFollow == Players.P2) { //Sets the static reference to this camera if one doesn't already exist if (p2GlobalReference == null) { p2GlobalReference = this; //Setting our rotation object to follow as the p2 ship if (PlayerShipController.p1ShipRef != null) { this.rotationFollowObj = PlayerShipController.p2ShipRef.gameObject; } //If the p2 ship is null, we disable this obj else { this.enabled = false; } } //If there's already a static reference else { //If the static reference is disabled, this camera becomes the static reference if (!p2GlobalReference.gameObject.activeInHierarchy) { p2GlobalReference = this; //Setting our rotation object to follow as the p2 ship if (PlayerShipController.p1ShipRef != null) { this.rotationFollowObj = PlayerShipController.p2ShipRef.gameObject; } //If the p2 ship is null, we disable this obj else { this.enabled = false; } } //If the static reference is still awake, this camera becomes disabled else { this.enabled = false; } } } //If this camera follows anything but player 2, it's set to player 1 else { //Sets the static reference to this camera if one doesn't already exist if (p1GlobalReference == null) { p1GlobalReference = this; //Setting our rotation object to follow as the p1 ship if (PlayerShipController.p1ShipRef != null) { this.rotationFollowObj = PlayerShipController.p1ShipRef.gameObject; } //If the p1 ship is null, we disable this obj else { this.enabled = false; } } //If there's already a static reference else { //If the static reference is disabled, this camera becomes the static reference if (!p1GlobalReference.gameObject.activeInHierarchy) { p1GlobalReference = this; //Setting our rotation object to follow as the p1 ship if (PlayerShipController.p1ShipRef != null) { this.rotationFollowObj = PlayerShipController.p1ShipRef.gameObject; } //If the p1 ship is null, we disable this obj else { this.enabled = false; } } //If the static reference is still awake, this camera becomes disabled else { this.enabled = false; } } } //Initializes the list to hold weighted objects this.weightObjects = new List <GameObject>(); //Makes sure the sum of weights starts at 0 this.weightSum = 0; //Stores the reference to this object's camera component this.weightCamera = GetComponent <Camera>(); }