Search Unity

Updating a java script terms?

Discussion in 'Scripting' started by Dude_Mojo, Sep 17, 2016.

  1. Dude_Mojo

    Dude_Mojo

    Joined:
    Jul 14, 2016
    Posts:
    13
    Hey peeps ive got a legacy unity script I was able to pull from somewhere on the internet. Due to my bad scripting skills I cant figure out how to change the script so that it will function properly in the current version of unity. The script is applied to a game object so that the player can open the object like a drawer. But I presume that the only way to make the script work is for a UI element to appear first. The problem is the UI element the script needs appears to no longer exist in this current version of unity. Which leads me to believe that the only way for the script to work properly is to update it so that it functions correctly. I am a unity noob so please bare with me.

    Disclaimer : these scripts aren't mine

    [Script applied to player so that player can hit "e" to open the drawers in game]

    var GuiText : GUIText;

    function Start () {
    }

    function Update () {

    var hit: RaycastHit;

    GuiText.text = "";

    //open drawer test
    if(Physics.Raycast(transform.position,transform.forward,hit,5)){

    var hitObject : GameObject = hit.collider.gameObject;

    if(hitObject.tag=="drawer"){

    var Drawer = hitObject.GetComponent(Drawer);

    if(!Drawer.opening && !Drawer.opened){

    GuiText.text = "Press E to open";

    //open drawer
    if(Input.GetKeyDown("e")){
    Drawer.opening = true;
    }
    }
    }
    }
    }


    [Script applied to game object so that it can be opened like a drawer]

    #pragma strict

    var smoothTime : float = 0.1;
    var maxSpeed = 8;
    var openDistance : float = 0.5;

    var opening : boolean;
    var opened : boolean;

    private var origin : Vector3;
    private var velocity = Vector3.zero;

    function Start (){
    origin = transform.position;
    }

    function Update (){

    if(opening){

    var targetPosition : Vector3 = origin + transform.forward * openDistance;

    transform.position = Vector3.SmoothDamp(transform.position,
    targetPosition, //open drawer
    velocity, smoothTime, maxSpeed);

    if(Vector3.Distance(transform.position, targetPosition) < 0.1){
    opening = false;
    opened = true;
    }
    }
    }
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
  3. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    While the GUI type of UI is the old style of UI it should still be all usable in the current version of Unity. What actual errors do you get when you put these scripts on the appropriate objects?