private void UpdateDeadEye() { // If we're in shooting state and we still have targets in our list if (deadEyeState == DeadEyeState.shooting && targets.Count > 0) { // Get the current target in a temporary variable of type Transform which is the first element in the list Transform currentTarget = targets[0]; // Get the required rotation for our camera to be looking at the target Quaternion rot = Quaternion.LookRotation(currentTarget.position - transform.position); // Updating the camera rotation to the "Looking at target" rotation gradually, 30deg/s transform.rotation = Quaternion.Slerp(transform.rotation, rot, 30 * Time.deltaTime); // Get the difference between our current rotation and the target's float diff = (transform.eulerAngles - rot.eulerAngles).magnitude; // Check if the diff is less than or quals "0.1f" (You can change it depending on the desired accuracy) // AND the gun has cooled down (You can use your gun script's cooldown if you are using one) if (diff <= 0.1f && cooldownTimer <= 0) { // We are looking at the target // Fire a single shot Fire(); // Remove the target form the list targets.Remove(currentTarget); // Destroy the target Destroy(currentTarget.gameObject); } } else // Either we're not in shooitng mode or we ran out of targets { deadEyeState = DeadEyeState.off; // Reset the DeadEye state to off } }
private void Update() { // Update timer if (cooldownTimer > 0.0f) { cooldownTimer -= Time.deltaTime; } else { cooldownTimer = 0.0f; } // Aim (Hold Right Click) - Enter DeadEye if (Input.GetButtonDown("Fire2")) { // Enter targeting mode if it's off if (deadEyeState == DeadEyeState.off) { deadEyeState = DeadEyeState.targeting; } } // Fire (Left Click) - If DeadEye, SetTarget. Else just Fire() if (Input.GetButtonDown("Fire1")) { // If we're not in the DeadEye mode, fire a single shot if (deadEyeState == DeadEyeState.off) { Fire(); } // If we're in targeting state in the DeadEye mode, then we will assign a new target if (deadEyeState == DeadEyeState.targeting) { // Setting targets // Storing the collision info into a new variable "hit" RaycastHit hit; // Check if we hit an object starting from our position, going straight forward with a max distance of 100 units if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, 100)) { // Creating a temporary GameObject to store the target info GameObject tmpTarget = new GameObject(); // Assign the target position to it tmpTarget.transform.position = hit.point; // Attach it to the target (child of the target) so it updates its position if the target is moving tmpTarget.transform.parent = hit.transform; // Add its transform to our List of targets targets.Add(tmpTarget.transform); } } } // Release (Release Right Click) if (Input.GetButtonUp("Fire2")) { // If we're in 'targeting' mode, we will go to 'shooting' mode if (deadEyeState == DeadEyeState.targeting) { deadEyeState = DeadEyeState.shooting; } } }