Open door problem

Hello I need help my script open door but open all door in scene pls help my

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

public class DoorCar : MonoBehaviour
{
   
    public float y;
    public float x;
    public float z;
    // Start is called before the first frame update
    void Start()
    {
        x = this.gameObject.transform.eulerAngles.x;
        z = this.gameObject.transform.eulerAngles.z;
    }

    // Update is called once per frame
    void Update()
    {
      

        if (transform.eulerAngles.y >70)
        {
            y = 0;

        }
        if (transform.eulerAngles.y < 70)
        {
            y = 90;

        }

        if ((Input.touchCount > 0) && (Input.GetTouch(0).phase == TouchPhase.Began))
        {
            RaycastHit hit = new RaycastHit();
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.GetTouch(0).position), out hit))
            {
                if (hit.collider.gameObject.CompareTag("Door"))
                {
                  
                    this.gameObject.transform.rotation = Quaternion.Euler(x, y, z);
                   
                  
                }


            }

        }
    }
}

So basically, each door performs a raycast from the camera; if the raycast hits anything tagged “Door”, then the one performing the racyast opens (which, as a reminder, is every door). Line 40 should be checking not whether the raycast hit any door, but whether the raycast hit this door:

if (hit.collider.gameObject == gameObject) {

This is not your current problem but probably will be a problem eventually: checking against Euler angles as on lines 24 & 29 is just asking for trouble, because Euler angles are terrible. They’re useful for typing in rotations (so line 43 is fine) but anything else is bad.

Thanks verry much i write y and x and z manually

I also want to point out that every door in your scene is doing a (duplicate) raycast for every touch. It would be much more efficient to have a single controller script that does only a single raycast and then allows the object hit by that raycast to react to it.

1 Like

sorry I am a beginner i have it

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

public class DoorCar : MonoBehaviour
{

    public float y;
    public float x;
    public float z;
    public bool openclosedoor;
    // Start is called before the first frame update
    void Start()
    {
      
    }

    // Update is called once per frame
    void Update()
    {


        if (openclosedoor == true)
        {
            this.gameObject.transform.rotation = Quaternion.Euler(x, y, z);

        } else

            this.gameObject.transform.rotation = Quaternion.Euler(x, 0, z);

   

        if ((Input.touchCount > 0) && (Input.GetTouch(0).phase == TouchPhase.Began))
        {
            RaycastHit hit = new RaycastHit();
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.GetTouch(0).position), out hit))
            {
              
                    if (hit.collider.gameObject == gameObject)
                    {

                    openclosedoor = !openclosedoor;

                   


                }

            }
        }
    }
}