Search Unity

Animation effects all instances - help!

Discussion in 'Animation' started by CodeNashor, Oct 14, 2014.

  1. CodeNashor

    CodeNashor

    Joined:
    Jan 6, 2014
    Posts:
    18
    Hy guys!

    I have a door which has some animations (open, close) and i made the door as a prefab.
    A script will trigger the animation, if a character is pressing 'E' close to it. It works fine, but if i instance more doors and put the same script on it, all doors playing the animation if just one door is triggered.

    It also not work if i break the prefab instance. I just got one workaround: multiple prefabs with the same door (multiple equal fbx file in assets (door1.fbx,door2.fbx,door3.fbx,..) ).

    It does also not work, if i add more animation clips on the door prefab and playing different animation clip on each door (door1_open, door2_open, door3_open).

    Is there a way to break the connection between instances or make them 'standalone'? It looks like they have still a connection to the prefab / the animation effects all instances of a prefab. Please help!

    PS: If I do Debug.Log(this.gameobject.name), it gives me the name of the prefab and not the name of the gameobject (all instances of it have the same name, even if a change the gameobject names AND breaking the prefab connection. Seems the Connection is still alive)
     
  2. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Looks like you've trouble shot it pretty well.

    - I'm not a programmer so this is probably wrong, but could it be because it's the same script on all the doors?

    - Or possibly since debug.log lists name of the prefab not the name of game object maybe apply the script to the game object then create the prefab, instead of creating the prefab then apply the script. ?? I'm just guessing now.

    Lemme know if you figure it out. I'm interested.
     
  3. CodeNashor

    CodeNashor

    Joined:
    Jan 6, 2014
    Posts:
    18
    That could be a thing, but i dont want to create 1000 scripts for 1000 doors, that can't be the right way. ;)

    Through the script, I trigger a door to play 'open'. It's disconnected from the prefab, but all doors from the same prefab playing the animation.

    Also different animation clip names, e.g. 'door1' has the animation clip 'open1', 'door2' has the animation clip 'open2' and i just trigger at door1 the animation clip name 'open1', all instances of the prefab trying to play the animation clip (getting errors for the other doors, because they have no animation clip name called 'open1' or something). Thats kind of weird, because they have no connection to each other.

    The prefab (the door) is a .fbx file, i made it in a other 3D Software. So its automatically a prefab with animation clips in it, nothing more.
     
    Last edited: Oct 14, 2014
  4. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Completely understand the horror of making 1000 scripts for 1000 doors. That certainly isn't the correct answer.
    So when you import the fbx it comes with a take001 right?
    I'm thinking since the animation clip is being activated,,, umm nevermind.

    Well since I started to help you out I will try and help more. :)
    So check these out.

    http://unity3d.com/learn/tutorials/projects/stealth/single-doors
    http://answers.unity3d.com/questions/294194/how-to-make-a-solid-door-open-and-close.html


    Lemme know if one of these helps you resolve your problem. Though Im no coder I like to have solid reference material for specific general tasks.
     
  5. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    It hard to know without the code but normally you clone the prefab instance in your scene with Object.Instantiate()
    http://docs.unity3d.com/ScriptReference/Object.Instantiate.html
    Each new instance should have their own object 'state' and their name should be something like 'original name (Clone)'

    If you are already using Object.Instantiate then the next step will be to share your project with us so we can take a look.
     
    theANMATOR2b likes this.
  6. CodeNashor

    CodeNashor

    Joined:
    Jan 6, 2014
    Posts:
    18
    I just gave up and doing it with duplicated prefabs. I can't figure it out. Thanks all!
     
  7. organdrew

    organdrew

    Joined:
    Jun 28, 2017
    Posts:
    5
    I know this thread is old but if anyone runs into this problem like I did, I have found a solution. I've been searching for an online solution myself but yielded no concrete answers. Luckily I solved it myself.

    Basically you need to get the transform of the gameObject that was activated it and compare it to the gameObject's actual position and play the animation only if the transforms are the same. This way only the object that was clicked on will play the animation.

    Here is my function that does that:

    Code (CSharp):
    1.  void OnMouseOver()
    2.     {
    3.         //If left mouse click, send a ray directly forward in scene
    4.         if (Input.GetMouseButtonDown(0))
    5.         {
    6.             RaycastHit hit;
    7.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    8.  
    9.             //If raycast out hits an object and that object has a capsule collider, destroy that object
    10.             if (Physics.Raycast(ray, out hit))
    11.             {
    12.                 CapsuleCollider cc = hit.collider as CapsuleCollider;
    13.  
    14.                 //If CapsuleCollider is not null and the hit object's transform is the same as the gameObject's transform
    15.                 if (cc != null && hit.transform == gameObject.transform)
    16.                 {
    17.                     Destroy(cc.gameObject);
    18.                     Instantiate(explosion, transform.position, transform.rotation);
    19.                 }
    20.             }
    21.  
    22.         }
    23.     }
    What's important here is this line of code:

    if (cc != null && hit.transform == gameObject.transform)

    In my code, I'm destroying a gameObject that is clicked on and playing an explosion VFX.

    What we're doing here is on left mouse click ( Input.GetMouseButtonDown(0) ), send out a ray. If this ray hits an object with a capsule collider, get the transform of the gameObject that it hit and compare it to this gameObject's transform. If they're the same, then destroy and play animation.

    Depending on how you access the object, you'd need to do something like

    "if (whatWeActivated.transform == gameObject.transform)"

    Where whatWeActivated is an instance of the transform of the gameObject.


    Cheers
     
    Ularis likes this.