How can I make my player teleport to somewhere else on collision with object?
I am new to unity and I'm making a game where you drive a car to the end of this big lane, and there are lots of cubes that if you hit, the scene restarts. At the end of the lane I want the car to drive over a line and it teleports them to the next level (in the same scene). Please use simple terms as I'm very new to Unity :) thx in advance.
Answer by AurimasBlazulionis · Sep 14, 2016 at 08:55 AM
Where the lane is, create a box collider and set it to trigger.
Now, create a script which will handle everything for you. Add Transform
variable which will store the next level position. Add a method OnTriggerEnter (Collider col)
.
Inside it, you have to check if it is a car, not the cube. To do so, first assign a unique tag to your car. I assume you assign "car" tag. So the check looks like this: if (col.transform.root.CompareTag ("car"))
if it is true, then you have to set the car position and rotation to the next level. Remember that transform variable we had before? So you would have to do col.transfor.root.position = thatTransformWeTalkedAbout.position
and col.transfor.root.rotation = thatTransformWeTalkedAbout.rotation
.
This should do all you want.
Your answer
![](https://koobas.hobune.stream/wayback/20220612092726im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Teleporting trail renderer 3 Answers
How to move a character along the Y axis x amount of units using the G key? 1 Answer
Teleport character to opposite side of map when they hit edge 0 Answers
Can't make player character teleport from one side of screen to another. 1 Answer
Script for teleport on collision? 2 Answers