Check distance between Player and GameObject

Hi all!

What I’m trying to do is a script that plays the animation of my door opening when the player is near to it.

The script is really simple, from Debug.Log i can see that it works, but it won’t play animation.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OpenDoor : MonoBehaviour
{
     public GameObject Door_L;
     public GameObject Door_R;
     float Distance1;
     float Distance2;
     bool Open = false;
     bool Closed = true;
     // Use this for initialization
     void Start()
     {
     }
     // Update is called once per frame
     void Update()
     {
         Distance1 = Vector3.Distance(Door_L.transform.position, this.transform.position);
         Distance2 = Vector3.Distance(Door_R.transform.position, this.transform.position);
         Debug.Log("Distance between obj1 and obj2 is " + Distance1 + " or " + Distance2);
         if ((Distance1 < 10.0f || Distance2 < 10.0f) && Closed)
         {
             Door_L.GetComponent<Animation>().Play("Take 001");
             Door_R.GetComponent<Animation>().Play("Take 001");
             Open = true;
             Closed = false;
         }
     }
}

Can you help me?

Thanks in advance!

I can’t tell you what you need to do to make it work with the Legacy animation system you’re using, but I can tell you how to make it work with the Mecanim one (which you probably should be using anyway).

By the way - you should also put a Debug.Log right before and after the animation, so you can see what gets called and what isn’t.

  • Add an Animator Controller to the door

  • Add states for ‘closed’, ‘opening’ and ‘open’ with your animations

  • Make transitions between the states and make closed → opening activate on trigger, e.g. ‘openTrigger’ and opening → open when finished

  • From your script instead of Animation.Play you want to call Animator.SetTrigger(“openTrigger”)

You can read more here:

Especially read the last paragraph, since that’s what you’re using.

Thanks for your help Zephus :slight_smile: I’ll certainly read and try to follow your advice.

By the way I’m so stubborn I’d like to know why my script isn’t working. I’ve tried with Debug.Log before and after the animation, and all seems fine. No errors in console, and if i check “Play automatically” on the animation component it works, so the animation is ok (at least I think it is).
I just can’t play that animation, don’t know why :frowning:

The solution is to mark the animation as Legacy. But I assure you - you don’t really want to do this. You’ll want to use Animator Controllers. That’s been the way to animate things in Unity for years now.

The way to mark something as Legacy is pretty hidden because it’s not supposed to be done like this anymore. But if you absolutely must:

  • Click on the Animation in the Project window
  • In the top right corner of the Inspector (to the right of the lock) is a dropdown menu. Select ‘Debug’
  • Enable Legacy

It should work then. It has nothing to do with your code. It’s just that Unity doesn’t operate like that anymore.

I marked it as Legacy, Unity gave me an error before I did, so i was forced to do that… but still it isn’t working :confused:

Does it work if you put nothing but

private void Update()
{
     GetComponent<Animation>().Play("New Animation");
}

in a script directly on the door?

Because I just created an object with that script, made an animation that scales it up, put just the animation on the object and now it plays continuously.

I’m thinking that either your if clause just doesn’t fire or the animation is set up in a way that it gets reset every Update, so it looks like it never starts.

Attaching the script to the door it works. Maybe the problem is that the script can’t move the door if it’s attached to the player?