0
\$\begingroup\$

I am trying to create a simple VR card game in Unity using OpenXR, just to learn Unity and also some VR stuff. I made a deck of cards object and a card object. I put both in a scene. The card is Kinematic and can be grabbed and thereby picked up. The deck is static on the table. I put the card on top of the deck and I can pick it up (see https://drive.google.com/file/d/1gi4RAUDzS-r862I6ur3eb_YZPQRXjMMA/view?usp=sharing). However I want to spawn a new card on top of the deck to grab. First I wanted to spawn the card, first when the user makes the grab action, touching the pile, but I could not find any information how to do that and actually attach the card (instead of the deck) to the hand. How can I detect the grab of my playing card, and spawn a new card on top of the deck instead of the one just taken by a player? I am an experienced software developer, but I am a total noob when it comes to Unity. I tried to find some answers online, but I think I lack some basic understanding of how Unity works. I hope someone can point me in the right direction.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

I don't know OpenXR, but I do know unity. You can just duplicate the card using Instantiate, like this:

private void PickedUpCard() {
    Instantiate(cardObject, deckPosition.position, Quaternion.idenitity);
}
\$\endgroup\$
5
  • \$\begingroup\$ I have this "Select Entered" event on the XR Grab Interactable, but the dropdown is saying "No Function", so how do I add the function? \$\endgroup\$
    – nivs1978
    Jan 20 at 20:11
  • \$\begingroup\$ You need to create it. \$\endgroup\$
    – Pow
    Jan 20 at 20:12
  • \$\begingroup\$ Thanks. I found a thread that told me to click Add Component and just put in any name to generate a C# script file. So I am getting closer. \$\endgroup\$
    – nivs1978
    Jan 20 at 20:41
  • \$\begingroup\$ I had to add a Public GameObject deckPrefab and assign the GameDeck object to this property, then use the deckPrefab.transform to get the position and rotation of the deck and then i can spawn a new card on top of the deck. Thank you for pointing me in the right direction. I still need to figure out the grab stuff, but at least this got me going. \$\endgroup\$
    – nivs1978
    Jan 20 at 22:38
  • \$\begingroup\$ No problem, yeah, instantiate just duplicates a prefab. So if you want to have multiple cards and pick a random one, you can turn the object into a list of gameobjects and then pick a random one. \$\endgroup\$
    – Pow
    Jan 21 at 11:56
0
\$\begingroup\$

With the help of "Pow" I have found a solution.

I created a property on my Card object:

public GameObject deckPrefab;

Then dragged the deck object from the scene onto this property to assign the deck object ot the deckPrefab property.

Next when the card is grabbed. I instanciate a new card on top of the deck:

GameObject newCard = Instantiate(cardPrefab, new Vector3(deckPrefab.transform.position.x, deckPrefab.transform.position.y + 0.01f, deckPrefab.transform.position.z), Quaternion.identity);

The card has a property, which card it is (taken from a list of all cards remaining in the deck). And the face of the card is replaced with a new texture:

var renderer = newCard.GetComponentInChildren<Renderer>();
for (int i=0; i<renderer.materials.Length; i++)
{
    var material = renderer.materials[i];
    if (material.name.StartsWith("CardFace"))
    {
        int idx = GetCardIndex(newCard.textureIndex);
        material.SetTexture("_BaseMap", cardFaces[idx]);
        break;
    }
}

cardFaces is an array of Texture2D objects with all the PNG images of the card faces. I used this guide on YouTube for inspiration, but modified to use 3D objects instead of sprites: https://www.youtube.com/playlist?list=PLuq_iMEtykn8XiZb6453f61U7HC7u8KiC

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .