How to open and close a door
I'm making some kind of a door script but it's not working, I've tried with both Lerp and Slerp but it just jumps to that location and doesn't come back. Can you help?
using System.Collections;
using UnityEngine;
public class Door : MonoBehaviour
{
public Transform fpsCam;
Vector3 startPos = new Vector3();
Vector3 endPos = new Vector3();
bool canOpen = true;
void Start()
{
startPos = transform.position;
endPos = startPos + Vector3.up * 15;
}
void OnMouseDown()
{
if(canOpen && Vector3.Distance(transform.position, fpsCam.position) > 5f)
return;
transform.position = Vector3.Lerp(transform.position, endPos, 0.5f);
canOpen = false;
StartCoroutine(GoDown());
}
IEnumerator GoDown()
{
yield return new WaitForSeconds(3.5f);
transform.position = Vector3.Lerp(transform.position, startPos, 0.5f);
canOpen = true;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613003454im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
missing cinemachine Kit tools tutorial menu 0 Answers
How to Slerp / Lerp Rotation on a Single Axis? 0 Answers
Hiding helper geometry during runtime 4 Answers
TargetPosition of leg spring stuck at 80 0 Answers