Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Can any one help me With a script.

Discussion in 'Scripting' started by Demonichero, Nov 30, 2015.

  1. Demonichero

    Demonichero

    Joined:
    Nov 28, 2015
    Posts:
    6
    Hello im new to scripting and am following tutorials and its going good but i need help with something a little more specific Im making a Survival Horror

    What im looking to do is Have and Object (3D Model Fuel can) be a pick up item and destroy its self when picked up.

    I then want to use the Fuel can on a Object (3D Diesel Generator) and have a few things happen
    1. I want a sound to play (which i already have the sound of) of a generator starting up
    2. I then want another sound to play (which i already have the sound of) of a generator running looping
    3. I want to activate a Panel on a nearby object ((Satellite Dish)that needed power from the generator lol) That when clicked will play another audio clip Made to sound like your communicating with someone(That i already have the sound of)

    I'm sure this is actually really simple but ive wasted two days trying to figure it out. Lucky the only otherthing i need to work out to do now is add fuel to a portable generator that will turn a light on and everything elce is done.
    If you can help me ofc i will credit you in the game end credits many thanks.
     
  2. DMSolace

    DMSolace

    Joined:
    Nov 30, 2015
    Posts:
    9
    You can use OverlapSphere or raycasting to get the can. In this example I've set all the items I want the player to be able to interact with to the layer "Interactable".

    Code (CSharp):
    1.     void PlayerInteraction()
    2.     {
    3.         Collider[] hitColliders = Physics.OverlapSphere(transform.position, interactRadius, 1<< LayerMask.NameToLayer("Interactable"));
    4.         for (int i = 0; i < hitColliders.Length; i++)
    5.         {
    6.             if ( hitColliders[i].name == "Fuel Can")
    7.             {
    8.                 //add fuel can to inventory, or use a bool to indicate that you have the fuel can
    9.                 hasFuel = true;
    10.                 Destroy(hitColliders[i].gameObject);
    11.              }
    12.             if(hitColliders[i].name == "Generator" && hasFuel)
    13.             {
    14.                 //play sound here and activate panel
    15.                 enablePanel = true; //use another bool to start a coroutine to play aditional sounds and enable the satelite panel
    16.                  hasFuel = false;
    17.             }
    18.          }
    19. }
    The audio stuff is easy enough to look up, the coroutines might be tricky and you will have to fiddle with the waitforseconds time to get it how you want it.
     
    Last edited: Dec 1, 2015