In the following script, red crosses appear on screen for every cam eldritch available. But what if I just want to show the red cross of the saved cams instead of all cams?
using com.ootii.Actors;
using com.ootii.Actors.AnimationControllers;
using com.ootii.Actors.Attributes;
using com.ootii.Input;
using FIMSpace.FLook;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
//using System.Security.Policy;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class EldritchSight : MonoBehaviour
{
[Separator("CAMERAS")]
public Camera camPlayer;
public List camEldritch = new List();
code
[Separator("CROSS")]
public List <GameObject> redCross = new List<GameObject>();
public GameObject playerCross;
public GameObject traderCross;
[Separator("PLAYER UI")]
//UI TO TOGGLE
public GameObject[] UI;
public GameObject shotgunUI;
public GameObject pistolUI;
public GameObject rifleUI;
[Separator("SIGHT UI")]
[SerializeField] public TextMeshProUGUI camText;
public GameObject camIcon;
public Image redScreenOverlay; // Assign in the Inspector
[Separator("AUDIO")]
public AudioSource audioSource;
public AudioClip channelAudio;
public AudioClip eldritchSightAudio;
[Separator("Save Buttons")]
//Parent GameObject of the UI Save Buttons
public GameObject camSaveButtons;
// Reference to all the save slots UI elements
public Image[] saveButtonsUI = new Image[4];
// Define UI element colors
public Color saveColor = new Color32(222, 70, 70, 100); // Color to show when a camera is saved
public Color loadColor = new Color32(79, 176, 54, 100); // Color to show when a camera is loaded
public Color blankColor = new Color32(128, 128, 128, 100); // Color to show when no camera is saved (blank)
public Color highlightColor = Color.white; // Highlight color for loading
//RedScreen Variables
public bool InEnemySightCooldown() { return inEnemySightCooldown; }
public bool inEnemySightCooldown = false;
public float enemySightCooldown = 60f; // 60 seconds cooldown
private float enemySightTimer = 0f;
//Save Variables
private float[] lastTapTime = new float[4]; // Track the last time each key was pressed
private int[] savedCamIndexes = new int[4]; // Associated camera index for each slot
private float doubleTapThreshold = 0.5f; // Time allowed between taps for a double-tap
//Components Variables
private InventoryManager InvManager;
private ActorController actorController;
private MotionController mMotionController;
private UnityInputSource InputSource;
private Animator m_Animator;
private PlayerVitals playerVitals;
private CameraFilterPack_EyesVision_2 cameraFilterPack_EyesVision_2;
private NotificationUI MsgUI;
private DemoEnemyControls demoEnemyControls;
private GameObject playerFXObject;
private GameObject[] eldritch;
private GameObject trader;
private Transform player;
private ParticleSystem playerFX;
private AudioListener playerAudioListener;
private string BaseLayer;
private int currentCam;
private float spamLock;
private float maxDistance = 250f;
private float mediumDistance = 150f;
private float lowDistance = 75f;
private float cleanDistance = 0f;
private float cameraDistance;
private Camera mainCamera;
private Vector3 positionToScreen;
private Vector2 tooltipPosition = new Vector2(0f, 0f);
private bool _SightMode;
private bool _ExitingEldritchSight;
// Use this for initialization
void Start()
{
//TO DO :
//Button Mapping
//Wheel channel minigame for direction purpose??
GameObject go = GameObject.Find("Player");
if (go)
{
player = go.transform;
playerVitals = FindObjectOfType(typeof(PlayerVitals)) as PlayerVitals;
playerAudioListener = player.GetComponentInParent<AudioListener>();
playerFXObject = GameObject.FindGameObjectWithTag("EldritchSightFX");
playerFX = playerFXObject.gameObject.GetComponent<ParticleSystem>();
actorController = FindObjectOfType(typeof(ActorController)) as ActorController;
}
trader = GameObject.FindGameObjectWithTag("Trader");
eldritch = GameObject.FindGameObjectsWithTag("EldritchCamera");
camEldritch.Clear();
foreach (GameObject eldr in eldritch)
{
camEldritch.Add(eldr);
}
InputSource = FindObjectOfType(typeof(UnityInputSource)) as UnityInputSource;
InvManager = FindObjectOfType(typeof(InventoryManager)) as InventoryManager;
MsgUI = FindAnyObjectByType(typeof(NotificationUI)) as NotificationUI;
BaseLayer = "Base Layer";
m_Animator = GetComponentInParent<Animator>();
mMotionController = GetComponentInParent<MotionController>();
audioSource = GetComponent<AudioSource>();
mainCamera = Camera.main;
currentCam = 0;
cameraFilterPack_EyesVision_2 = camPlayer.gameObject.GetComponent<CameraFilterPack_EyesVision_2>();
_SightMode = false;
_ExitingEldritchSight = false;
camText.transform.gameObject.SetActive(false);
camIcon.SetActive(false);
camSaveButtons.SetActive(false);
//demoEnemyControls = GetComponentInParent<DemoEnemyControls>();
saveButtonsUI[0] = saveButtonsUI[0];
saveButtonsUI[1] = saveButtonsUI[1];
saveButtonsUI[2] = saveButtonsUI[2];
saveButtonsUI[3] = saveButtonsUI[3];
for (int i = 0; i < saveButtonsUI.Length; i++)
{
lastTapTime[i] = -1f; // Initializing to a negative value
savedCamIndexes[i] = -1; // No camera saved initially
saveButtonsUI[i].color = blankColor; // Initialize the button color to indicate a blank slot
}
}
// Update is called once per frame
void Update()
{
//RED CROSS
if (_ExitingEldritchSight)
{
for (int i = 0; i < camEldritch.Count; i++)
{
foreach (GameObject j in camEldritch)
{
positionToScreen = mainCamera.WorldToScreenPoint(camEldritch[i].transform.position);
cameraDistance = Vector3.Distance(transform.position, camEldritch[i].transform.position);
foreach (GameObject cross in redCross)
{
if (positionToScreen.z > 0)
{
if (redCross[i] != null)
{
redCross[i].transform.localPosition = new Vector3(positionToScreen.x + tooltipPosition.x - Screen.width / 2f, positionToScreen.y - Screen.height / 2f + tooltipPosition.y / (cameraDistance / 10), positionToScreen.z);
}
}
}
}
}
}
//GREEN CROSS
if (_SightMode)
{
foreach (GameObject j in camEldritch)
{
if (j.GetComponent<Camera>().enabled == true)
{
positionToScreen = j.GetComponent<Camera>().WorldToScreenPoint(transform.position);
cameraDistance = Vector3.Distance(j.GetComponent<Camera>().transform.position, player.transform.position);
playerCross.SetActive(false);
if (positionToScreen.z > 0)
{
playerCross.transform.localPosition = new Vector3(positionToScreen.x + tooltipPosition.x - Screen.width / 2f, positionToScreen.y - Screen.height / 2f + tooltipPosition.y / (cameraDistance / 10), positionToScreen.z);
playerCross.SetActive(true);
}
}
}
}
else
{
playerCross.SetActive(false);
}
//BLUE CROSS
if (_SightMode)
{
foreach (GameObject a in camEldritch)
{
if (a.GetComponent<Camera>().enabled == true)
{
positionToScreen = a.GetComponent<Camera>().WorldToScreenPoint(trader.transform.position);
cameraDistance = Vector3.Distance(a.GetComponent<Camera>().transform.position, trader.transform.position);
traderCross.SetActive(false);
if (positionToScreen.z > 0)
{
traderCross.transform.localPosition = new Vector3(positionToScreen.x + tooltipPosition.x - Screen.width / 2f, positionToScreen.y - Screen.height / 2f + tooltipPosition.y / (cameraDistance / 10), positionToScreen.z);
traderCross.SetActive(true);
}
}
}
}
else
{
traderCross.SetActive(false);
}
//SWITCH SIGHT
if (_SightMode)
{
if (Input.GetButton("NextCamera") && Time.time >= spamLock)
{
currentCam++;
toggleCam();
spamLock = Time.time + 2f;
}
if (Input.GetButton("PreviousCamera") && Time.time >= spamLock)
{
currentCam--;
toggleCam();
spamLock = Time.time + 2f;
}
}
//TOGGLE SIGHT
if ((Input.GetKey(KeyCode.Z) || DPadButtons.IsUp) && Time.time >= spamLock)
{
spamLock = Time.time + 2f;
if (!_SightMode)
{
StartCoroutine(EldritchSightMode());
_SightMode = true;
}
else
{
StartCoroutine(ExitingSightMode());
_SightMode = false;
}
}
//SAVE SIGHT
if (_SightMode)
{
// Input handling for save/load
string[] keys = new string[] { "1", "2", "3", "4" }; // Keys on the keyboard
string[] buttons = new string[] { "JoystickButton0", "JoystickButton1", "JoystickButton2", "JoystickButton3" }; // Corresponding buttons on the controller
for (int i = 0; i < 4; i++)
{
if (Input.GetKeyUp(keys[i]) || Input.GetButtonUp(buttons[i]))
{
// Check if it’s a double tap - within the threshold time
if (Time.time - lastTapTime[i] < doubleTapThreshold)
{
// Double-tap detected, save current camera index
savedCamIndexes[i] = currentCam;
// …Add visual or audio feedback for saving…
saveButtonsUI[i].color = saveColor; // Change color to indicate saving
ResetSaveButtonColors(i, saveColor, blankColor);
}
else
{
// Not a double-tap, load camera if saved
if (savedCamIndexes[i] >= 0)
{
StartCoroutine(setCam(savedCamIndexes[i]));
// …Add visual or audio feedback for loading…
// Highlight the loaded slot and then change to load color after short delay
StartCoroutine(HighlightSaveSlot(saveButtonsUI[i]));
//saveButtonsUI[i].color = loadColor; // Change color to indicate loading
ResetSaveButtonColors(i, saveColor, blankColor);
}
else // No saved cam index, slot is blank
{
// This might be a place where you display a message or sound indicating nothing is saved i
Assistant
To display the red cross only for saved cameras, you need to modify the logic that updates the position of the red crosses. You have to check whether the camera's index is within the savedCamIndexes array before updating its position. If it is not saved, you should not update or activate its red cross.
Here's a possible modification to the section of code that updates the red cross positions to take this into account:
Firstly, ensure that you have a way to know which red cross corresponds to which camera. If redCross list is ordered in the same way as camEldritch, then you can use the index to match them up. If not, you may need to create a mapping or ensure they are ordered correctly.
Here's a simplified version of the Update method with the modified red cross logic:
csharp
void Update()
{
// ... [Other code] ...
// RED CROSS
if (_ExitingEldritchSight)
{
for (int i = 0; i < camEldritch.Count; i++)
{
// Check if the camera is saved by looking for its index in savedCamIndexes
if (savedCamIndexes.Contains(i))
{
positionToScreen = mainCamera.WorldToScreenPoint(camEldritch[i].transform.position);
cameraDistance = Vector3.Distance(transform.position, camEldritch[i].transform.position);
if (positionToScreen.z > 0)
{
// Update the corresponding red cross position
// Ensure the index 'i' matches between 'camEldritch' and 'redCross'
redCross[i].transform.localPosition = new Vector3(positionToScreen.x + tooltipPosition.x - Screen.width / 2f, positionToScreen.y - Screen.height / 2f + tooltipPosition.y / (cameraDistance / 10), positionToScreen.z);
redCross[i].SetActive(true);
}
else
{
redCross[i].SetActive(false);
}
}
else
{
redCross[i].SetActive(false); // Hide the red cross if the camera is not saved
}
}
}
// ... [Other code] ...
// Ensure you also manage the visibility of the red crosses elsewhere in the script, such as when saving/loading cameras
}
Make sure to manage the red cross game objects only within the check if the camera is saved (if (savedCamIndexes.Contains(i))). With this change, the red crosses should only appear for cameras that have been saved by the player. Remember to also manage the activation and deactivation of red crosses when cameras are saved or loaded, as this logic only covers the update loop.
Lastly, when saving a camera, make sure to correctly update the savedCamIndexes array and the corresponding redCross activation state. When a camera is no longer saved, you should deactivate its red cross.
Please test thoroughly to ensure that the behavior is correct and no other part of the code conflicts with this logic.