Reputation: 11319
Once I'm entering the trigger area the door open.
If I'm staying inside the trigger area after the doors opened after some seconds the door will close on me. But in my logic the doors should be stay open as long as I'm inside the trigger area. The door should be close start closing only when leaving the trigger area.
Another problem is if I'm moving inside/outside the trigger area of the door too fast. Once I'm in the door start open when the door is opened if I will move back out the trigger area fast and move back in very fast again even if the door is still open it will play the open door animation again. I'm not sure how to solve this too fast entering/exiting problem.
I think the logic should be very simple.
Entering the trigger area open the door.
If I'm staying inside the trigger area while the door is opened finished open don't close the door ever until I'm exiting the trigger area then take some seconds and start closing the door.
If the door started closing and I'm then entering the trigger area again open the door at the point where the door is now even if it's in the middle of closing.
If I'm quick very fast entering/exiting the door trigger area keep handle it like in the first two rules. And don't play the open door animation if the door is already finished open and opened already. What if the play is running ?
The Doors Manager script is attached to a empty GameObject:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorsManager : MonoBehaviour
{
public List<HoriDoorManager> _doors = new List<HoriDoorManager>();
private void Start()
{
var doors = GameObject.FindGameObjectsWithTag("Door");
foreach(var door in doors)
{
_doors.Add(door.GetComponent<HoriDoorManager>());
}
}
public void LockDoor(int doorIndex)
{
_doors[doorIndex].ChangeLockState(true);
}
public void UnlockDoor(int doorIndex)
{
_doors[doorIndex].ChangeLockState(false);
}
}
The script Hori Door Manager is attached to the door child name Horizontal_Doors_Kit:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class HoriDoorManager : MonoBehaviour
{
private bool doorLockState;
private List<DoorHori> doors = new List<DoorHori>();
private void Start()
{
if (transform.parent != null)
{
Transform parent = transform.parent;
var children = parent.GetComponentsInChildren<Transform>();
if(children != null)
{
foreach (Transform door in children)
{
if (door.name == "Door_Left" || door.name == "Door_Right")
doors.Add(door.GetComponent<DoorHori>());
}
}
}
}
void OnTriggerEnter()
{
if (doorLockState == false)
{
if (doors != null)
{
for(int i =0; i < doors.Count; i++)
{
doors[i].OpenDoor();
}
}
}
}
public void ChangeLockState(bool lockState)
{
doorLockState = lockState;
}
}
And the last script Door Hori is attached to each door childs of the door. The door childs name as Door_Keft and Door_Right and they are moving to the sides left and right:
using UnityEngine;
using System.Collections;
public class DoorHori : MonoBehaviour {
public float translateValue;
public float easeTime;
public OTween.EaseType ease;
public float waitTime;
private Vector3 StartlocalPos;
private Vector3 endlocalPos;
private void Start(){
StartlocalPos = transform.localPosition;
gameObject.isStatic = false;
}
public void OpenDoor(){
OTween.ValueTo( gameObject,ease,0.0f,-translateValue,easeTime,0.0f,"StartOpen","UpdateOpenDoor","EndOpen");
GetComponent<AudioSource>().Play();
}
private void UpdateOpenDoor(float f){
Vector3 pos = transform.TransformDirection( new Vector3( 1,0,0));
transform.localPosition = StartlocalPos + pos*f;
}
private void UpdateCloseDoor(float f){
Vector3 pos = transform.TransformDirection( new Vector3( -f,0,0)) ;
transform.localPosition = endlocalPos-pos;
}
private void EndOpen(){
endlocalPos = transform.localPosition ;
StartCoroutine( WaitToClose());
}
private IEnumerator WaitToClose(){
yield return new WaitForSeconds(waitTime);
OTween.ValueTo( gameObject,ease,0.0f,translateValue,easeTime,0.0f,"StartClose","UpdateCloseDoor","EndClose");
GetComponent<AudioSource>().Play();
}
}
The first screenshot showing example of a door structure. And the Horizontal_Doors_Kit Inspector:
The second and last screenshot show one of the Door_Left childs Inspector. The script and settings values are the same on Door_Left and Door_Right:
My character FPSController have a Rigidbody attached a box collider to trigger the door.
Upvotes: 0
Views: 131
Reputation: 27011
But in my logic the doors should be stay open as long as I'm inside the trigger area.
But not in your code logic, your code logic is close the door after the door finishes opening.
private void EndOpen(){
....
StartCoroutine( WaitToClose());
}
You need add OnTriggerLeave
event to close the door.
The second question is related to the first question, so please fix it first.
Upvotes: 1