closig door after charecter passing
code for 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);
}
}
}
code for ray casting at the character. 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 managed to close the door after the player has passed but couldn't keep it closed. thanks a lot
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612094018im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Open Door Animation Problem 0 Answers
How to make an enter building button in a 2D game? 0 Answers
how to make a door close for ever after a player pass through it 0 Answers
How do I not set a trigger to happen? 1 Answer
How to get "on trigger enter"( or exit) to only react to a specific tag 1 Answer