1
\$\begingroup\$

I have two scripts and an animator. The first script is attached to an empty gameobject and creates eight plane (for use as cards). The second script runs a flipping animation if the plane/cards are click.

Card Init

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class cardInit : MonoBehaviour {

    List<Texture> cardFront = new List<Texture>();
    List<GameObject> cardList = new List<GameObject>();

    public GameObject newCard;

    Vector3 startPos;
    Vector3 endPos;

    // Use this for initialization
    void Start () {

        startPos = new Vector3(-10, 0, 0);
        Vector3 left = new Vector3(3 ,0 ,0);

        for (int i = 0; i <= 7; i++)
        {        
            cardList.Add(GameObject.Instantiate(newCard,startPos,Quaternion.Euler(90,0,0)));
            startPos += left;
        }
    }

    // Update is called once per frame
    void Update () {

    }
}

Card Flip

using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;

public class cardScript : MonoBehaviour {

    private Animator animator;

    // Use this for initialization
    void Start ()
    {
       // animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                //gameObject.animator.SetInteger("onMouse", 1);
                gameObject.GetComponent<Animator>().SetInteger("onMouse", 1);
            }
        }
    }
}

However, since the instantiated objects all uses the same animator, changing the animator parameter to flip a single card causes all the cards to flip.

I believe the issue lies with the instantiated objects all referencing a singular animator rather than initialising a copy of its own. Is there a way to rectify it so each card only flips itself on click?

enter image description here

\$\endgroup\$
1
  • \$\begingroup\$ Wouldn't it be better to attach a script, responsible for the flip animation, to card prefab and run it on the clicked card? Btw, is the Animator attached to the empty gameObject or the card prefab? \$\endgroup\$ Apr 27 '17 at 10:41
2
\$\begingroup\$

The issue is not that you are sharing the same instance of the Animator. Each card has its own instance. The problem is that you are not checking what is being hit by your raycast:

    if (Input.GetMouseButtonDown(0))
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))
        {
            // Here you are not doing any sort of check
            // The code below will fire for every single card on scene
            gameObject.GetComponent<Animator>().SetInteger("onMouse", 1);
        }
    }

What you want to do is check to see what card was clicked, and then set the animator of that card only:

    if (Input.GetMouseButtonDown(0))
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))
        {
            // Check if you hit this object
            if (hit.collider == gameObject.GetComponent<BoxCollider>())  
            {
                gameObject.GetComponent<Animator>().SetInteger("onMouse", 1);
            }
        }
    }

Although a much better way would be to have some sort of "CardManager" that would handle the click event across all cards.

\$\endgroup\$

You must log in to answer this question.

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