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

[Problem] Slider Door/Door Collider

Discussion in 'Scripting' started by BamBam13, Oct 24, 2014.

  1. BamBam13

    BamBam13

    Joined:
    Oct 24, 2014
    Posts:
    1
    Hey guys,
    I?m sorry about that bad question but I got problem with easy door opening while useing Collider as triggers.

    I Set my Scene and placed a few boxes for Walls and so on. Named my Door "Door", set a Collider Box scaled much bigger arround it (Mesh Render is deactivatet and the trigger is on). Next I started animating my door with easy keyframes. I placed the animation onto my "Door" Object and it is working but it is looping the whole time too (removed the button in the automaticly option). I do not have to stay inside of the trigger that the animation for the door slide is working. It just starts and repeats the whole time.

    I took some javascript from a video (tutorial shown up)

    This is the Script I used onto the Door

    using UnityEngine;
    using System.Collections;

    [AddComponentMenu("Tamer/Prefabs/DoorSlide1")]

    public class DoorSlide1 : MonoBehaviour {

    public float speed = 1.0F;
    public float slideDistance = 1.0F;

    private Vector3 slideBegin, slideEnd;
    private Transform door;
    private bool canMove = false;

    // Use this for initialization
    void Awake () {
    door = transform.GetChild (0);
    slideBegin = door.localPosition;
    slideEnd = slideBegin;
    slideEnd.x = slideBegin.x + slideDistance;
    }

    // Update is called once per frame
    void Update () {
    if (canMove)
    DoorSlide (slideEnd);
    else
    DoorSlide (slideBegin);
    }

    void OnTriggerEnter (Collider other) {
    if (other.tag == "Player") canMove = true;
    }

    void OnTriggerExit (Collider other) {
    if (other.tag == "Player") canMove = false;
    }

    void DoorSlide (Vector3 targetPos) {
    door.localPosition = Vector3.Lerp(door.localPosition, targetPos, speed * Time.deltaTime);
    }
    }



    May somebody can help?