Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Check distance between Player and GameObject

Discussion in 'Scripting' started by Aaren_13, Apr 16, 2017.

  1. Aaren_13

    Aaren_13

    Joined:
    Apr 14, 2017
    Posts:
    5
    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.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class OpenDoor : MonoBehaviour
    5. {
    6.      public GameObject Door_L;
    7.      public GameObject Door_R;
    8.      float Distance1;
    9.      float Distance2;
    10.      bool Open = false;
    11.      bool Closed = true;
    12.      // Use this for initialization
    13.      void Start()
    14.      {
    15.      }
    16.      // Update is called once per frame
    17.      void Update()
    18.      {
    19.          Distance1 = Vector3.Distance(Door_L.transform.position, this.transform.position);
    20.          Distance2 = Vector3.Distance(Door_R.transform.position, this.transform.position);
    21.          Debug.Log("Distance between obj1 and obj2 is " + Distance1 + " or " + Distance2);
    22.          if ((Distance1 < 10.0f || Distance2 < 10.0f) && Closed)
    23.          {
    24.              Door_L.GetComponent<Animation>().Play("Take 001");
    25.              Door_R.GetComponent<Animation>().Play("Take 001");
    26.              Open = true;
    27.              Closed = false;
    28.          }
    29.      }
    30. }
    Can you help me?

    Thanks in advance!
     
  2. Zephus

    Zephus

    Joined:
    May 25, 2015
    Posts:
    361
    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:
    https://docs.unity3d.com/Manual/AnimationOverview.html

    Especially read the last paragraph, since that's what you're using.
     
  3. Aaren_13

    Aaren_13

    Joined:
    Apr 14, 2017
    Posts:
    5
    Thanks for your help Zephus :) 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 :(
     
    Last edited: Apr 16, 2017
  4. Zephus

    Zephus

    Joined:
    May 25, 2015
    Posts:
    361
    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.
     
  5. Aaren_13

    Aaren_13

    Joined:
    Apr 14, 2017
    Posts:
    5
    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 :/
     
  6. Zephus

    Zephus

    Joined:
    May 25, 2015
    Posts:
    361
    Does it work if you put nothing but
    Code (CSharp):
    1. private void Update()
    2. {
    3.      GetComponent<Animation>().Play("New Animation");
    4. }
    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.
     
  7. Aaren_13

    Aaren_13

    Joined:
    Apr 14, 2017
    Posts:
    5
    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?