how to make a door close for ever after a player pass through it
hi there. im using two scripts. first one is at the door:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class doorscript : MonoBehaviour
{
public bool open = false;
public float DoorOpenAngle = 90f;
public float DoorCloseAngle = 0f;
public float smooth = 2f;
// Use this for initialization
void Start()
{
}
public void OnTriggerEnter(Collider other)
{
open = false;
}
public void ChangeDoorState()
{
open = !open;
}
// Update is called once per frame
void Update()
{
if (open)
{
Quaternion targetRotation = Quaternion.Euler(0, DoorOpenAngle, 0);
transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, smooth * Time.deltaTime);
}
else
{
Quaternion targetRotation2 = Quaternion.Euler(0, DoorCloseAngle, 0);
transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation2, smooth * Time.deltaTime);
}
}
}
the second is for ray casting at the character camera
using UnityEngine;
using System.Collections;
public class interactscriptt : MonoBehaviour
{
public float InteractDistance = 5f;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, InteractDistance))
if (hit.collider.CompareTag("Door"))
{
hit.collider.transform.GetComponent<doorscript>().ChangeDoorState();
}
}
}
}
i want to do it at triggerenter if possible. i tried to do it but i couldnt.
thank you so much in advance
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612094018im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Opening door script in C# 3 Answers
How to open and close a door 0 Answers
Need help with this code? thanks. 2 Answers
How to specifically arrange only 'Mesh Collider' to be hit by Raycast? 0 Answers