#63 - Door script for Unity
Date: 2019-06-01 12:00 - c#
Simple door script for unity that rotates a game object around a transform.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Door : Interactable {
public Transform doorSocket;
public bool isOpen = false;
public bool isMoving = false;
private Quaternion initialRotation;
void Start() {
transform.parent = doorSocket;
}
void Update() {
if (isMoving) {
Quaternion targetRotation;
if (isOpen)
targetRotation = Quaternion.Euler(0.0f, -90.0f, 0.0f);
else
targetRotation = Quaternion.Euler(0.0f, 0.0f, 0.0f);
doorSocket.localRotation = Quaternion.RotateTowards(doorSocket.localRotation, targetRotation, 180.0f * Time.deltaTime);
if (Vector3.Distance(targetRotation.eulerAngles, doorSocket.localRotation.eulerAngles) < 1)
isMoving = false;
}
}
public override void OnInteract(GameObject caller) {
if (!isMoving) {
isOpen = !isOpen;
isMoving = true;
}
}
}