How to create a portal/teleport.
I have a game where you roll a ball around, avoid obstacles, and collect as many yellow cubes in the given time. How do I create a portal so that if a player goes on to a certain spot, they will be teleported to another area in the same scene? I'm fine with either an invisible portal that the player unknowingly goes in or like an actual oval-shaped portal that the player can see and just go through. All answers are appreciated. Thanks in advance!
Answer by Josp101 · Nov 03, 2016 at 10:30 PM
Make an object that you want to be the portal (can be invisible if you want).
Add a collider to the object so that the ball collides with the object when you want it to be teleported. Then just add a script to the ball.
void OnCollisionEnter(Collision collision) {
if(collision.gameobject.name == "invisible portal")
gameobject.transporm.position = new Vector2( *some position*)
}
This C# code is untested and this is just one very simple way of making a portal.
If this is a 3D game then you should just use any 3D collider (sphere, box, etc.).
It just depends on where you want the ball to be teleported from.
I might have done something wrong but an error message came up
Answer by Breeziggy · Feb 14, 2018 at 02:48 PM
@josp101 I tried this code for my game but it's saing i have a error at void OnCollisionEnter(Collision collision) { and it's saying it with the void. What should I do?
I gave this answer 2 years ago but I believe I made an error:
It should be:
if(collision.gameObject.name == "invisible portal")
(the change is gameobject to gameObject)
Your answer
Follow this Question
Related Questions
Teleporting MainCamera and GameController 1 Answer
What is wrong with my 2D portal code? 0 Answers
Making Portals 2 Answers
Script for teleport on collision? 2 Answers
Destroying a prefab bullet after time with the list 0 Answers