This is the scene. If you need to know anything else, just tell me.
The object I selected, is the button, obviously.
Though, I wanted to let the button open the door, but right now only if I look at the door and push F the door opens, I can only close the door by using the button, but I can’t open it with it.
The Code is the following:
using UnityEngine;
using System.Collections;
public class Doors : MonoBehaviour
{
public float timeleft = 0;
public RaycastHit hit;
public Transform currentdoor;
public bool open;
public bool IsOpeningDoor;
public Transform cam;
public LayerMask mask;
void Update()
{
if (Input.GetKeyDown(KeyCode.F) && timeleft <= 0f)
CheckDoor();
if (IsOpeningDoor)
OpenAndCloseDoor();
}
public void CheckDoor()
{
Debug.Log(Time.frameCount + ": Pressed F");
if (Physics.Raycast(cam.position, cam.forward, out hit, 5, mask))
{
Debug.Log(Time.frameCount + ": Pressed F and closed");
open = false;
if (hit.transform.localRotation.eulerAngles.z > 50)
open = true;
IsOpeningDoor = true;
currentdoor = hit.transform.gameObject.GetComponent<Schaltermich>().doorluke.transform;
Debug.Log(Time.frameCount + ": Hit a " + mask + " (" + currentdoor + ")");
}
}
public void OpenAndCloseDoor()
{
timeleft += Time.deltaTime;
if (open)
currentdoor.localRotation = Quaternion.Slerp(currentdoor.localRotation, Quaternion.Euler(0, 0, 0), timeleft);
else
currentdoor.localRotation = Quaternion.Slerp(currentdoor.localRotation, Quaternion.Euler(0, 0, 90), timeleft);
if (timeleft > 1)
{
timeleft = 0;
IsOpeningDoor = false;
}
}
}
Shaltermich shaltermich = hit.transform.GetComponent<Shaltermich>();
// does the object have the component, and also check that doorluke is not null (if it can be)
if(shaltermich != null && shaltermich.doorluke != null){
currentdoor = shaltermich.doorluke.transform
}
open = true;
IsOpeningDoor = true;
currentdoor = hit.transform.gameObject.GetComponent<Schaltermich>().doorluke.transform;
Schaltermich schaltermich = hit.transform.GetComponent<Schaltermich>();
// does the object have the component, and also check that doorluke is not null (if it can be)
if (schaltermich != null && schaltermich.doorluke != null)
{
currentdoor = schaltermich.doorluke.transform;
}
Debug.Log(Time.frameCount + ": Hit a " + mask + " (" + currentdoor + ")");
}
}
We all have to start somewhere, however, not much else we can offer. With your current code you need to figure out what is null. That really is the only thing to do unless you want to rewrite your code to something else.