Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

AR Colliders

Discussion in 'AR/VR (XR) Discussion' started by lhocraffer, Nov 8, 2015.

  1. lhocraffer

    lhocraffer

    Joined:
    Nov 8, 2015
    Posts:
    10
    I'm working on an AR project, and I'm trying to have the models interact with each other. I am using Vuforia and currently have 2 functioning models that are animated. I want the models to change animation when they are within a certain proximity of each other. My current approach is to use colliders and have a script that changed the animation when the collider is triggered. Below is my current script. I've tried everything I can think of, but it seems like my colliders aren't doing anything. Any help would be greatly appreciated!

    using UnityEngine;
    using System.Collections;
    using System;

    public class LRH_collider_script : MonoBehaviour {

    void OnTriggerEnter(Collider other)
    {
    GetComponent<Animation>().Play("New Animation2");
    }

    void OnTriggerStay(Collider other)
    {
    GetComponent<Animation>().Play("New Animation2");
    }

    }
     
  2. SniperEvan

    SniperEvan

    Joined:
    Mar 3, 2013
    Posts:
    161
    Are the colliders set to "is trigger" in the inspector? If not you should use the function OnCollisionEnter
     
  3. lhocraffer

    lhocraffer

    Joined:
    Nov 8, 2015
    Posts:
    10
    The colliders are set to "is trigger". So i think that's the right function correct?
     
  4. SniperEvan

    SniperEvan

    Joined:
    Mar 3, 2013
    Posts:
    161
    I don't think two trigger objects collide with each other by default in Unity. You might be able to change this in the physics settings but I've never checked.

    http://answers.unity3d.com/questions/218366/do-2-triggers-collide.html

    As a test, un-check isTrigger on one of them and see if they have the right behaviour. Then we will know if that is the issue or not.
     
  5. lhocraffer

    lhocraffer

    Joined:
    Nov 8, 2015
    Posts:
    10
    I tried that, only having one model have is trigger checked, and still no improvement. I think the problem might be with my animations. I'm using the animation tab to create the animations for each model. I'm not sure if I should have apply root motion checked?
     
  6. DrBlort

    DrBlort

    Joined:
    Nov 14, 2012
    Posts:
    72
    If you only need the proximity as condition to change the animations, why not use a simple distance check?

    Would be something like this:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class LRH_collider_script : MonoBehaviour
    4. {
    5.     public Transform other;
    6.     public float distance = 5.0f;
    7.  
    8.     void Update()
    9.     {
    10.  
    11.         if ((transform.position - other.position).sqrMagnitude > distance * distance)
    12.         {
    13.             GetComponent<Animation>().Play("New Animation1");
    14.         }
    15.         else
    16.         {
    17.             GetComponent<Animation>().Play("New Animation2");
    18.         }
    19.     }
    20. }
    I don't know if applying Play every frame would reset the animations, but in that case you can use flags to use Play only once, until the other condition happens.

    BTW, you should preferably cache the Animation component in a member variable, as it has some runtime cost.
     
    gibbrish123 likes this.
  7. Omnifinity

    Omnifinity

    Joined:
    Nov 3, 2015
    Posts:
    17
    Make sure that you have attached a RigidBody to one of the objects with the Colliders.
     
    reddtoric and solkar like this.
  8. solkar

    solkar

    Joined:
    Aug 15, 2012
    Posts:
    38
    I was going to suggest exactly the same. :D
     
  9. lhocraffer

    lhocraffer

    Joined:
    Nov 8, 2015
    Posts:
    10
    Sorry, i'm new to unity. Does a collider that is not checked as "is trigger" count as a rigid body? Or do I need to attach a different object that is a rigid body? Thanks for the response!
     
  10. lhocraffer

    lhocraffer

    Joined:
    Nov 8, 2015
    Posts:
    10
    Sorry, i'm new to unity. Does a collider that is not checked as "is trigger" count as a rigid body? Or do I need to attach a different object that is a rigid body? Thanks for the response!
     
  11. SniperEvan

    SniperEvan

    Joined:
    Mar 3, 2013
    Posts:
    161
    A rigid body is a different component.
     
  12. lhocraffer

    lhocraffer

    Joined:
    Nov 8, 2015
    Posts:
    10
    Thank you for the answer, I put in a rigid body and it almost works now, haha! It now throws an error when the collider and rigid body interact so thats progress!
    My error is that there is not animation attached to my game object, so I guess I have a problem with how my animation is set up. But the colliders seem to be working!
     
    Omnifinity likes this.
  13. SniperEvan

    SniperEvan

    Joined:
    Mar 3, 2013
    Posts:
    161
    Thats great! If you are using the mecanim system (recommended) than you have to:
    1. Set up an animator
    2. Get a reference to it in your script and
    3. Pass a parameter to it so it can transition between states

    There are lots of tutorials online for this. Let me know if you need more info!

    -E
     
  14. lhocraffer

    lhocraffer

    Joined:
    Nov 8, 2015
    Posts:
    10
    I am not using mecanim, at least I don't think I am, but I'm sure I'll be able to find a tutorial/solution. I'm doing really basic animations in the animations tab and creating curves. I'm moving the whole model, not having a skeleton rig or anything.
     
  15. SniperEvan

    SniperEvan

    Joined:
    Mar 3, 2013
    Posts:
    161
    K. Good luck. If you have any more questions I'll be here.
     
  16. lhocraffer

    lhocraffer

    Joined:
    Nov 8, 2015
    Posts:
    10
    alright, I do have more questions. I've been looking at animations and tutorials and I'm still not sure why my code won't work. Below is what I'm doing, do I need to call the animator somehow/where?

    void OnTriggerEnter(Collider other)
    {
    GetComponent<Animation>().Play("New Animation2");
    }
     
  17. SniperEvan

    SniperEvan

    Joined:
    Mar 3, 2013
    Posts:
    161
    If you have an animator set up then yeah, you will need to get a reference to it like:
    Animator anim = getComponent <Animator>()

    Then you could do anim.changeState, or anim.SetParameter to enter the desired animation state. (I'm not sure what the exact method names are but you can find them by googling).

    Do you have an Animator component on the game object?
    If your doing it workout an animator I don't remember the correct procedure but I could look it up in the morning.
     
  18. lhocraffer

    lhocraffer

    Joined:
    Nov 8, 2015
    Posts:
    10
    I do have an animator on the game object and several animation clips created. I'll try adding the reference in the morning/afternoon, thanks so much for the continued help!
     
  19. lhocraffer

    lhocraffer

    Joined:
    Nov 8, 2015
    Posts:
    10
    Just wanted to let anyone who is still following that I was able to get the collision to cause animation change. This tutorial and the related ones were extremely helpful, along with the tips I got from these comments. Thanks for the help!
    https://unity3d.com/learn/tutorials/modules/beginner/animation/animator-scripting

    For the curious, here is my script.

    public class LRH_collider_script : MonoBehaviour
    {
    Animator anim;
    int attackHash= Animator.StringToHash("attack");
    int idleHash = Animator.StringToHash("idle");

    void OnTriggerEnter(Collider other)
    {
    Debug.Log("Object Entered the trigger***********************************************************");
    anim = GetComponent<Animator>(); //initialized variable anim and gets component
    anim.SetTrigger(attackHash); //from https://unity3d.com/learn/tutorials/modules/beginner/animation/animator-scripting
    }

    void OnTriggerExit (Collider other)
    {
    Debug.Log("Object Exited the trigger##################################################");
    anim.SetTrigger(idleHash);
    }
    }