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. Dismiss Notice

Need Help With Javascript!

Discussion in 'Scripting' started by OneSource_, Jul 28, 2016.

  1. OneSource_

    OneSource_

    Joined:
    Jul 28, 2016
    Posts:
    1
    I have created a Script for a door. It works for opening and closing the door but I want to add sound. I don't like to program with JS (I use C#). Here is the script that works with animation to open the door.

    private var guiShow : boolean = false;

    var isOpen : boolean = false;

    var door : GameObject;

    var rayLength = 2;

    var DoorOpenSound = AudioClip;
    var DoorCloseSound = AudioClip;

    function Update()
    {
    var hit : RaycastHit;
    var fwd = transform.TransformDirection(Vector3.forward);

    if(Physics.Raycast(transform.position, fwd, hit, rayLength))
    {
    if(hit.collider.gameObject.tag == "Door")
    {
    guiShow = true;
    if(Input.GetKeyDown("e") && isOpen == false)
    {
    door.GetComponent.<Animation>().Play("DoorOpen");
    isOpen = true;
    guiShow = false;
    }

    else if(Input.GetKeyDown("e") && isOpen == true)
    {
    door.GetComponent.<Animation>().Play("DoorClose");
    isOpen = false;
    guiShow = false;
    }
    }
    }

    else
    {
    guiShow = false;
    }
    }

    function OnGUI()
    {
    if(guiShow == true && isOpen == false)
    {
    //I have nothing to put here xD
    }
    }

    I have set a var for DoorOpenSound and DoorCloseSound, but I really don't know what to do after that. If Anyone could help, It would be appreciated.
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    Agreed...if you use c#, why would you make the switch to unityscript?
    Also, please use code tags when asking for script related help.

    KelsoMRK is correct on the audiosource. You might also consider something off the asset store. We use https://www.assetstore.unity3d.com/en/#!/content/2679 at work (just the paid version, not the free one) and it works fairly well in making the process pretty easy to do.
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802