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.
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.
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.
- 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
_______________________________________________________
Player
- Make sure your player has the "Player" tag
- And that nothing you don't want to teleport doesn't
_______________________________________________________
Script
- 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
#pragma strict
var locator : GameObject;
var player : Transform;
player = GameObject.FindGameObjectWithTag("Player").transform;function OnMouseDown () {
player.position = locator.transform.position;
}
_______________________________________________________
Use
- 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
______________________________________________________________
Tips
- ** 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)!