Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Portal Scripting

Discussion in 'Scripting' started by bobbillington6, Oct 6, 2014.

  1. bobbillington6

    bobbillington6

    Joined:
    Oct 6, 2014
    Posts:
    5
    I currently have a portal in a 2D level of mine and was wondering how I would write a script that would trigger the player upon contact with the portal collider to be moved to the center of the portal and then spin and shrink out of the level and into the next one. I began scripting the players position to the center of the portal with my very limited knowledge and nothing happened.

    #pragma strict

    var Player : GameObject;

    function OnTriggerEnter (other : Collider) {
    transform.position = Vector3(-0.64,-0.63,0);
    }
     
  2. djfunkey

    djfunkey

    Joined:
    Jul 16, 2012
    Posts:
    201
    Use code tags.
    To help you along you could equal the player position to the portals position e.g.
    Code (csharp):
    1. Player.transform.position = other.transform.position
    Then you would then rotate the player around using transform.rotate

    Then Scale the player down to 0 using transform.localScale and a a mathf function called lerp (mathf.lerp) this will scale the player over time to 0
     
    bobbillington6 likes this.
  3. Cpt Chuckles

    Cpt Chuckles

    Joined:
    Dec 31, 2012
    Posts:
    86
    i think you should have the portal warping all done in a separate script on the player, and disable the regular player script when it is running.

    you should also have a script component on the portal. we'll just call it PortalScript.cs. it doesn't actually have to have anything in it, it just has to exist. identifying objects based on what components they have is much faster than using names or tags because no text parsing is involved.

    PlayerScript would have these elements in it
    Code (csharp):
    1. //PlayerScript
    2.  
    3. [RequireComponent(typeof (GettingTeleported))] //make sure we have a GettingTeleported.cs script
    4. public class Player : MonoBehaviour {
    5.  
    6.     private GettingTeleported teleportScript; //a handle on our own teleport script
    7.  
    8.     void Awake() {
    9.         teleportScript = GetComponent<GettingTeleported>(); //fill the handle
    10.     }
    11.  
    12.     void Update() {
    13.         ... //just all the player controls etc
    14.     }
    15.  
    16.     void OnTriggerEnter( Collider other ) {
    17.         //if the trigger we entered has a PortalScript on it, then it's a portal
    18.         if( other.gameObject.GetComponent<PortalScript>() ) {
    19.             teleportScript.enabled = true; //enable the teleport script
    20.             teleportScript.target = other.gameObject.transform; //give the target to the teleport script
    21.             this.enabled = false; //turn off the player script to disable controls, etc
    22.         }
    23.     }
    24. }
    and the GettingTeleported would have stuff like this in it
    Code (csharp):
    1. //GettingTeleported.cs
    2.  
    3. public class GettingTeleported : MonoBehaviour {
    4.  
    5.     public Transform target; //a handle on the portal's transform
    6.  
    7.     void Start() {
    8.         enabled = false; //this makes sure the teleporting script doesn't run when the player is spawned
    9.     }
    10.  
    11.     void Update() {
    12.         if(target) { //if the target has been given a value...
    13.             //this line moves the player slightly towards the portal each frame
    14.             transform.position = new Vector3(Mathf.Lerp(transform.position, target.position, Time.deltaTime));
    15.             //this spins the player
    16.             transform.Rotate(Time.deltaTime * rotateSpeed; 0f, 0f);
    17.             //this swirls the player around the center of the portal to spiral in like a toilet:
    18.             transform.RotateAround(target.position, Vector3.forward, Time.deltaTime * rotateSpeed);
    19.  
    20.             //if we are basically at the center of the portal...
    21.             if((new Vector3(transform.position - target.position)).sqrMagnitude < 0.1f) {
    22.                 Application.LoadLevel("the next level");
    23.             }
    24.         }
    25.     }
    26. }
    this is all pseudo-code but i hope it gives you an idea of what i mean and helps you a bit
     
    bobbillington6 and djfunkey like this.
  4. bobbillington6

    bobbillington6

    Joined:
    Oct 6, 2014
    Posts:
    5
    Getting quite a few errors. Also should I be mixing java and sharp scripts. I renamed the existing PortalScript and applied it to the portal but it is a js script and I have no clue how to type in c# (only just getting a handle on js). Does it need to be in c#? Sorry about the time by the way, it's midday in Aus.
     

    Attached Files:

  5. djfunkey

    djfunkey

    Joined:
    Jul 16, 2012
    Posts:
    201
    @Cpt Chuckles wrote that code and had not tested it, so you need to check it before you put it straight into your game :p

    alright so a little bit of his code needs to be changed.
    First off, a float is being called for the player position that needs to be changed to a vector 3. try using Vector3.Lerp instead of mathf.Lerp, try looking here http://docs.unity3d.com/ScriptReference/Vector3.Lerp.html

    And its probably not good to be mixing up the languages to much because it can cause headaches down the road, so i would stick with one language and convert scripts if needed. you can find online converters in many places.
    Like here: http://www.m2h.nl/files/js_to_c.php ;)
     
  6. Cpt Chuckles

    Cpt Chuckles

    Joined:
    Dec 31, 2012
    Posts:
    86
    it's perfectly fine to have a mixture of UnityScript and C# in your project, as long as you use one language per script file :p they all get compiled at the end. just be aware that certain functionality probably won't transfer, like C# delegates or UnityScript "SendMessage". for example, in the AngryBots demo you will find mostly UnityScript but there are some C# scripts in the project as well.

    oh and i realize i had not named the PlayerScript class the same as the filename i suggested. when in C#, it's important to name your class the same as the filename or it won't work :s
     
  7. bobbillington6

    bobbillington6

    Joined:
    Oct 6, 2014
    Posts:
    5
    I managed to get rid of most of the errors and would like to convert all of it to javascript but it's not worth it because none of the converters can actually convert from c# to js. I am unfortunately, however, still getting 3 errors, having classified the PlayerScript as a js script, changing transform.postition to a Vector3.lerp and other stuff. I have emboldened the lines on which these errors occur. Could you please see if there is a solution because I have no clue (what on earth is a bool).

    //GettingTeleported.cs

    using UnityEngine;
    using System.Collections;

    public class GettingTeleported : MonoBehaviour {

    public Transform target;

    void Start() {
    enabled = false;
    }

    void Update() {
    if(target) {
    transform.position = Vector3.Lerp(transform.position, target.position, Time.deltaTime);
    transform.Rotate(Time.deltaTime * 30f, 0f, 0f);
    transform.RotateAround(target.position, Vector3.forward, Time.deltaTime * 30f);

    if((new Vector3(transform.position - target.position)).sqrMagnitude < 0.1f) {
    Application.LoadLevel("level 2");
    }
    }
    }
    }


    //PlayerScript.cs

    using UnityEngine;
    using System.Collections;

    [RequireComponent(typeof (GettingTeleported))]
    public class Player : MonoBehaviour {

    private GettingTeleported teleportScript;

    void Awake() {
    teleportScript = GetComponent<GettingTeleported>();
    }

    void OnTriggerEnter( Collider other ) {
    if( other.gameObject.GetComponent<PortalScript.js>() ) {
    teleportScript.enabled = true;
    teleportScript.target = other.gameObject.transform;
    this.enabled = false;
    }
    }
    }
     

    Attached Files:

  8. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    If you don't know what a bool is, you should seriously consider reading a couple of introductory chapters/tutorials on types in C#, especially if you want to keep writing code. That will save you a lot of headaches in the future.

    Also, http://bit.ly/1sk3BIK :p
     
    Last edited: Oct 9, 2014
  9. Cpt Chuckles

    Cpt Chuckles

    Joined:
    Dec 31, 2012
    Posts:
    86
    a bool is short for "boolean" which means a variable that simply holds either true or false (also represented as 1 or 0, respectively)

    and in the future make sure you enclose code in [ code ] code tags [ /code ] so it gets formatted on the forum post correctly

    and post the errors you are being given on those lines so we can see why the compiler doesn't like them