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!
Comment
maybe the animation is playing but in different layer and you cannot see it...
Your answer
![](https://koobas.hobune.stream/wayback/20220612115917im_/https://answers.unity.com/themes/thub/images/avi.jpg)