Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Anyone who can tell me where I can post my recvest about making tcg/trading card battle ?

Discussion in '2D' started by henningm, Nov 15, 2018.

  1. henningm

    henningm

    Joined:
    Jul 11, 2016
    Posts:
    18
    Design is easy, but scripting to help the game is hard.
    I try to get rotating the card, but it will not work what ever I do.

    Anyone who can collaborate with me ?
     
  2. henningm

    henningm

    Joined:
    Jul 11, 2016
    Posts:
    18
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    [ExecuteInEditMode]

    //0 referance

    public class Card : MonoBehaviour
    {
    public RectTransform cardFront;
    public RectTransform cardBack;
    public Transform targetFacePoint;
    public Collider col;

    private bool showingBack = false;


    private void Update()
    {
    RaycastHit[] hits;
    hits = Physics.RaycastAll(origin: Camera.main.transform.position + targetFacePoint.position,
    direction: (-Camera.main.transform.position + targetFacePoint.position).normalized,
    maxDistance: (-Camera.main.transform.position + targetFacePoint.position).magnitude);

    bool passedThroughTargetCollider = false;

    foreach (RaycastHit h in hits)
    {
    if (h.collider == col)
    {
    passedThroughTargetCollider = true;
    }

    if (passedThroughTargetCollider != showingBack)
    {
    showingBack = passedThroughTargetCollider;
    if (showingBack)
    {
    cardFront.gameObject.SetActive(false);
    cardBack.gameObject.SetActive(true);
    }
    else
    {
    cardFront.gameObject.SetActive(true);
    cardBack.gameObject.SetActive(false);
    }
    }
    }
    }
    }


    I hope someone can help me to figure out what the problem I having here.
     

    Attached Files:

  3. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    maybe try to check if there is something in the array.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [ExecuteInEditMode]
    6.  
    7. //0 referance
    8.  
    9. public class Card : MonoBehaviour
    10. {
    11.     public RectTransform cardFront;
    12.     public RectTransform cardBack;
    13.     public Transform targetFacePoint;
    14.     public Collider col;
    15.  
    16.     private bool showingBack = false;
    17.  
    18.  
    19.     private void Update()
    20.     {
    21.         RaycastHit[] hits;
    22.         hits = Physics.RaycastAll(origin: Camera.main.transform.position + targetFacePoint.position,
    23.         direction: (-Camera.main.transform.position + targetFacePoint.position).normalized,
    24.         maxDistance: (-Camera.main.transform.position + targetFacePoint.position).magnitude);
    25.  
    26.         bool passedThroughTargetCollider = false;
    27.  
    28.         if (hits != null)
    29.         {
    30.             foreach (RaycastHit h in hits)
    31.             {
    32.                 if (h.collider == col)
    33.                 {
    34.                     passedThroughTargetCollider = true;
    35.                 }
    36.  
    37.                 if (passedThroughTargetCollider != showingBack)
    38.                 {
    39.                     showingBack = passedThroughTargetCollider;
    40.                     if (showingBack)
    41.                     {
    42.                         cardFront.gameObject.SetActive(false);
    43.                         cardBack.gameObject.SetActive(true);
    44.                     }
    45.                     else
    46.                     {
    47.                         cardFront.gameObject.SetActive(true);
    48.                         cardBack.gameObject.SetActive(false);
    49.                     }
    50.                 }
    51.             }
    52.         }
    53.     }
    54. }
     
  4. henningm

    henningm

    Joined:
    Jul 11, 2016
    Posts:
    18
    Thank you for answering my post.
    You have to help me out here.
    How do I check if there is something in the array.
    What is wrong with the array ?
    Shoul it be something in the array ?

    Maybe something like this: private void Update(ref int i) ??
    Or RaycastHit[ref int i] hits; ??

    No wonder I having big problem to find it out.
     
  5. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    line 28 in the edited script, still I am not sure if this will help
     
  6. henningm

    henningm

    Joined:
    Jul 11, 2016
    Posts:
    18
    I hope you can see this picture. Data of the card.png
     

    Attached Files:

    Last edited: Nov 17, 2018
  7. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    Look at the script that I have posted. For array-check you can compare it with null
    1. if (hits != null)
    2. {
    3. foreach (RaycastHit h in hits)
     
  8. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    and if you will make just card rotation then maybe use 3d card. You can rotate it as you will.
     
  9. henningm

    henningm

    Joined:
    Jul 11, 2016
    Posts:
    18
    I try to make a Trading card battle with face down.
    You pick a card and you can see an avatar do battle.
    Card it self not importen to have a 3d, only 2d.
    Battle is in 3d.

    I try to upload som picture but am not sure I got it right to show you.
    For that am sorry.

    I made a video recording but it was to big for forum.

    If you go to twitch under Henmart and find a clip called "Testing" you can see the recording of it.
     
  10. henningm

    henningm

    Joined:
    Jul 11, 2016
    Posts:
    18
    Look at the script that I have posted. For array-check you can compare it with null
    #D Game looks like this, I hope.
    1. if (hits != null)
    2. {
    3. foreach (RaycastHit h in hits)
    [/QUOTE]
    Ok, but what will I put in instead for h ??



    Ok, I change it to this:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    [ExecuteInEditMode]
    public class Card : MonoBehaviour
    {
    public RectTransform cardFront;
    public RectTransform cardBack;
    public Transform targetFacePoint;
    public Collider col;

    private bool showingBack = false;

    private void Update()
    {
    RaycastHit[] hits;
    hits = Physics.RaycastAll(origin: Camera.main.transform.position + targetFacePoint.position,
    direction: (-Camera.main.transform.position + targetFacePoint.position).normalized,
    maxDistance: (-Camera.main.transform.position + targetFacePoint.position).magnitude);

    bool passedThroughTargetCollider = false;


    if (hits != null)
    {
    {
    passedThroughTargetCollider = true;
    }

    if (passedThroughTargetCollider != showingBack)
    {
    showingBack = passedThroughTargetCollider;
    if (showingBack)
    {
    cardFront.gameObject.SetActive(false);
    cardBack.gameObject.SetActive(true);
    }
    else
    {
    cardFront.gameObject.SetActive(true);
    cardBack.gameObject.SetActive(false);
    }
    }

    }
    }
    }


    But still the card back not showing up.
    But now the spell card body is now in highlighting in yellow. :)





    Edit:
    Not even this worked:
    if (hits != null)
    {
    showingBack = passedThroughTargetCollider;
    if (showingBack)
    {
    cardFront.gameObject.SetActive(false);
    cardBack.gameObject.SetActive(true);
    }
    else
    {
    cardFront.gameObject.SetActive(true);
    cardBack.gameObject.SetActive(false);
    }
    }




    Edit 2:

    hits = Physics.RaycastAll(origin: Camera.main.transform.position + targetFacePoint.position,

    object reference not set to an instance of an object.

    Loks I have a poblem with this ??
     

    Attached Files:

    Last edited: Nov 17, 2018
  11. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    try to do something with this,
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Card : MonoBehaviour
    6. {
    7.     public RectTransform cardFront;
    8.     public RectTransform cardBack;
    9.     public bool showingBack = false;
    10.     Collider col;
    11.     bool canRotate = false, canRotateBack = false;
    12.     public float rotateSpeed;
    13.  
    14.     void Start ()
    15.     {
    16.         col = GetComponent<Collider>();
    17.     }
    18.  
    19.     private void Update()
    20.     {
    21.         if (Input.GetMouseButtonDown(0) && !canRotate && !canRotateBack)
    22.         {
    23.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    24.             RaycastHit[] hit = Physics.RaycastAll(ray);
    25.             if (hit != null)
    26.             {
    27.                 foreach (RaycastHit h in hit)
    28.                 {
    29.                     if (h.collider == col)
    30.                     {
    31.                         CardRotate();
    32.                     }
    33.                 }
    34.             }
    35.         }
    36.  
    37.         if (canRotate)
    38.         {
    39.             transform.Rotate (Vector3.up * rotateSpeed * Time.deltaTime);
    40.             if (UnityEditor.TransformUtils.GetInspectorRotation (transform).y >= 90)
    41.             {
    42.                 transform.eulerAngles = Vector3.up * 90;
    43.                 canRotate = false;
    44.                 showingBack = !showingBack;
    45.                 if (showingBack)
    46.                 {
    47.                     cardFront.gameObject.SetActive(false);
    48.                     cardBack.gameObject.SetActive(true);
    49.                 } else
    50.                 {
    51.                     cardFront.gameObject.SetActive(true);
    52.                     cardBack.gameObject.SetActive(false);
    53.                 }
    54.                 canRotateBack = true;
    55.             }
    56.         }
    57.  
    58.         if (canRotateBack)
    59.         {
    60.             transform.Rotate(Vector3.up * -rotateSpeed * Time.deltaTime);
    61.             if (UnityEditor.TransformUtils.GetInspectorRotation(transform).y <= 0)
    62.             {
    63.                 transform.eulerAngles = Vector3.zero;
    64.                 canRotateBack = false;
    65.             }
    66.         }
    67.     }
    68.  
    69.     void CardRotate ()
    70.     {
    71.         canRotate = true;
    72.     }
    73. }
    but I would say, use 3d card for rotate
     
  12. henningm

    henningm

    Joined:
    Jul 11, 2016
    Posts:
    18
    Thanks.
    is there a 3d templates of card ?
    I guess you have o buy it ??
     
  13. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    you can find the free royality 3d card in google. Or make it with one 3d program. The simple card is just one flat box.

    you can try this fbx file, this is the perfect work from me in blender :D Sorry i am not making 3d games and this is awful.
    http://xrayisgray.de/packages/Card.fbx
    then import this .fbx file in unity, create new material (right click in assets), add your card image to this material (left from "Albedo" is the select point, or switch material to unlite/texture and add the image to it). Then add this material to the card prefab (the card have 3 materials: border, back and front, just change the needed).
     
  14. henningm

    henningm

    Joined:
    Jul 11, 2016
    Posts:
    18
    Thanks am gonna try it.

    and this is what I got.
    ny card back with info.png
     
    Last edited: Nov 18, 2018
  15. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    try to change: element 0 - border (you can leave it as black, it is just the side from the card), element 1 - back, element 2 - front.

    And you should change the script now, no need for rect transform, if the card is 3d.

    maybe this
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Card : MonoBehaviour
    6. {
    7.     public bool showingBack = false;
    8.     Collider boxCol;
    9.     bool canRotate = false;
    10.     public float rotateSpeed;
    11.     float maxRotate = 0;
    12.  
    13.     void Start ()
    14.     {
    15.         boxCol = GetComponent<Collider>();
    16.     }
    17.  
    18.     private void Update()
    19.     {
    20.         if (Input.GetMouseButtonDown(0) && !canRotate)
    21.         {
    22.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    23.             RaycastHit[] hit = Physics.RaycastAll(ray);
    24.             if (hit != null)
    25.             {
    26.                 foreach (RaycastHit h in hit)
    27.                 {
    28.                     if (h.collider == boxCol)
    29.                     {
    30.                         CardRotate();
    31.                     }
    32.                 }
    33.             }
    34.         }
    35.  
    36.         if (canRotate)
    37.         {
    38.             if (showingBack)
    39.             {
    40.                 transform.Rotate(Vector3.up * -rotateSpeed * Time.deltaTime);
    41.                 maxRotate += rotateSpeed * Time.deltaTime;
    42.                 if (maxRotate >= 180)
    43.                 {
    44.                     maxRotate = 0;
    45.                     transform.eulerAngles = Vector3.zero;
    46.                     canRotate = false;
    47.                     showingBack = !showingBack;
    48.                 }
    49.             } else
    50.             {
    51.                 transform.Rotate(Vector3.up * rotateSpeed * Time.deltaTime);
    52.                 maxRotate += rotateSpeed * Time.deltaTime;
    53.                 if (maxRotate >= 180)
    54.                 {
    55.                     maxRotate = 0;
    56.                     transform.eulerAngles = Vector3.up * 180;
    57.                     canRotate = false;
    58.                     showingBack = !showingBack;
    59.                 }
    60.             }
    61.         }
    62.     }
    63.  
    64.     void CardRotate ()
    65.     {
    66.         canRotate = true;
    67.     }
    68. }
    sorry, I just don't know what the type of rotating you are needing :D

    if you don't like 20000 scale, you can select imported card, go to model tab and change scale factor (don't forget to reset the collider on the prefab in this case).
     
    Last edited: Nov 18, 2018
  16. henningm

    henningm

    Joined:
    Jul 11, 2016
    Posts:
    18
    It's working now, execpt for picture face.
    I have no idea how to fix that.

    Order in layer or/and additional shader, or/and blocking object, blocking mask, or/and sorting layer ?
    I hope you can help me.
     
  17. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    try to put your "Back" material in all 3 slots. If the problem stays then I have did something wrong with normals (reverted face).
    you can try this other card
    http://xrayisgray.de/packages/CardTwo.fbx
     
    Last edited: Nov 18, 2018
  18. henningm

    henningm

    Joined:
    Jul 11, 2016
    Posts:
    18
    I thank you for contribution yours, but I think it over and it's not necessary to have it.