Forum rules - please read before posting.

Make script only trigger on active camera

I'm trying to write a basic script that allows for manual changing of cameras on Right-Click. I have it working so far, but it works too well since it's always firing. I only want the script to function if the camera is currently switched to it. I'm trying to familiarize myself with the code documentation but I've hit a wall.

How do I prevent the script from working unless the player is set to a specific camera?

public _Camera GoBackTo;

// Start is called before the first frame update
void Update ()
{
    if (Input.GetButtonDown("InteractionB"))
    {
        AC.KickStarter.mainCamera.SetGameCamera(GoBackTo, 1.0f, MoveMethod.Smooth, null);
    }
}

Comments

  • The currently-active camera is recorded in the MainCamera's attachedCamera variable. If your script is attached to the Camera you want it to respond to, you can include a check to see if it's also the active camera:

    if (Input.GetButtonDown("InteractionB") && KickStarter.mainCamera.attachedCamera == GetComponent<_Camera>())
    
  • edited August 2020

    Ah thanks that works really well. However, I'm running into an issue that I'll try to explain the best I can.

    This script is essentially made so that when the Player Right-Clicks, they go back to a previous area/camera. I had tried automating it through an Active Input to go back, however this didn't work as well. You could easily get in a loop between 2 different locations when pressed without going to where it needed to when backtracking. I put this script on every camera so I can specify which is the camera it's going to switch to when I right-click, which is great and is so far mostly fine.

    However, I'm running into an issue that seems to occur when I use Pixel Crusher's Dialogue System. In my game, one hotspot activates an Action List Interaction that switches from Camera A to Camera B, triggers a Dialogue System conversation, runs a Dialogue System Alert and ends. After the conversation ends, right-clicking should go from Camera B > Camera A > Camera C. However, once the conversation finishes, Adventure Creator seems to be in a weird state where it doesn't correctly recognize the camera it is currently on. While I'm on Camera B, it seems to internally think I'm on Camera A because upon right-click, it runs the Backtrack Script on Camera A and switches to Camera C. This is consistent even when I change the Camera C is in Camera A's script.

    I'm lost as to why this is as I put a check in the code to constantly spit out which camera it is on (KickStarter.mainCamera.attachedCamera) in the console. However, when it's on Camera B it says it is on Camera B, so why it's running the commands of Camera A don't make much sense.Including an extra action at the end of the Action List to ensure the player is on Camera B also doesn't work. I'm a bit lost as to what's happening here. Any ideas?

  • If you enable the AC Status window via the bottom of the Settings Manager, it will tell you what the active GameCamera is at runtime. This should match the .attachedCamera field you're debugging.

    The custom script should only have any effect on the camera it's attached to, so my first guess would be that maybe there's two instances of the script on one camera, or the fields on one of them aren't correct.

    If these are all fine, what is the state of the MainCamera? Is it still the only one that's rendering, or is Dialogue System taking control over it or rendering from another camera?

  • Just checked the Status Window and everything checks out. It's on the right camera, it's just not running that same camera's code, which makes no sense. Upon closer look at the checks I made in the console on Update, it seems like for like a very small fraction of a second assigns itself from Camera B to Camera A then immediately goes to Camera C so quickly you don't even notice. It does this so fast, it doesn't even show up in the AC Status window.

    Even worse, I discovered this is also happening on a few other objects that are set to Switch to Camera A, so something seems to be up with that camera. This again is odd because I thought maybe it's just processing my button clicks very fast and fast forwarding, but there's another area in the level that works similarly and it works just fine. I've checked countless times and all the camera objects check out: They run only one of same script with different Cameras set to switch to.

    I tested this by removing the script from Camera A and sure enough, now it goes back to the correct camera so it seems like it's just zooming past Camera A and running its script immediately to go to Camera C, so the dialogue system isn't the issue (though I need to figure out a way to disable the script when Dialogue System Conversations are active).

    Might there be a way to bullet proof the code somehow? Maybe forcing an a mandatory transition that can't be interrupted? It seems that for some reason everything is fast forwarding through Camera A when its script is active, but other cameras don't have this issue.

  • edited August 2020

    Okay, well this was dumb...

    I removed the script from Camera A while I worked for a bit. Then I reattached it again with the exact same configuration to test it again and now... It works... For some reason. I even went back a version in my collab and yep, it's exactly as before... But it works now. Problem solved I guess?

  • Strange indeed. One other thing it could be is that - during a transition - the "next" camera will be considered the "active" one when requested. It could be that the same click is registering on both scripts together.

    You should be able to confirm this with a Debug.Log statement inside the custom script - next to the SetGameCamera call.

    I'll add a means of checking if the MainCamera is currently transitioning, so that this can be avoided if this is the case.

  • edited August 2020

    Yes, please do. I haven't touched the camera, but it started happening again. Seems to be a glitch with the system.

    Also, this is the third time I've made this post and it keeps glitching out existence when I put the code here in case that might be helpful, so I'm just gonna put it on pastebin.

    https://pastebin.com/6VXteNcz

  • Yes, please do.

    Insert the following into MainCamera.cs:

    public bool IsInTransition ()
    {
        return (transitionTimer > 0f);
    }
    

    Also, this is the third time I've made this post and it keeps glitching out existence

    Your posts were being flagged up by the spam checker for some reason. I've now verified your account, so that should not happen again.

  • I put that code in MainCamera.cs, but nothing seems to have changed. Glitch still occurs on that camera.

  • That wasn't the fix - that's so you can incorporate the function into your code.

    Update your "if" statement with this:

    if (Input.GetButtonUp("InteractionB") && KickStarter.mainCamera.attachedCamera == GetComponent<_Camera>() && isInConversation == false && !KickStarter.mainCamera.IsInTransition())
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.