Forum rules - please read before posting.

Setting a Camera: Switch Action’s NavCamera GameObject from a Script

I would like to be able to update the NavCamera object assigned to a Camera: Switch Action using a Script. In the Script, I am able to get the Action object from the ActionList, but I’m not sure how to update the Action’s NavCamera value, or if that’s even the correct way to go about it.

Any advice about how to do this or pointing me in the right direction of some documentation or discussion I’ve missed would be greatly appreciated!

Thanks!

Comments

  • edited September 2021

    Welcome to the community, @Kopavi.

    To update an Action's fields at runtime, you want to make use of ActionList parameters - see ActionList parameters for an overview.

    The Camera: Switch Action's "New camera" field can be overridden with a GameObject parameter. Parameter values are normally set using either the ActionList: Run or ActionList: Set parameter Action, but they can also be set using custom script.

    For example, this function can be used to update an ActionList's first parameter to a specified camera:

    public AC.ActionList actionList;
    public Camera myCamera;
    
    public void SetCameraParameter ()
    {
        actionList.GetParameter (0).SetValue (myCamera.gameObject);
    }
    

    Though, given that parameters can be set without scripting, you may not need to rely on a custom script for this. What's the scenario you're faced with?

  • Perfect! This looks like exactly what I was looking for!

    I’m lazy. My goal is to create a generic Room Prefab which has a child NavMesh and Camera. Then I want to have a Door Prefab which is or has a Hotspot child and Marker child. An instance of the Door Prefab will be added as a child of a Room Prefab.

    I’d like to have a script attached to the Room object that exposes a Door object member. That way, when one Door instance is attached to another Door instance, the script will automatically set which Camera the Switch Action is attached to by looking at the Parent Room object of the Door. Three actions will always occur when using the Door: the camera will switch to the Room object which contains the corresponding Door object, the active NavMesh will be set to that of the Room’s child NavMesh, and the the Player will be teleported to the Marker of the Corresponding Door.

    I k is all of the can be done fairly simply through the UI, but I want something I ca. Reuse with very little effort, and I’m hoping the script will save me having to set the 3 GameObjects I would otherwise have to set manually.
  • If you're looking to re-use logic, you may wish to go through this tutorial on using parameters to create logic prefabs.

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.