x


Moving (teleporting) player and camera when they enter a certain area

Hello all,

I was recently working on a little project called cube. Basically you control a cube that you move around, but when you get to a certain point i want the cube to teleport and also the camera to move. Is there any way of doing this?. I want it so that if the cube gets within a certain area i want it to teleport.

Thanks!

more ▼

asked Feb 16 '10 at 09:45 AM

Elicoor gravatar image

Elicoor
31 9 10 13

(comments are locked)
10|3000 characters needed characters left

5 answers: sort voted first

You should use Triggers to achieve this.

Create a new GameObject to represent the trigger area. Choose the most appropriate shape. This will be the area that you have to enter in order to cause the teleport to occur.

Now select the new object you just made, and tick the "Is Trigger" option in the collider component, shown in the inspector pane. This changes the collider from a solid object to an area that you can walk into.

Un-tick the "Mesh Renderer" component, so that the GameObject becomes invisible.

You probably also want to specify a destination for the teleporter, and an easy way to do this is to create another invisible GameObject. This time however you don't need a collider either, so it's best to use "Create Empty" to create a completely empty GameObject. Name it "Teleport Destination".

Now, depending on how you want the teleporter to work, you need to either add some script to the teleporter object, or to the player object.

If you want the teleporter to teleport any object which enters it, add a script something like this to the teleporter object:

var destination : Transform;

function OnTriggerEnter(other : Collider) {
    other.transform.position = destination.position;
}

If you only want the player to teleport when it enters the trigger, you should make sure your player object is tagged as "Player", then add a check for this in your teleporter script, making it look more like this:

var destination : Transform;

function OnTriggerEnter(other : Collider) {
    if (other.tag == "Player") {
        other.transform.position = destination.position;
    }
}

See the Colliders Manual Page for more information about triggers.

Now for the camera. If your camera is parented to your player (as it is, in the case of the first-person controller prefab), then you need do nothing. The camera will be moved along with the player automatically.

If you're using a chase-style camera however, you'll probably want to make the camera jump the same relative distance as the player just jumped. You could do this by measuring the distance that the player moved, and then adding that distance to the camera too - something like this:

var destination : Transform;

function OnTriggerEnter(other : Collider) {
    if (other.tag == "Player") {
        var startPosition = other.transform.position;
        other.transform.position = destination.position;
        var moveDelta = other.transform.position - startPosition;
        Camera.main.transform.position += moveDelta;
    }
}
more ▼

answered Feb 16 '10 at 10:05 AM

duck gravatar image

duck ♦♦
41k 92 148 415

(comments are locked)
10|3000 characters needed characters left

you can teleport b setting the transform.position directly to a position vector. to see if you are in a certain area simple put a collider (with is trigger enabled) in that area and put your teleporting code inside OnTriggerEnter.

void OnTriggerEnter (Collider other)
{
if (other.name == "your object name")
{
other.transform.position = new Vector3(300,100,0);
camera.main.transform.position = new Vector3 (300,100,0);
}
}
more ▼

answered Feb 16 '10 at 11:40 AM

Ashkan_gc gravatar image

Ashkan_gc
9.1k 33 56 117

(comments are locked)
10|3000 characters needed characters left

You could also use this simple aproach I used when testing alternatives for respawning a character that collided with some object.

You put this in the object that has a collider and has the box "is trigger" activated

//this will be our "respawn point"
var startPosition : Transform;

//when it collides with the object (a portal or anything)
function OnTriggerEnter(theCollider : Collider) 
{
    //the object that collided (our player for example)
    //will now have the same position in space as the "respawn point"
    theCollider.transform.position = startPosition.position;
}

After you put this script in the object that you want (portal), you create an empty object (lets call it "respawn point"). and finally you just need to drag the "respawn point" game object in the "startPosition" var (on unity editor) and it will work.

more ▼

answered Feb 16 '10 at 12:36 PM

lollevel gravatar image

lollevel
191 2 8

er... isn't that exactly the same as my first example? :-)

Feb 19 '10 at 05:28 PM duck ♦♦

Sorry, just copy / pasted one of my scripts from one of my projects, didn't read your answer =)

Feb 19 '10 at 08:42 PM lollevel
(comments are locked)
10|3000 characters needed characters left

How can make the teleport point to teleport between 2 points?

Means point A teleport to point B then in the same object i can teleport back to point A.

more ▼

answered Dec 13 '10 at 03:44 AM

Zac gravatar image

Zac
1

(comments are locked)
10|3000 characters needed characters left

Thanks Everyone for the help!. How do i mark this as answered?

more ▼

answered Feb 19 '10 at 04:54 PM

Elicoor gravatar image

Elicoor
31 9 10 13

You need to click the tick next to the accepted answer.

Feb 19 '10 at 05:28 PM duck ♦♦

make sure you comment on stuff like that, only "Answer" a question if you are answering it, otherwise hit the add comment

Feb 22 '10 at 02:01 AM Adam Bruns
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2991
x1366
x981

asked: Feb 16 '10 at 09:45 AM

Seen: 9624 times

Last Updated: Feb 16 '10 at 10:12 AM