Door script not working!! Help :(

Trying to open a door by y access but it wont work i found the guidance to do so on a youtube video i will post the video if anyone is interseted in the bio. But i think the only thing i did wrong is i want to use the empty game object to the far left of game object, And that and clearly each error i get is only over symbol ‘timeleft’ But im completely new to this so i have no idea how to possibly understand what that means i need to do without a professinal of some sorts o,o
Errors______________

  1. Assets/Doors.cs(48,110): error CS1525: Unexpected symbol `timeleft’

  2. Assets/Doors.cs(50,111): error CS1525: Unexpected symbol `timeleft’


Code________
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

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;

// Update is called once per frame
void Update() {
if (Input.GetKeyDown (KeyCode.F) && timeleft == 0.0f)
CheckDoor();
}

public void CheckDoor()
{
if (Physics.Raycast(cam.position, cam.forward, out hit, 5, mask))
{
print(hit.collider.gameObject.name);
open = false;
if(hit.transform.localRotation.eulerAngles.y > 45)
{
open = true;
}
IsOpeningDoor = true;
currentdoor = hit.transform;
}
}

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, 90, 0) timeleft);

if (timeleft > 1)
{
timeleft = 0;
IsOpeningDoor = false;
}
}
}


But thank you for making the software free to use and everything!! :slight_smile: But heres the video

Video i was learning form_________

By Gamad


The compiler tells you the error and what lines - you’re missing commas in your Quaternion.Slerp right before timeleft.

Also please use [ code ] [/ code ] tags in the future.

1 Like

I’m sorry, but this is a very, very bad tutorial on how to open and close a door, and how to learn basic programming techniques, and how to properly use MonoBehaviours in Unity.
I cannot stress how bad of a tutorial this is.
It should be removed from YouTube.

  1. The class should be Door, not Doors.
  2. “currentdoor” should not be needed. (see #6 below)
  3. OpenAndCloseDoor() function naming makes little sense. The door cannot open and close at the same time.
  4. The door should be done moving when it has moved a certain amount of degrees, not based on how much time has elapsed, just like in real life. Reading and analyzing euler angles will get you in trouble at some point.
  5. You need to think in terms of a state machine. The door has multiple possible states: Opened, Closing, Closed, Opening.
    Rather than reading the euler angles of doors to determine if it is opened, let the Door script keep a state. But, you can’t do this the way the script is designed (see below)
  6. Attaching this door script to the ground gameobject makes no sense. The scripts should be attached to the door. Each door has its own script. Each door controls and remembers its state.

Maybe there are other tutorials you can follow. But this one will teach you very bad practices of creating scripts for game objects in Unity.

1 Like

Agreed, simple naming.

I havent seen the tutorial but yes that name is awful. I expect a OpenDoor and CloseDoor would be better.

camelCase for variables!

EDIT:
Just checked the poll and what the… the answers dont make sense and the poll is useless.
Either too young, or a non-english speaker.