Saturday, August 30, 2014

Unity Tutorial: Easy Teleport in JS

UnityGirl's Easy Teleportation

Whether it's SciFi or Fantasy, many games make use of convenient teleportation. There are several ways to do this in Unity, and some of them are a lot bigger pains than others. Here's the quickest, easiest way I've found to get your character where it needs to be with no guessing at large world coordinates.

Teleporter Setup
Teleporter Setup

This works along the same principle as the Building Instantiator, using an existing Game Object in the  scene to represent where we want to go.

Capsule Locator Sitting in a Chair
  • Create a GameObject to represent where you want your character to be
    • I use one I can see, then turn off the Mesh Renderer and Collider
  • Put your Locator object wherever you want your player to teleport to
    • make sure it's high enough you won't fall through the floor/ground
  • TURN OFF the Mesh Renderer** and any Colliders***
  • Name it something recognizable
Setting Player Tag

  • Make sure your player has the "Player" tag
    • And that nothing you don't want to teleport doesn't


  • Establish your Locator
  • Find your Player
  • Tell the Player to be at the Locator based on some criteria
    • We're using OnMouseDown so clicking the script-bearing object will trigger the 'port.


The Script

#pragma strict

var locator : GameObject;
var player : Transform;
player = GameObject.FindGameObjectWithTag("Player").transform;

function OnMouseDown () {
player.position = locator.transform.position;


  • Attach the script to a Game Object
  • Assign the Locator in the Inspector or GameObject.Find() it's unique name
  • Hit Play
  • Click on the script-bearing Game Object
  • Teleport!

Alternative Teleport Triggers
  • Other button based triggers
    • ex: OnMouseUp, Input.GetMouseButton(), Input.GetKey(Up/Down) and so forth
  • OnTrigger Enter or OnTriggerExit
    • For this you will want to place the script on some kind of trigger object for the character to walk/fall into with the collider marked "Is Trigger"
    • ex: simulated "trip wire" with low to the ground stretched GameObject with 'trigger' collider
  • On a Timer
    • There are many kinds of timer scripts, I'll cover some simple favorites in the near future
  • Based on some other random game criteria
    • ex: player solves puzzle, puzzle success bool triggered, player teleports based on boolean trigger

See It In Action


  • ** If you haven't turned off your Mesh Renderer, you'll be able to see your Locator
  • *** If you haven't turned off your Collider, you might get stuck inside your Locator

All blog posts by UnityGirl are inspired by my work with Brunelleschi: Age of Architects on the Aesop Games team. Check out our Crowd Funding page(s)!

Tuesday, August 26, 2014

Unity Tutorial: Instantiating a Building on a Lot - JS

Here's a quick guide to how to Instantiate a Prefab to a specific position and rotation. The example I'll be using is calling a pre-made building (prefab) onto a lot, but the scripting operation can be used for any location-specific Instantiation.


Object and Target

The key to this technique is to have a target-object that will represent the position and rotation you would like your object to take. For the sake of this tutorial, we'll use a Lot. This is a square with the scale dimensions of  40, 1, 40 and given a dirt material.

This is a Cube Game Object flattened in a large square set in a terrain. It is serving as my lot, but you can use anything as your position-base.
Model Street Lot
Now here's the script in Unity's very own JavaScript. This script has been tested and should work if you

  • Assign "lot" to your target Game Object in the scene
    • drag object from Hierarchy into "lot" slot in the Inspector
  • Store your Building prefab in your Resources folder
    • drag building object into Project to save it as a Prefab
  • set "resourceLoadString" as the path to your prefab in your Resources folder
    • ex: Bakery is in Assets/Resources/Buildings
    • resourceLoadString = "Buildings/Bakery"

The Script

#pragma strict

var building : GameObject;
var lot : GameObject;
var buildHere : GameObject;

var resourceLoadString : String;

var thisPos : Vector3 = new Vector3();
var thisRot : Quaternion = new Quaternion();

function Start () {
//sets your building's position to the same coordinates as your lot
thisPos = lot.transform.position;
//this raises your building up a bit above the lot so you don't get z-fighting flickers
thisPos[1] = thisPos[1] + 0.45f;

thisRot = lot.transform.rotation;

//this string should be your path through your Resources folder
//if you want the building to build on some kind of condition, use these lines inside your condition {}
resourceLoadString = "Buildings/Bakery";
buildHere = Instantiate(Resources.Load(resourceLoadString), thisPos, thisRot) as GameObject;



Visual Guides


See It In Action

Apply the script to one of the GameObjects in the scene. I use the Main Camera because it's not going anywhere.

This same premise can be applied to creating any prefab at a GameObject determined position and/or rotation. ^_^

All blog posts by UnityGirl are inspired by my work with Brunelleschi: Age of Architects on the Aesop Games team. Check out our Crowd Funding page(s)!

Girl, Unity 3D, and Brunelleschi

Why I Started This Blog:

Why You Might Want to Read This Blog:
  • To learn the methods, tips, and tricks I've learned without combing the forums for hours
  • To spy on the secret life of the elusive female Dev
  • (<_<)   (>_>)
  • To join the secret legions of female Devs without combing the forums for hours (or having to ask the boys how to do it)

Unity Girl
  • Will be played by AesopRebecca (Rebecca of Aesop Games)
  • Encourages all girls to become Game Developers (and in general, take over the computer world)
  • Is open to sister Female Devs that want to join in
  • Is based on my work with Brunelleschi: Age of Architects

    (said ee-sop)
    • is a girl
    • is from Texas. You can't make her go back.
    • Is obsessed with virtual realities
    • Has been working with Unity for about 4 months
    • Would like more girls to come make more virtual realities with her.
    • Will be online from 4pm-12am Pacific Time for the Brune Support Campaign


    Brunelleschi: Age of Architects:

         Ever find yourself looking at a fully designed and implemented web game thinking to yourself "It sure would be great to have a 3D representation of this."?

         Brunelleschi : Age of Architects (Brune for short) is a fully functional society-building web game that has been in steady development for years. Recently, the two founders (AesopMargaret and AesopMatt) invited Rebecca to become AesopRebecca and learn Unity for Brune's future 3D client.

        Thus, armed with nothing but a lifetime familiarity with technology, the vast internet, and a copy of Unity 3D Basic, AesopRebecca underwent a second transformation into UNITY GIRL! Now Brune has a 20 district self-building map, integrated web views, and an interlinking teleportation system.

         Ladies, it only took me 4 months to go from "How do I move this cube?" to over 70+ building models and a vast player map for web-game built settlements. Don't let anyone tell you it's too hard, there's too much, or you don't have enough time.

       Unity Girl is here to help with tutorials, cool work-arounds, and simple tricks for the beginner to intermediate Unity developer. Stay tuned!


    All blog posts by UnityGirl are inspired by my work with Brunelleschi: Age of Architects on the Aesop Games team. Check out our Crowd Funding page(s)!