- Home /
Teleport script, who can help me?
here is a script I made:
function OnTriggerEnter(other : Collider) { if (Input.GetKeyDown ("e")) Application.LoadLevel (1); }
function Awake () { DontDestroyOnLoad (transform.gameObject); transform.position = Vector3(71,79,-456); }
I used it in a door object/mesh, and when I start the game the door suddenly disapear why? I didnt turned the "trigger button
Then I deleted the function awake and the door didnt disappear anymore but the script didnt work either.
please help me!
All i want to do is that the FPS controller teleports to a new scene/level when he preses e in front of the door with the exact position.
As some can guess I still a big noob, I'm aware that I ask for a big help here but it takes time before I can manage all this on my own. thank you.
Please select your code and press the 101010 button in the future.
Answer by DaveA · Feb 14, 2011 at 07:57 PM
You will fire OnTriggerEnter in the frame when that happens. You will get GetKeyDown in the frame that happens. In your code, both would have to happen in the same frame for this to load the level. Yu could use Input.GetKey("e") instead, it will tell you if the 'e' key is down when the trigger is entered, but sounds like you want the reverse, so use OnTriggerStay instead, which will be called all frames you stand near the door. Then Input.GetKeyDown will go true when the 'e' key is hit.
thank you, but that doesnt work.
not what you told me but the whole script. I put "ontriggerstay" and you are right that's the effect I wanted.
But when i press the '"e" key in front of the door nothing happens.
Why?
To make it more simple, you've got a FPS chara controller(ccapsule) and a door.
I just need a script, to teleport the capsule on another scene, and in a specific position when I press "e" in front of it.
That's all.
Why nobody can help me do that? it something as simple as 1+1 for someone who knows scripts.
I don't think you transport it to another scene, unless you mean to another part of the same scene. If it's to another part of the same scene, you set the transform.position of the object to the new spot. If it's in a completely different scene, you'll be loading a whole new scene right? If there is only one place to spawn there (your teleport destination), then set the controller in THAT scene to that spot. If on the other hand you will have multiple possible destinations in that new scene, and you want to pick it, read on:
You'll need to have a piece of data persist across the scene load. You can do this by using PlayerPrefs, or Object.DontDestroyOnLoad. In the latter, you could do that on the capsule, but probably better just make an string var like 'destination' which would be the name of an object in the new scene. When the scene loads, a script there would have a Start or Awake function that would read that string, look for that object, get it's transform.position, and set the capsule position to that.
Answer by Shatterstar · Jun 11, 2014 at 01:59 PM
I think you have to write a function inside your camera script that teleports your camera also.