Forum rules - please read before posting.

Card Game Feature

I have a few questions about the AC template/package. I am making a 3D Adventure, but as you need to search for items. In this game you need to search for Cards and you can use them as well. What is want to make right now is that you have cards in your inventory and if you walk to a NPC than your camera goes to a first person perspactive and you will see the NPC and right in front of you you will see your cards.

My question is how can I make this? Do I need a Custom Script for this?

This is a picture how I want it.

«134

Comments

  • edited February 2021

    Welcome to the community, @HugoMoritz.

    You can define your cards as Inventory items, and use the Camera: Switch Action as part of your NPC's Interaction to switch to a first-person perspective, but for something so specific in terms of arranging items like that - yes, it would be best to rely on custom scripting.

    At least so far as I can tell, It looks like the cards themselves are in the game world in 3D space - not a menu. What you'll need to do first is to create prefab equivalents for each card. That is, a 3D model of the card, that can be made into a prefab so that it can be added to the scene when desired.

    Once you've created a prefab for a card, navigate to it's associated inventory item's properties in the Inventory Manager, and assign it in the Linked prefab field. This will allow you to access it through scripting.

    One approach would be to hook into the OnCameraSwitch custom event, and spawn your cards at three the positions of 3 Transforms that represent the left, middle and right positions they should appear at. Here's a sample script that does just that:

    using UnityEngine;
    using AC;
    
    public class CardSpawner : MonoBehaviour
    {
    
        public _Camera firstPersonCamera;
        public Transform leftTransform;
        public Transform middleTransform;
        public Transform rightTransform;
    
    
        private void OnEnable () { EventManager.OnSwitchCamera += SwitchCamera; }
        private void OnDisable () { EventManager.OnSwitchCamera -= SwitchCamera; }
    
    
        private void SwitchCamera (_Camera old, _Camera newCamera, float transitionTime)
        {
            if (newCamera == firstPersonCamera)
            {
                SpawnCards ();
            }
        }
    
    
        private void SpawnCards ()
        {
            InvItem[] invItems = KickStarter.runtimeInventory.PlayerInvCollection.InvItems.ToArray ();
    
            if (invItems.Length > 0)
            {
                // Spawn first at middle
                SpawnCard (invItems[0], middleTransform);
    
                if (invItems.Length > 1)
                {
                    // Spawn second on left
                    SpawnCard (invItems[1], leftTransform);
    
                    if (invItems.Length > 2)
                    {
                        // Spawn third on right
                        SpawnCard (invItems[2], rightTransform);
                    }
                }
            }
    
        }
    
    
        private void SpawnCard (InvItem invItem, Transform appearTransform)
        {
            if (invItem.linkedPrefab == null)
            {
                Debug.LogWarning ("Cannot spawn item " + invItem.label + " in the scene, as it has no linked prefab.");
                return;
            }
    
            if (appearTransform == null)
            {
                Debug.LogWarning ("Cannot spawn item " + invItem.label + " in the scene, as no Transform was assigned.")
                return;
            }
    
            GameObject cardInstance = Instantiate (invItem.linkedPrefab);
            cardInstance.transform.position = appearTransform.position;
            cardInstance.transform.rotation = appearTransform.rotation;
        }
    
    }
    

    Of course, it's likely you'll want to extend / tweak this to suit your needs, but try attaching it to your scene, assign the "First Person Camera" field to it's Inspector as well as 3 Transforms that the 3 cards should appear at.

    For more on custom events, see this tutorial. More details on working with inventory items through script can be found in the Manual's "Inventory scripting" chapter.

  • Thank you for your time. I will test it and than I will let you know if it works but thanks for the quick reaction.

  • Hey Chris I don't understand this part:
    Once you've created a prefab for a card, navigate to it's associated inventory item's properties in the Inventory Manager, and assign it in the Linked prefab field. This will allow you to access it through scripting.

    I did make a prefab for the card but after that I don't understand. Can you send some screenshots of where I need to navigate?

  • Have you made an inventory item for it as well?

    Once you've created a new item in the Inventory Manager, the "Linked prefab" field will appear in its list of properties:

  • Another question I have is where should I put the custom script on?
    Because now I have it on the FirstPersonCamera. Because you have the default camera that is for the player and when the player interacts with the NPC than you switch to the firstpersoncamera.

    There is a actionlist on the npc default talk to interactions.
    But as player you don't really need to talk against the NPC you just have to interact and than you can see the cards and the NPC just stands still and does nothing.

  • It doesn't matter where it is, so long as it's in the scene. Though, it'd be tidiest to add it to the camera.

    What's important is that you fill in its Inspector - it's there that you need to assign the camera (NPC_Camera), which tells it which camera it should kick in for.


  • I get a error when the player interacts with the npc camera because I assigned the custom script on the camera. I don't know what to do with this error could you help me?

  • For the script to work, you must have a linked prefab for the first three items in your inventory, as well as having the three "Transform" fields in the Inspector assigned.

    Copy/paste the code above into your script. It will now give a detailed warning if the issue occurs again.


  • I put it on the wrong asset file. So I don't get any errors now but i can't see the cards it goes to the firtpersoncamera (NPC_Camera) but it does not show the cards

  • Have you updated the script from above?

    The cards will be placed at the positions you set through the three Transform fields, which should be assigned in the Inspector.

    Where are these Transforms? Check your Hierarchy and Scene windows when the spawning occurs - you should find the cards listed in the Hierarchy, and selecting them will allow you to see where they are in the Scene window.

  • The cards don't spawn in.
    Do they need to spawn is that maybe the problem?

  • edited February 2021

    They do need to spawn in - you want to have 3D versions of your inventory items to be present in the scene. I'm only going by the details you shared in your original post.

    If you've updated the script from above, you'll get a warning if there's a problem. If there's no warning, they should be spawning in - check your Hierarchy window.

  • How am i sure that the inventory is 3D version?
    But I swear the items dont spawn in. I don't see them in the hierarchy.

    https://cdn.discordapp.com/attachments/503593005266108427/811189747732447242/unknown.png
    This is the only warning I get.

  • By "3D inventory", I'm referring to the linked item prefabs.

    Where in your scene have you attached the script? Let's see screens of its Inspector fields.

  • Replace your CardSpawner script with the following. It will parent the cards to the Transforms, and report in the Console as well:

    using UnityEngine;
    using AC;
    
    public class CardSpawner : MonoBehaviour
    {
    
        public _Camera firstPersonCamera;
        public Transform leftTransform;
        public Transform middleTransform;
        public Transform rightTransform;
    
    
        private void OnEnable () { EventManager.OnSwitchCamera += SwitchCamera; }
        private void OnDisable () { EventManager.OnSwitchCamera -= SwitchCamera; }
    
    
        private void SwitchCamera (_Camera old, _Camera newCamera, float transitionTime)
        {
            if (newCamera == firstPersonCamera)
            {
                SpawnCards ();
            }
        }
    
    
        private void SpawnCards ()
        {
            InvItem[] invItems = KickStarter.runtimeInventory.PlayerInvCollection.InvItems.ToArray ();
    
            if (invItems.Length > 0)
            {
                // Spawn first at middle
                SpawnCard (invItems[0], middleTransform);
    
                if (invItems.Length > 1)
                {
                    // Spawn second on left
                    SpawnCard (invItems[1], leftTransform);
    
                    if (invItems.Length > 2)
                    {
                        // Spawn third on right
                        SpawnCard (invItems[2], rightTransform);
                    }
                }
            }
    
        }
    
    
        private void SpawnCard (InvItem invItem, Transform appearTransform)
        {
            if (invItem.linkedPrefab == null)
            {
                Debug.LogWarning ("Cannot spawn item " + invItem.label + " in the scene, as it has no linked prefab.");
                return;
            }
    
            if (appearTransform == null)
            {
                Debug.LogWarning ("Cannot spawn item " + invItem.label + " in the scene, as no Transform was assigned.");
                return;
            }
    
            GameObject cardInstance = Instantiate (invItem.linkedPrefab);
            cardInstance.transform.SetParent (appearTransform);
            cardInstance.transform.position = appearTransform.position;
            cardInstance.transform.rotation = appearTransform.rotation;
            Debug.Log ("Spawned item " + invItem.label + " at " + appearTransform, cardInstance);
        }
    
    }
    
  • It still does not work and i also get no warnings or errors and the cards dont spawn in.

  • edited February 2021

    The script works by checking the Player's inventory - how many items is the Player actually carrying at the time?

    Here is a super-debug-logging version of the script that will print every part of the process into the Console. What gets shown when you switch to the camera?

    using UnityEngine;
    using AC;
    
    public class CardSpawner : MonoBehaviour
    {
    
        public _Camera firstPersonCamera;
        public Transform leftTransform;
        public Transform middleTransform;
        public Transform rightTransform;
    
    
        private void OnEnable () { EventManager.OnSwitchCamera += SwitchCamera; Debug.Log ("CardSpawner - Registered event"); }
        private void OnDisable () { EventManager.OnSwitchCamera -= SwitchCamera; Debug.Log ("CardSpawner - Unregistered event"); }
    
    
        private void SwitchCamera (_Camera old, _Camera newCamera, float transitionTime)
        {
            if (newCamera == firstPersonCamera)
            {
                Debug.Log ("CardSpawner - Switched to correct camera");
                SpawnCards ();
            }
        }
    
    
        private void SpawnCards ()
        {
            InvItem[] invItems = KickStarter.runtimeInventory.PlayerInvCollection.InvItems.ToArray ();
    
            Debug.Log ("CardSpawner - Currently got " + invItems.Length + " items");
            if (invItems.Length > 0)
            {
                // Spawn first at middle
                SpawnCard (invItems[0], middleTransform);
    
                if (invItems.Length > 1)
                {
                    // Spawn second on left
                    SpawnCard (invItems[1], leftTransform);
    
                    if (invItems.Length > 2)
                    {
                        // Spawn third on right
                        SpawnCard (invItems[2], rightTransform);
                    }
                }
            }
    
        }
    
    
        private void SpawnCard (InvItem invItem, Transform appearTransform)
        {
            if (invItem.linkedPrefab == null)
            {
                Debug.LogWarning ("CardSpawner - Cannot spawn item " + invItem.label + " in the scene, as it has no linked prefab.");
                return;
            }
    
            if (appearTransform == null)
            {
                Debug.LogWarning ("CardSpawner - Cannot spawn item " + invItem.label + " in the scene, as no Transform was assigned.");
                return;
            }
    
            GameObject cardInstance = Instantiate (invItem.linkedPrefab);
            cardInstance.transform.SetParent (appearTransform);
            cardInstance.transform.position = appearTransform.position;
            cardInstance.transform.rotation = appearTransform.rotation;
            Debug.Log ("CardSpawner - spawned item " + invItem.label + " at " + appearTransform, cardInstance);
        }
    
    }
    
  • this is what I
    get from the console

  • If you're getting that, it's because the Player currently has no inventory items.

    Make sure that the Player is carrying the cards - either by default by checking Carry on start? in the Item's properties, or by adding them at runtime with the Inventory: Add or remove Action.

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.