Search Unity

Need some Javascripting support

Discussion in 'Scripting' started by ddulshan, Aug 16, 2014.

  1. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Hey guys. I'm making a game and I faced some scripting problems. I'm no scripting expert, so here's my problems.

    Ok, so. I've Instantiated a object(figure) when the player reach a collider without a Mesh Render with trigger unabled, I just need to use this as a trigger that's why I removed the Mesh Render component. Here's the script I've made.

    Code (JavaScript):
    1. var figure : GameObject;
    2. var spawn : Transform;
    3.  
    4. function OnTriggerEnter ()
    5. {
    6.      Instantiate(figure, spawn.position, spawn.rotation);
    7. }
    1. I need the Instantiated object to move along the X-axis in a certain speed when the player is moving towards it.
    2. I need the trigger to turn off after the player reach it the first time, so the object wont Instantiate always the player steps on the trigger again and again.
    3. I need the Instantiated object to destroyed after reaching another trigger.
    4. Lastly, I need some text(like subtitles) to appear when entered a trigger. I realy hate using the GUIText option. Is there a way to do it with scripting?

    How can I do these, please explain me step-by-step. I'm just a beginner in scripting. Thanks is Advance.

    Edit:

    1. Can I use "collider.isTrigger = true;" to "false" to turn off the trigger after entering the trigger for the first time? Please help me I'm just trying to learn.
     
    Last edited: Aug 16, 2014
  2. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    1. Create a script called "FigureScript" or something that adds X-axis movement to your figure gameObject.
    Something like this should work :
    Code (JavaScript):
    1. var speed = 10 //Set this to whatever
    2. transform.Translate(speed * Time.deltaTime,0,0);
    2. You can simply destroy the object, de-activate it or disable the script so that It wont execute.

    3. You could add an OnTriggerEnter function in the "FigureScript" script that destroys the figure gameObject.

    4. Use GUI Labels.
    Code (JavaScript):
    1. function OnGUI(){
    2. GUI.Label(Rect(Screen.width / 2 - 50, Screen.height - 50, 100,50),"Your In A Trigger");
    3. }
    You should go through the Learn section , you'll pick up tonnes of useful stuff there.
     
  3. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    @Intense_Gamer94 : Thanks for replying.

    1. So, I need to make a new script for the GameObject and attach to it, so it'll execute as soon as it's Instantiated? I made this my self, will it work?

    Code (JavaScript):
    1. var speed : int = 10;
    2.  
    3. function Update ()
    4. {
    5.      if(Input.GetButtonDown("w"))  //I use this because it'll move if only player moves forward. If not right, how do it?
    6.      {
    7.           transform.Translate(speed * Time.deltaTime, 0, 0);
    8.      }
    9. }
    2. Ok, got it.

    3. Ok.

    4. I'll try it, and what did u meant by "Your in a trigger"? The text to display?

    Edit:

    1. I feel like the "If get button down w the object moves" is not good enough, I mean if the player goes back and move forward again the object will be far ahead. So is there a way to do it(move object fwd) when the player is in some radius? So the object will be in certain distance even when the player moves back.
     
  4. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Yeah, you should attach a script to your figure gameObject so it'll execute when it's Instantiated. Your script looks fine.

    "Your In A Trigger" is the text to display. You can put whatever you want there.

    You could look into Vector3.Distance to move the object only when the player is close enough or do something like :
    Code (JavaScript):
    1. if(Input.GetAxis("Vertical") > 0){
    2. transform.Translate(speed * Time.deltaTime,0,0);
    3. }
    4. else if(Input.GetAxis("Vertical") < 0){
    5. transform.Translate(-speed * Time.deltaTime,0,0);
    6. }
     
  5. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    @Intense_Gamer94 : Thanks! Works great! And is there a way to destroy the figure by fading it out without disappearing it instantly? And instantiate it with fading in effect? And how to make Slow motion effect?

    EDIT:

    1. I tried the code u gave above to move the figure. But still the figure is faster than the player. I even tried minus no. still. (Solved)

    2. When the player turns back and press the forward button, the figure move away. Moves back when the player turned to the opposite side.

    Please help.
     
    Last edited: Aug 17, 2014
  6. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    About the fading, you'll have to change its shader to Transparent/Diffuse and smoothly lerp the alpha from 0 to 1 then change it's shader back to diffuse (or whatever shader your using). Do the opposite for a fade out effect.

    Look into "Time.timeScale" to create cool slow mo effects.

    The code I gave you, moves the object forward when you press (W) and back when you press (S). If you want it to always follow you, you could just make it look at you all the time :
    Code (JavaScript):
    1. transform.LookAt(Player);
     
  7. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    I don't understand what you said about the Fading, can you please explain it step-by-step?

    The slow motion works.

    And the code you gave above on moving the figure, a error shows "Unknown Identifier Player".

    And about destroying the Instantiated figure, it doesn't seems to work, here's the code I use.

    Code (JavaScript):
    1. var figure : GameObject;
    2.  
    3. function OnTriggerEnter ()
    4. {
    5.      Destroy(figure);
    6. }
    I assigned the figure prefb(I think It wont work that way, error "Destroying assests is not permitted for avoid data loss") to the figure GameObject. And I assigned this script to a trigger. Please help.
     
  8. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    You'll need to add a "Player" variable in the script. Since it's an Instantiated GameObject, you'll have to assign the Player during runtime.
    Code (JavaScript):
    1. var Player : Transforrm;
    2. function Start(){
    3. Player = GameObject.FindWithTag("Player").transform; //Put the "Player" tag on your player for this to work.
    4. }
    Use a parameter in your OnTriggerEnter function :
    Code (JavaScript):
    1. function OnTriggerEnter(other : Collider){
    2. if(other.gameObject.name == "figure"){ //Replace "figure" with the hierarchy name of your figure object.
    3.  
    4. Destroy(other.gameObject);
    5. }
    6. }
    About the fading, it's pretty simple. Just change the shader of your figure gameObject during runtime and lerp the alpha to 1.
    Here's a super basic example of how you'd do it :
    Code (JavaScript):
    1. private var transparentShader : Shader;
    2. private var normalShader : Shader;
    3.  
    4. function Start () {
    5. transparentShader = Shader.Find("Transparent/Diffuse");
    6. normalShader = Shader.Find("Diffuse");
    7. gameObject.renderer.material.shader = transparentShader;
    8. gameObject.renderer.material.color.a = 0;
    9. }
    10.  
    11. function Update () {
    12. gameObject.renderer.material.color.a = Mathf.Lerp(gameObject.renderer.material.color.a,1,Time.deltaTime);
    13. }
    Look through the Learn Section, Scripting Reference and also Google some of these things.
     
    Last edited: Aug 18, 2014
  9. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Wow thanks! And is there a way to assign different triggers to different OnTriggerEnter functions.

    I mean, there are 2 triggers "trigger1, trigger2". can I make a something happen if we steps on trigger1 and something else happen when we steps on trigger2. I need these 2 triggers on the same script. For example I wrote down like this.

    Code (JavaScript):
    1. function OnTriggerEnter (trigger1)
    2. {
    3. .............................
    4. }
    5.  
    6. function OnTriggerEnter (trigger2)
    7. {
    8. ..................
    9. }
    At first I tried this and it worked. Then not, why?
     
  10. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Use "else if" statements :
    Code (JavaScript):
    1. function OnTriggerEnter(other : Collider){
    2. if(other.gameObject.name == "Trigger1"){
    3. //Do Something Cool
    4. }
    5. else if (other.gameObject.name == "Trigger2"){
    6. //Do Something Awesome
    7. }
    8. else if (other.gameObject.name == "Trigger3"){
    9. //Do Something Super Awesome
    10. }
    11. //And so on...
     
  11. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Great, thanks man you really helped me! And can I put up just "If" for all that's containing "else if"?
     
  12. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Yeah, that would work aswell.
     
  13. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Got a little problem. I use the above code and tried making a variable "trigger1 : GameObject;" and assigned the trigger GameObject for it and the name of the variable for the string, like this.

    Code (JavaScript):
    1. var trigger1 : GameObject;
    2.  
    3. function OnTriggerEnter (other : Collider)
    4. {
    5.      if(other.gameObject.name == "trigger1")//The name of the trigger's variable
    6.       {
    7.       //Happen Something
    8.       }
    9. }
    It didn't work, so I put the ingame name of the trigger, didn't work either. Help. Here's my current code.

    Code (JavaScript):
    1. var figure : GameObject;
    2. var spawn : Transform; //The Instantiating figure and spawn position.
    3.  
    4. function OnTriggerEnter (other : Collider)
    5. {
    6.      if(other.gameObject.name == "Lv1S1_1")//Ingame name of the trigger
    7.      {
    8.           Instantiate(figure, spawn.position, spawn.rotation);
    9.           Destroy(gameObject);
    10.      }
    11. }
     
    Last edited: Aug 20, 2014
  14. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Your code looks fine. Are you getting any errors in the console?

    Make sure that this script is attached to the player(or the gameObject that enters the trigger) and the hierarchy name of the trigger is correct and it should work.