Make A Light Switch Or Button In Unity3D using Triggers (script below)

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 ม.ค. 2025

ความคิดเห็น • 19

  • @bigbirdgames6659
    @bigbirdgames6659  3 ปีที่แล้ว +20

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class lightonoff : MonoBehaviour
    {
    public GameObject txtToDisplay; //display the UI text

    private bool PlayerInZone; //check if the player is in trigger
    public GameObject lightorobj;
    private void Start()
    {
    PlayerInZone = false; //player not in zone
    txtToDisplay.SetActive(false);
    }
    private void Update()
    {
    if (PlayerInZone && Input.GetKeyDown(KeyCode.F)) //if in zone and press F key
    {
    lightorobj.SetActive(!lightorobj.activeSelf);
    gameObject.GetComponent().Play();
    gameObject.GetComponent().Play("switch");
    }
    }
    private void OnTriggerEnter(Collider other)
    {
    if (other.gameObject.tag == "Player") //if player in zone
    {
    txtToDisplay.SetActive(true);
    PlayerInZone = true;
    }
    }

    private void OnTriggerExit(Collider other) //if player exit zone
    {
    if (other.gameObject.tag == "Player")
    {
    PlayerInZone = false;
    txtToDisplay.SetActive(false);
    }
    }
    }

    • @TrMx
      @TrMx 3 ปีที่แล้ว

      .

    • @tamchugg5431
      @tamchugg5431 ปีที่แล้ว +1

      real for this

  • @Saud.alh911
    @Saud.alh911 3 ปีที่แล้ว +2

    THANK U SO MUCH!!!!!!!!!! THIS HELPED A LOTTTTTTTTT

  • @LUFFY-bg8vy
    @LUFFY-bg8vy ปีที่แล้ว +2

    DUDE U R THE BEST

  • @iam_river
    @iam_river ปีที่แล้ว +1

    my player is just a camera since im doing a fnaf style game, how can i get it to have a collider? Because its not working right now. Also my player tag is set.

    • @bigbirdgames6659
      @bigbirdgames6659  ปีที่แล้ว

      Just add a empty gameobject with a collider to the camera

  • @userlillu
    @userlillu 3 ปีที่แล้ว +4

    very helpful, ty !

  • @zerbezy7723
    @zerbezy7723 3 ปีที่แล้ว +1

    thank you so much!!!!

  • @blumpflerdinko3565
    @blumpflerdinko3565 ปีที่แล้ว

    Hey everything seems to be working but the player cannot enter the hitbox of the button?

    • @blumpflerdinko3565
      @blumpflerdinko3565 ปีที่แล้ว

      oof, forgot to click is trigger.. but now the button doesent stop clickinG

    • @bigbirdgames6659
      @bigbirdgames6659  ปีที่แล้ว

      Try instructions again

    • @bigbirdgames6659
      @bigbirdgames6659  ปีที่แล้ว

      Make sure player tag is set, and your using the script I provided

  • @Robocal
    @Robocal 2 ปีที่แล้ว

    Just wondering how would I make it to have more than one lightorobj I want to have more than one light all connected to one switch?

    • @Robocal
      @Robocal 2 ปีที่แล้ว +1

      Never mind just found out how to but thanks so much :))))

    • @ebenkotke6184
      @ebenkotke6184 ปีที่แล้ว

      @@Robocal ik this was long ago but how>????

    • @Robocal
      @Robocal ปีที่แล้ว +1

      @@ebenkotke6184 i think i just made another public game object and called it light 2 or something. i did this a very long time ago tho so dont take my word on that

    • @thepinkpanther249
      @thepinkpanther249 ปีที่แล้ว

      @@ebenkotke6184 You could just use a simple empty object, and put all lights as it's child. That way if you set that gameObject to false, all lights will go out.

  • @Batoki744
    @Batoki744 ปีที่แล้ว

    TY SO MUCH! THE gameObject.GetComponent().Play("switch"); is all what i need this whole time! Much love!!!♥